ch.elca.el4j.util.codingsupport
Class CollectionUtils

java.lang.Object
  extended by ch.elca.el4j.util.codingsupport.CollectionUtils

public class CollectionUtils
extends Object

This class supports methods to handle with collections. It covers only gaps of class org.springframework.util.CollectionUtils.

Author:
Martin Zeltner (MZE), Adrian Moos (AMS)
File-location:
CollectionUtils
Last check-in date:
2009-08-04 13:59:45 +0200 (Di, 04. Aug 2009) by swismer for revision 3873

Constructor Summary
protected CollectionUtils()
          Hidden constructor.
 
Method Summary
static
<T> List<T>
asList(List<T> list)
          Method to always return a list object.
static
<K,V> Map<K,V>
asMap(Map<K,V> map)
          Method to always return a map object.
static
<T> Set<T>
asSet(Set<T> set)
          Method to always return a set object.
static boolean containsOnlyObjectsOfType(Collection<?> c, Class<?> containingClassType)
          Method to check if a collection contains only objects which are equals, a subclass or implements the given class.
static boolean containsOnlyObjectsOfType(Collection<?> c, Class<?>[] containingClassTypes)
          Method to check if a collection contains only objects which are equals, a subclass or implements one of the given classes.
static
<T> FilteredList<T>
filtered(List<? extends T> list, Filter<? super T> filter)
          returns a filtered view on list.
static
<T> int
find(List<T> list, T t, int startAt)
          Finds the first element with index at least startAt that equals o.
static boolean isEmpty(Collection<?> c)
          Null save check if a collection is empty.
static
<T,O> TransformedList<T,O>
mapped(List<T> list, Function<? super T,O> function)
          Convenience method returning a TransformedList view to the supplied list.
static Collection nullSaveAddAll(Collection base, Collection annex)
           
static
<T> void
orderLike(ExtendedReorderableList<T> list, List<? extends T> example)
          See ExtendedReorderableList.orderLike(List).
static
<T> void
swap(List<T> list, int i, int j)
          swaps the elements at locations i and j.
static
<T> T[]
toArray(List<? extends T> list, Class<T> c)
          Copies the list's contents to an array.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CollectionUtils

protected CollectionUtils()
Hidden constructor.

Method Detail

isEmpty

public static boolean isEmpty(Collection<?> c)
Null save check if a collection is empty.

Parameters:
c - Is the given collection
Returns:
Returns true if the given collection is null or empty.

asList

public static <T> List<T> asList(List<T> list)
Method to always return a list object.

Parameters:
list - Is the list which should not be null.
Returns:
Returns the given list if it is not null. Otherwise it returns an empty list.

asSet

public static <T> Set<T> asSet(Set<T> set)
Method to always return a set object.

Parameters:
set - Is the set which should not be null.
Returns:
Returns the given set if it is not null. Otherwise it returns an empty set.

asMap

public static <K,V> Map<K,V> asMap(Map<K,V> map)
Method to always return a map object.

Parameters:
map - Is the map which should not be null.
Returns:
Returns the given map if it is not null. Otherwise it returns an empty map.

containsOnlyObjectsOfType

public static boolean containsOnlyObjectsOfType(Collection<?> c,
                                                Class<?>[] containingClassTypes)
Method to check if a collection contains only objects which are equals, a subclass or implements one of the given classes. If the given collection is empty, true will be returned.

Parameters:
c - Is the collection to check.
containingClassTypes - Are the class types which are expected.
Returns:
Returns true if the given collection contains only objects which are equals, a subclass or implements one of the given classes

containsOnlyObjectsOfType

public static boolean containsOnlyObjectsOfType(Collection<?> c,
                                                Class<?> containingClassType)
Method to check if a collection contains only objects which are equals, a subclass or implements the given class. If the given collection is empty, true will be returned.

Parameters:
c - Is the collection to check.
containingClassType - Is the class type which is expected.
Returns:
Returns true if the given collection contains only objects which are equals, a subclass or implements the given class.

find

public static <T> int find(List<T> list,
                           T t,
                           int startAt)
                throws NoSuchElementException
Finds the first element with index at least startAt that equals o. This method may take linear time.

Type Parameters:
T - the list's element type (duh!)
Parameters:
list - the list to search
t - the element to look for
startAt - the index to start the search at
Returns:
the index of the first matching element
Throws:
NoSuchElementException - if no such element exists.

orderLike

public static <T> void orderLike(ExtendedReorderableList<T> list,
                                 List<? extends T> example)
See ExtendedReorderableList.orderLike(List).

Type Parameters:
T - see there
Parameters:
list - see there
example - see there

swap

public static <T> void swap(List<T> list,
                            int i,
                            int j)
swaps the elements at locations i and j. If this list is a ExtendedReorderableList, its swap method is invoked, otherwise, set/get is used. This behaves like Lists always declared a convenience swap method, that is overridden if ReordableRandomAccessList is implemented.

Type Parameters:
T - .
Parameters:
list - see above
i - see above
j - see above

toArray

public static <T> T[] toArray(List<? extends T> list,
                              Class<T> c)
Copies the list's contents to an array.

Type Parameters:
T - the arrays element type
Parameters:
list - the list to copy
c - the element type for the new array
Returns:
an new array containing this list's contents

filtered

public static <T> FilteredList<T> filtered(List<? extends T> list,
                                           Filter<? super T> filter)
returns a filtered view on list.

Type Parameters:
T - .
Parameters:
list - see above
filter - the filter deciding which elements to include
Returns:
the filtered view

mapped

public static <T,O> TransformedList<T,O> mapped(List<T> list,
                                                Function<? super T,O> function)
Convenience method returning a TransformedList view to the supplied list.

Type Parameters:
T - the backing list's element type
O - this list's element type
Parameters:
list - Is the list to transform.
function - the transformation function to apply to each element
Returns:
see above

nullSaveAddAll

public static Collection nullSaveAddAll(Collection base,
                                        Collection annex)
Parameters:
base - Is the collection to enrich.
annex - Is the collection with the items to enrich the base collection.
Returns:
Return the complemented base collection.


Copyright © 2005-2011 ELCA. All Rights Reserved.