|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectspiffy.core.util.CollectionHelper
public class CollectionHelper
Helper methods for collections
Constructor Summary | |
---|---|
CollectionHelper()
|
Method Summary | ||
---|---|---|
static
|
arrayList(T... elements)
An easy way to construct an array list. just do ArrayList<String> al = CollectionHelper.ArrayList("a", "b", "c"); Rather than ArrayList<String> al = new ArrayList<String>() al.add("a"); al.add("b"); al.add("c"); |
|
static ArrayList<? super Object> |
arrayListObjects(Object... elements)
An easy way to generate an ArrayList holding mixed types of objects. |
|
static
|
firstOnly(Collection<T> collection)
Checks to see if the collection is of size 1 and if so returns that element. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public CollectionHelper()
Method Detail |
---|
public static <T> ArrayList<T> arrayList(T... elements)
ArrayList<String> al = CollectionHelper.ArrayList("a", "b", "c");Rather than
ArrayList<String> al = new ArrayList<String>() al.add("a"); al.add("b"); al.add("c");
elements
- the elements to create an ArrayList of
public static ArrayList<? super Object> arrayListObjects(Object... elements)
ArrayList<? extends Object> genericList = arrayList(1, "two", 3.0);thus you should use this helper method instead
elements
- elements to store in the list
public static <T> T firstOnly(Collection<T> collection)
This method is particularly nice for DAO implementations, as all get methods should return a collection of objects rather than just one object. This enables the DAO to return several objects in case the query is wrong, or worse, if there are data problems in the database. Hence avoid code such as
class PersonDao { Person getPerson(String arg1, String arg2); }instead use
class PersonDao { Collection<Person> getPerson(String arg1, String arg2); }and query the first element with this method
collection
- any non-collection
IllegalStateException
- when collection is not of size 1
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |