| 1 | package spiffy.core.util; |
| 2 | |
| 3 | import java.util.ArrayList; |
| 4 | import java.util.Collection; |
| 5 | |
| 6 | /** |
| 7 | * Helper methods for collections |
| 8 | * |
| 9 | * @author Kasper B. Graversen, (c) 2007 |
| 10 | */ |
| 11 | public class CollectionHelper { |
| 12 | |
| 13 | /** |
| 14 | * An easy way to construct an array list. just do |
| 15 | * |
| 16 | * <pre> |
| 17 | * ArrayList<String> al = CollectionHelper.ArrayList("a", "b", "c"); |
| 18 | * </pre> |
| 19 | * |
| 20 | * Rather than |
| 21 | * |
| 22 | * <pre> |
| 23 | * ArrayList<String> al = new ArrayList<String>() |
| 24 | * al.add("a"); |
| 25 | * al.add("b"); |
| 26 | * al.add("c"); |
| 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<? extends Object> genericList = arrayList(1, "two", 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<Person> 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 | } |