org.littleshoot.util
Class CollectionUtilsImpl

java.lang.Object
  extended by org.littleshoot.util.CollectionUtilsImpl
All Implemented Interfaces:
CollectionUtils

public class CollectionUtilsImpl
extends Object
implements CollectionUtils

An implementation of the collection utilities interface.


Constructor Summary
CollectionUtilsImpl()
           
 
Method Summary
<T1,T2> T2
foldl(F2<T2,T1,T2> f, T2 initial, Collection<? extends T1> collection)
          Left-associative fold function.
<T> void
forAllDo(Collection<? extends T> collection, Closure<T> closure)
          Executes the specified Closure on all elements of the Collection.
<T> void
forAllDoSynchronized(Collection<? extends T> collection, Closure<T> closure)
          Executes the specified Closure on all elements of the Collection.
<T1,T2> Collection<T2>
map(Collection<? extends T1> collection, F1<T1,T2> f)
          Maps the items in a collection using a given function.
<T> boolean
matchesAll(Collection<? extends T> collection, Predicate<T> pred)
          Checks to see if all of the elements of the given Collection matches the predicate.
<T> boolean
matchesAny(Collection<? extends T> collection, Predicate<T> pred)
          Checks to see if any of the elements of the given Collection matches the predicate.
<T> Collection<T>
select(Collection<? extends T> collection, Predicate<T> predicate)
          Returns a collection of all of the elements in a given collection that satisfy a given predicate.
<T> void
select(Collection<? extends T> collection, Predicate<T> predicate, Collection<T> result)
          Adds all of the elements in a given collection that satisfy a given predicate to another given collection.
<T> T
selectFirst(Collection<? extends T> collection, Predicate<T> pred)
          Selects the first element matching the desired criteria, or null if no matching element exists.
<T> T
selectFirstSynchronized(Collection<? extends T> collection, Predicate<T> pred)
          Selects the first element matching the desired criteria, or null if no matching element exists.
 long sum(Collection<Long> c)
          Returns the sum of a collection of long integers.
<T> boolean
synchronizedMatchesAny(Collection<? extends T> elements, Predicate<T> pred)
          Checks to see if any of the elements of the given Collection matches the predicate.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CollectionUtilsImpl

public CollectionUtilsImpl()
Method Detail

select

public <T> void select(Collection<? extends T> collection,
                       Predicate<T> predicate,
                       Collection<T> result)
Adds all of the elements in a given collection that satisfy a given predicate to another given collection.

Specified by:
select in interface CollectionUtils
Type Parameters:
T - The type of objects in the collection and expected by the predicate.
Parameters:
collection - The collection.
predicate - The predicate used to evaluate each element.
result - The collection to which to add the elements that satisfy the predicate.

select

public <T> Collection<T> select(Collection<? extends T> collection,
                                Predicate<T> predicate)
Returns a collection of all of the elements in a given collection that satisfy a given predicate.

Specified by:
select in interface CollectionUtils
Type Parameters:
T - The type of objects in the collection and expected by the predicate.
Parameters:
collection - The collection.
predicate - The predicate used to evaluate each element.
Returns:
A collection of all of the elements in a given collection that satisfy a given predicate.

forAllDo

public <T> void forAllDo(Collection<? extends T> collection,
                         Closure<T> closure)
Executes the specified Closure on all elements of the Collection.

Specified by:
forAllDo in interface CollectionUtils
Type Parameters:
T - The type of elements in the Collection.
Parameters:
collection - The Collection with elements to perform the closure on.
closure - The Closure to execute on each element.

forAllDoSynchronized

public <T> void forAllDoSynchronized(Collection<? extends T> collection,
                                     Closure<T> closure)
Executes the specified Closure on all elements of the Collection. Also synchronizes on the Collection.

Specified by:
forAllDoSynchronized in interface CollectionUtils
Type Parameters:
T - The type of elements in the Collection.
Parameters:
collection - The Collection with elements to perform the closure on.
closure - The Closure to execute on each element.

map

public <T1,T2> Collection<T2> map(Collection<? extends T1> collection,
                                  F1<T1,T2> f)
Maps the items in a collection using a given function.

Specified by:
map in interface CollectionUtils
Type Parameters:
T1 - The type of elements in the original collection.
T2 - The type to which we are mapping.
Parameters:
collection - The collection to map.
f - The mapping function.
Returns:
The mapped collection.

foldl

public <T1,T2> T2 foldl(F2<T2,T1,T2> f,
                        T2 initial,
                        Collection<? extends T1> collection)
Left-associative fold function.

Specified by:
foldl in interface CollectionUtils
Type Parameters:
T1 - The type of the collection over which to fold.
T2 - The type returned by the folding function.
Parameters:
f - The folding function.
initial - The initial value for folding.
collection - The collection.
Returns:
The result of the folding.

sum

public long sum(Collection<Long> c)
Returns the sum of a collection of long integers.

Specified by:
sum in interface CollectionUtils
Parameters:
c - The collection of long integers.
Returns:
The sum of the collection of long integers.

matchesAny

public <T> boolean matchesAny(Collection<? extends T> collection,
                              Predicate<T> pred)
Description copied from interface: CollectionUtils
Checks to see if any of the elements of the given Collection matches the predicate.

Specified by:
matchesAny in interface CollectionUtils
Type Parameters:
T - The type of elements in the Collection.
Parameters:
collection - The Collection to check.
pred - The Predicate determining if an element matches.
Returns:
true if there's any element in the Collection matching the desired criteria.

matchesAll

public <T> boolean matchesAll(Collection<? extends T> collection,
                              Predicate<T> pred)
Description copied from interface: CollectionUtils
Checks to see if all of the elements of the given Collection matches the predicate.

Specified by:
matchesAll in interface CollectionUtils
Type Parameters:
T - The type of elements in the Collection.
Parameters:
collection - The Collection to check.
pred - The Predicate determining if all elements match.
Returns:
true if all elements in the Collection match the desired criteria.

synchronizedMatchesAny

public <T> boolean synchronizedMatchesAny(Collection<? extends T> elements,
                                          Predicate<T> pred)
Description copied from interface: CollectionUtils
Checks to see if any of the elements of the given Collection matches the predicate. Synchronizes on the Collection.

Specified by:
synchronizedMatchesAny in interface CollectionUtils
Type Parameters:
T - The type of elements in the Collection.
Parameters:
elements - The Collection to check.
pred - The Predicate determining if an element matches.
Returns:
true if there's any element in the Collection matching the desired criteria.

selectFirst

public <T> T selectFirst(Collection<? extends T> collection,
                         Predicate<T> pred)
Description copied from interface: CollectionUtils
Selects the first element matching the desired criteria, or null if no matching element exists. Synchronizes on the Collection.

Specified by:
selectFirst in interface CollectionUtils
Type Parameters:
T - The type of elements in the Collection.
Parameters:
collection - The Collection of elements to search.
pred - The Predicate determining a match.
Returns:
The first matching element, or null if no such element exists.

selectFirstSynchronized

public <T> T selectFirstSynchronized(Collection<? extends T> collection,
                                     Predicate<T> pred)
Description copied from interface: CollectionUtils
Selects the first element matching the desired criteria, or null if no matching element exists. Synchronizes on the Collection.

Specified by:
selectFirstSynchronized in interface CollectionUtils
Type Parameters:
T - The type of elements in the Collection.
Parameters:
collection - The Collection of elements to search.
pred - The Predicate determining a match.
Returns:
The first matching element, or null if no such element exists.


Copyright © 2011-2013 LittleShoot. All Rights Reserved.