EMMA Coverage Report (generated Sat May 17 10:56:07 GMT 2008)
[all classes][spiffy.core.util]

COVERAGE SUMMARY FOR SOURCE FILE [CollectionHelper.java]

nameclass, %method, %block, %line, %
CollectionHelper.java100% (1/1)100% (4/4)98%  (95/97)94%  (17/18)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class CollectionHelper100% (1/1)100% (4/4)98%  (95/97)94%  (17/18)
CollectionHelper (): void 100% (1/1)100% (3/3)100% (1/1)
arrayList (Object []): ArrayList 100% (1/1)100% (32/32)100% (6/6)
arrayListObjects (Object []): ArrayList 100% (1/1)94%  (30/32)83%  (5/6)
firstOnly (Collection): Object 100% (1/1)100% (30/30)100% (5/5)

1package spiffy.core.util;
2 
3import java.util.ArrayList;
4import java.util.Collection;
5 
6/**
7 * Helper methods for collections
8 * 
9 * @author Kasper B. Graversen, (c) 2007
10 */
11public class CollectionHelper {
12        
13        /**
14         * An easy way to construct an array list. just do
15         * 
16         * <pre>
17         * ArrayList&lt;String&gt; al = CollectionHelper.ArrayList(&quot;a&quot;, &quot;b&quot;, &quot;c&quot;);
18         * </pre>
19         * 
20         * Rather than
21         * 
22         * <pre>
23         * ArrayList&lt;String&gt; al = new ArrayList&lt;String&gt;()
24         * al.add(&quot;a&quot;);
25         * al.add(&quot;b&quot;);
26         * al.add(&quot;c&quot;);
27         * </pre>
28         * 
29         * @param elements
30         *            the elements to create an ArrayList of
31         * @return a freshly created <tt>ArrayList</tt> containing elements given as arguments. If elements is null, null
32         *         is returned.
33         * @since 0.1
34         */
35        public static <T> ArrayList<T> arrayList(final T... elements) {
36                if( elements == null )
37                        return null;
38                final ArrayList<T> result = new ArrayList<T>(elements.length);
39                for(final T elem : elements) {
40                        result.add(elem);
41                }
42                return result;
43        }
44        
45        /**
46         * An easy way to generate an ArrayList holding mixed types of objects. The current type system in Java does not
47         * allow you to say
48         * 
49         * <pre>
50         * ArrayList&lt;? extends Object&gt; genericList = arrayList(1, &quot;two&quot;, 3.0);
51         * </pre>
52         * 
53         * thus you should use this helper method instead
54         * 
55         * @param elements
56         *            elements to store in the list
57         * @return an array list
58         */
59        public static ArrayList<? super Object> arrayListObjects(final Object... elements) {
60                if( elements == null )
61                        return null;
62                final ArrayList<? super Object> result = new ArrayList<Object>(elements.length);
63                for(final Object elem : elements) {
64                        result.add(elem);
65                }
66                return result;
67        }
68        
69        /**
70         * Checks to see if the collection is of size 1 and if so returns that element.
71         * <p>
72         * This method is particularly nice for DAO implementations, as all get methods should return a collection of
73         * objects rather than just one object. This enables the DAO to return several objects in case the query is wrong,
74         * or worse, if there are data problems in the database. Hence avoid code such as
75         * 
76         * <pre>
77         * class PersonDao {
78         *         Person getPerson(String arg1, String arg2);
79         * }
80         * </pre>
81         * 
82         * instead use
83         * 
84         * <pre>
85         * class PersonDao {
86         *         Collection&lt;Person&gt; getPerson(String arg1, String arg2);
87         * }
88         * </pre>
89         * 
90         * and query the first element with this method
91         * 
92         * @param collection
93         *            any non-collection
94         * @return first element of a collection, if the collection has a size of 1.
95         * @throws IllegalStateException
96         *             when collection is not of size 1
97         * @since 0.3
98         */
99        public static <T> T firstOnly(final Collection<T> collection) {
100                if( collection == null )
101                        throw new IllegalArgumentException("argument collection is null");
102                if( collection.size() != 1 )
103                        throw new IllegalStateException("Collection has size " + collection.size() + " must have size 1!");
104                return collection.iterator().next();
105        }
106}

[all classes][spiffy.core.util]
EMMA 2.0.5312 (C) Vladimir Roubtsov