org.deegree.commons.utils
Class CollectionUtils

java.lang.Object
  extended by org.deegree.commons.utils.CollectionUtils

public class CollectionUtils
extends Object

CollectionUtils

Version:
$Revision: 27494 $, $Date: 2010-10-22 14:23:34 +0200 (Fri, 22 Oct 2010) $
Author:
Andreas Schmitz, last edited by: $Author: aschmitz $

Nested Class Summary
static interface CollectionUtils.Mapper<T,U>
          Mapper gives a name to a simple function.
static interface CollectionUtils.Predicate<T>
           
static interface CollectionUtils.Reducer<T>
          Reducer
 
Field Summary
static CollectionUtils.Reducer<Boolean> AND
           
static CollectionUtils.Mapper<String,double[]> DOUBLE_PRINTER
          Use like this: System.out.println(map(List object, DOUBLE_PRINTER)) Unfortunately, a generic version for all primitive-arrays is not possible...
 
Constructor Summary
CollectionUtils()
           
 
Method Summary
static
<T> void
addAllUncontained(Collection<T> col1, Collection<T> col2)
          Adds all elements from col2 to col1, if they're not already contained.
static
<T> ArrayList<T>
clearNulls(Collection<T> col)
           
static
<T> void
clearNulls(List<T> col)
          Removed null elements in-place.
static
<T> List<T>
filter(Collection<T> col, CollectionUtils.Predicate<T> pred)
           
static
<T> CollectionUtils.Mapper<String,T[]>
getArrayPrinter()
           
static
<T> CollectionUtils.Mapper<Boolean,T>
getInstanceofMapper(Class<?> c)
           
static CollectionUtils.Reducer<String> getStringJoiner(String delim)
           
static
<T,U> LinkedList<T>
map(Collection<U> col, CollectionUtils.Mapper<T,U> mapper)
          Wraps a for loop and the creation of a new list.
static
<T,U> LinkedList<T>
map(U[] col, CollectionUtils.Mapper<T,U> mapper)
          Wraps a for loop and the creation of a new list.
static
<T> T
reduce(T identity, Collection<T> col, CollectionUtils.Reducer<T> reducer)
           
static
<T> List<T>
removeDuplicates(List<T> list)
          Keeps and returns the original list.
static
<T,U,V> Triple<ArrayList<T>,ArrayList<U>,ArrayList<V>>
unzip(Collection<Triple<T,U,V>> col)
           
static
<T,U> Pair<ArrayList<T>,ArrayList<U>>
unzip(Map<T,U> map)
           
static
<T,U> Pair<ArrayList<T>,ArrayList<U>>
unzipPair(Collection<Pair<T,U>> col)
          This indirection is useful in case you use ComparablePairs instead of normal ones and want to unzip.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DOUBLE_PRINTER

public static final CollectionUtils.Mapper<String,double[]> DOUBLE_PRINTER
Use like this: System.out.println(map(List object, DOUBLE_PRINTER)) Unfortunately, a generic version for all primitive-arrays is not possible...


AND

public static final CollectionUtils.Reducer<Boolean> AND
Constructor Detail

CollectionUtils

public CollectionUtils()
Method Detail

getStringJoiner

public static CollectionUtils.Reducer<String> getStringJoiner(String delim)
Parameters:
delim -
Returns:
a reducer that joins a string list

getArrayPrinter

public static <T> CollectionUtils.Mapper<String,T[]> getArrayPrinter()
Type Parameters:
T -
Returns:
a mapper to output an object array

getInstanceofMapper

public static <T> CollectionUtils.Mapper<Boolean,T> getInstanceofMapper(Class<?> c)
Type Parameters:
T -
Parameters:
c -
Returns:
a list of booleans, true if u instanceof c

removeDuplicates

public static <T> List<T> removeDuplicates(List<T> list)
Keeps and returns the original list.

Type Parameters:
T -
Parameters:
list -
Returns:
removes any duplicates in the list. Keeps the first occurrence of duplicates.

addAllUncontained

public static <T> void addAllUncontained(Collection<T> col1,
                                         Collection<T> col2)
Adds all elements from col2 to col1, if they're not already contained. Keep in mind that this will be expensive for lists.

Type Parameters:
T -
Parameters:
col1 -
col2 -

unzipPair

public static <T,U> Pair<ArrayList<T>,ArrayList<U>> unzipPair(Collection<Pair<T,U>> col)
This indirection is useful in case you use ComparablePairs instead of normal ones and want to unzip.

Type Parameters:
T -
U -
Parameters:
col -
Returns:
two separate lists

unzip

public static <T,U,V> Triple<ArrayList<T>,ArrayList<U>,ArrayList<V>> unzip(Collection<Triple<T,U,V>> col)
Type Parameters:
T -
U -
V -
Parameters:
col -
Returns:
three separate lists

unzip

public static <T,U> Pair<ArrayList<T>,ArrayList<U>> unzip(Map<T,U> map)
Type Parameters:
T -
U -
Parameters:
map -
Returns:
two separate lists

map

public static <T,U> LinkedList<T> map(U[] col,
                                      CollectionUtils.Mapper<T,U> mapper)
Wraps a for loop and the creation of a new list.

Type Parameters:
T -
U -
Parameters:
col -
mapper -
Returns:
a list where the mapper has been applied to each element in the map

map

public static <T,U> LinkedList<T> map(Collection<U> col,
                                      CollectionUtils.Mapper<T,U> mapper)
Wraps a for loop and the creation of a new list.

Type Parameters:
T -
U -
Parameters:
col -
mapper -
Returns:
a list where the mapper has been applied to each element in the map

reduce

public static <T> T reduce(T identity,
                           Collection<T> col,
                           CollectionUtils.Reducer<T> reducer)
Type Parameters:
T -
Parameters:
identity -
col -
reducer -
Returns:
the folded value

clearNulls

public static <T> ArrayList<T> clearNulls(Collection<T> col)
Type Parameters:
T -
Parameters:
col -
Returns:
a list without null elements

clearNulls

public static <T> void clearNulls(List<T> col)
Removed null elements in-place.

Type Parameters:
T -
Parameters:
col -

filter

public static <T> List<T> filter(Collection<T> col,
                                 CollectionUtils.Predicate<T> pred)
Type Parameters:
T -
Parameters:
col -
pred -
Returns:
a list of values which the predicate applies to


Copyright © 2011. All Rights Reserved.