org.omnaest.utils.structure.collection
Class CollectionUtils

java.lang.Object
  extended by org.omnaest.utils.structure.collection.CollectionUtils

public class CollectionUtils
extends Object


Nested Class Summary
static interface CollectionUtils.CollectionConverter<FROM,TO>
          Transformer interface for transforming whole Collections into a single object
static class CollectionUtils.CollectionTransformerToString<FROM>
          CollectionUtils.CollectionConverter which produces a String from a Collection
 
Constructor Summary
CollectionUtils()
           
 
Method Summary
static
<FROM,TO> Collection<TO>
adapter(Collection<FROM> collection, ElementBidirectionalConverter<FROM,TO> elementBidirectionalConverter)
          Returns a new CollectionToCollectionAdapter instance
static void addAll(Collection<Integer> integerCollection, int[] intArray)
          Adds an array of integer values to a collection of Integer
static void addAll(Collection<Long> longCollection, long[] intArray)
          Adds an array of long values to a collection of Long
static
<E> Collection<E>
composite(Collection<Collection<E>> collectionOfCollections)
          Returns a new CollectionComposite instance for the given Collections
static
<E> Collection<E>
composite(Collection<E>... collections)
          Returns a new CollectionComposite instance for the given Collections
static
<E> boolean
containsObjectIdentity(Collection<E> collection, Object object)
          Returns true if the given Collection contains the given object.
static
<FROM,TO> TO
convert(Collection<FROM> collection, CollectionUtils.CollectionConverter<FROM,TO> collectionConverter)
          Converts a given Collection instance from one generic type into a single value using a CollectionUtils.CollectionConverter
static
<TO,FROM> Collection<TO>
convertCollection(Collection<FROM> collectionFrom, ElementConverter<FROM,TO> elementConverter)
          Converts a given Collection into another Collection with other element types using an ElementConverter.
static
<TO,FROM> Collection<TO>
convertCollectionExcludingNullElements(Collection<FROM> collectionFrom, ElementConverter<FROM,TO> elementConverter)
          Converts a given Collection into another Collection with other element types, whereby all elements which convert to null will not be inserted into the target Collection.
static
<E> void
copyIntoCollectionFrom(Collection<E> collection, Iterable<E> iterable)
          Copies all elements from the given Iterable into the given Collection
static
<E> void
copyIntoCollectionFrom(Collection<E> collection, Iterable<E> iterable, int maxNumberOfElements)
          Copies all elements from the given Iterable into the given Collection
static
<E> void
copyIntoCollectionFrom(Collection<E> collection, Iterator<E> iterator)
          Copies all elements from the given Iterator into the given Collection
This traverses the Iterator
static
<E> void
copyIntoCollectionFrom(Collection<E> collection, Iterator<E> iterator, int maxNumberOfElements)
          Copies all elements from the given Iterator into the given Collection.This traverses the Iterator only as far as necessary.
static
<E> void
drainTo(Iterable<E> iterable, Collection<E> collection)
          Drains the elements of the given Iterable to the given Collection
static
<E> void
drainTo(Iterable<E> iterable, Collection<E> collection, int maxNumberOfElements)
          Drains the elements of the given Iterable by the given maximum number of elements to the given Collection.
static boolean equals(Iterable<?> collection1, Iterable<?> collection2)
          Returns true if the elements of the two given Collections are equal and have the same order
static boolean equalsUnordered(Collection<?> collection1, Collection<?> collection2)
          Tests, if the two collection have the same elements.
static
<E> int
hashCode(Collection<E> collection)
          Calculates the hash code for a given collection including the order of elements
static
<E> int
hashCodeUnordered(Collection<E> collection)
          Calculates the hash code for a given collection not including the order of elements.
static
<E> int
indexOfObjectIdentity(Collection<E> collection, Object object)
          Returns the index position of the first occurring object within the Collection.
static
<E> int
lastIndexOfObjectIdentity(Collection<E> collection, Object object)
          Returns the index position of the last occurring object within the Collection.
static
<E> Collection<E>
locked(Collection<E> collection, Lock lock)
          Returns a view of the given Collection which uses the given Lock to synchronize all of its methods
static
<E> Collection<E>
lockedByReentrantLock(Collection<E> collection)
          Returns a view of the given Collection which uses a new ReentrantLock instance to synchronize all of its methods
static
<E> Collection<E>
mergeAll(Collection<E>... collections)
          Merges all elements of the given Collection instances into one single Collection instance which keeps the order of the elements.
static double sumOfCollectionDouble(Iterable<Double> doubleIterable)
          Returns the sum of the values within an Double Iterable.
static int sumOfCollectionInteger(Iterable<Integer> integerIterable)
          Returns the sum of the values within an Integer Iterable.
static
<E> E[]
toArray(Collection<E> collection, Class<? extends E> clazz)
          Converts a given Collection into a typed array
static int[] toArrayInt(Collection<Integer> integerCollection)
          Returns the values of a Integer Collection as array.
static
<K,V,E> Map<K,V>
toMap(Iterable<E> iterable, ElementConverterElementToMapEntry<E,K,V> elementToMapEntryTransformer)
          Transforms a given Iterable into a Map using a LinkedHashMap which keeps the order of the List.
static
<E> String
toString(Collection<E> collection, ElementConverter<E,String> elementConverter, com.google.common.base.Joiner joiner)
           
static
<E> String
toString(Collection<E> collection, com.google.common.base.Joiner joiner)
           
static
<E> String
toString(Collection<E> collection, com.google.common.base.Joiner joiner, String prefix, String suffix)
           
static String toString(Iterable<?> iterable)
          Returns a String representation of the given Iterable using String.valueOf(boolean)
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CollectionUtils

public CollectionUtils()
Method Detail

addAll

public static void addAll(Collection<Integer> integerCollection,
                          int[] intArray)
Adds an array of integer values to a collection of Integer

Parameters:
integerCollection -
intArray -

addAll

public static void addAll(Collection<Long> longCollection,
                          long[] intArray)
Adds an array of long values to a collection of Long

Parameters:
longCollection -
intArray -

containsObjectIdentity

public static <E> boolean containsObjectIdentity(Collection<E> collection,
                                                 Object object)
Returns true if the given Collection contains the given object. Contains uses the "object == element" instead of the "equals" method to determine the object identity.

Parameters:
collection -
object -
Returns:

convert

public static <FROM,TO> TO convert(Collection<FROM> collection,
                                   CollectionUtils.CollectionConverter<FROM,TO> collectionConverter)
Converts a given Collection instance from one generic type into a single value using a CollectionUtils.CollectionConverter

Parameters:
collection -
collectionConverter -
See Also:
CollectionUtils.CollectionConverter

convertCollection

public static <TO,FROM> Collection<TO> convertCollection(Collection<FROM> collectionFrom,
                                                         ElementConverter<FROM,TO> elementConverter)
Converts a given Collection into another Collection with other element types using an ElementConverter.

Parameters:
collectionFrom -
elementConverter -
Returns:
See Also:
ElementConverter

convertCollectionExcludingNullElements

public static <TO,FROM> Collection<TO> convertCollectionExcludingNullElements(Collection<FROM> collectionFrom,
                                                                              ElementConverter<FROM,TO> elementConverter)
Converts a given Collection into another Collection with other element types, whereby all elements which convert to null will not be inserted into the target Collection.

Parameters:
collectionFrom -
elementConverter -
Returns:
See Also:
ElementConverter

equals

public static boolean equals(Iterable<?> collection1,
                             Iterable<?> collection2)
Returns true if the elements of the two given Collections are equal and have the same order

Parameters:
collection1 -
collection2 -
Returns:

equalsUnordered

public static boolean equalsUnordered(Collection<?> collection1,
                                      Collection<?> collection2)
Tests, if the two collection have the same elements.

Parameters:
collection1 -
collection2 -
Returns:

hashCode

public static <E> int hashCode(Collection<E> collection)
Calculates the hash code for a given collection including the order of elements

Parameters:
collection -
Returns:

hashCodeUnordered

public static <E> int hashCodeUnordered(Collection<E> collection)
Calculates the hash code for a given collection not including the order of elements. This can be used for Set implementations e.g.

Parameters:
collection -
Returns:

indexOfObjectIdentity

public static <E> int indexOfObjectIdentity(Collection<E> collection,
                                            Object object)
Returns the index position of the first occurring object within the Collection. Comparisons uses the "object == element" instead of the "equals" method to determine the object identity.

Parameters:
collection -
object -
Returns:
index position of the first matching element;-1 if no element is matching

lastIndexOfObjectIdentity

public static <E> int lastIndexOfObjectIdentity(Collection<E> collection,
                                                Object object)
Returns the index position of the last occurring object within the Collection. Comparisons uses the "object == element" instead of the "equals" method to determine the object identity.

Parameters:
collection -
object -
Returns:
index position of the first matching element;-1 if no element is matching

mergeAll

public static <E> Collection<E> mergeAll(Collection<E>... collections)
Merges all elements of the given Collection instances into one single Collection instance which keeps the order of the elements.

Type Parameters:
E -
Parameters:
collections -
Returns:

toString

public static String toString(Iterable<?> iterable)
Returns a String representation of the given Iterable using String.valueOf(boolean)

Parameters:
iterable -
Returns:

toString

public static <E> String toString(Collection<E> collection,
                                  com.google.common.base.Joiner joiner,
                                  String prefix,
                                  String suffix)
Parameters:
collection -
joiner - Joiner
prefix -
suffix -
Returns:

toString

public static <E> String toString(Collection<E> collection,
                                  com.google.common.base.Joiner joiner)
Parameters:
collection -
joiner - Joiner
Returns:

toString

public static <E> String toString(Collection<E> collection,
                                  ElementConverter<E,String> elementConverter,
                                  com.google.common.base.Joiner joiner)

sumOfCollectionDouble

public static double sumOfCollectionDouble(Iterable<Double> doubleIterable)
Returns the sum of the values within an Double Iterable.

Parameters:
doubleIterable -
Returns:

sumOfCollectionInteger

public static int sumOfCollectionInteger(Iterable<Integer> integerIterable)
Returns the sum of the values within an Integer Iterable.

Parameters:
integerIterable -
Returns:

toArray

public static <E> E[] toArray(Collection<E> collection,
                              Class<? extends E> clazz)
Converts a given Collection into a typed array

Parameters:
collection -
clazz -
Returns:

toArrayInt

public static int[] toArrayInt(Collection<Integer> integerCollection)
Returns the values of a Integer Collection as array.

Parameters:
integerCollection -
Returns:

toMap

public static <K,V,E> Map<K,V> toMap(Iterable<E> iterable,
                                     ElementConverterElementToMapEntry<E,K,V> elementToMapEntryTransformer)
Transforms a given Iterable into a Map using a LinkedHashMap which keeps the order of the List. Returns an empty Map for a null value as Iterable. Null values within the Iterable will be excluded from the map, if the respective ElementConverterElementToMapEntry.convert(Object) returns null.

Parameters:
iterable -
elementToMapEntryTransformer -
Returns:
See Also:
ElementConverterElementToMapEntry

locked

public static <E> Collection<E> locked(Collection<E> collection,
                                       Lock lock)
Returns a view of the given Collection which uses the given Lock to synchronize all of its methods

Parameters:
collection -
lock -
Returns:

lockedByReentrantLock

public static <E> Collection<E> lockedByReentrantLock(Collection<E> collection)
Returns a view of the given Collection which uses a new ReentrantLock instance to synchronize all of its methods

Parameters:
collection - F@return

drainTo

public static <E> void drainTo(Iterable<E> iterable,
                               Collection<E> collection)
Drains the elements of the given Iterable to the given Collection

Parameters:
iterable -
collection -

drainTo

public static <E> void drainTo(Iterable<E> iterable,
                               Collection<E> collection,
                               int maxNumberOfElements)
Drains the elements of the given Iterable by the given maximum number of elements to the given Collection.

Parameters:
iterable -
collection -
maxNumberOfElements -

copyIntoCollectionFrom

public static <E> void copyIntoCollectionFrom(Collection<E> collection,
                                              Iterable<E> iterable)
Copies all elements from the given Iterable into the given Collection

Parameters:
collection -
iterable -

copyIntoCollectionFrom

public static <E> void copyIntoCollectionFrom(Collection<E> collection,
                                              Iterable<E> iterable,
                                              int maxNumberOfElements)
Copies all elements from the given Iterable into the given Collection

Parameters:
collection -
iterable -
maxNumberOfElements -

copyIntoCollectionFrom

public static <E> void copyIntoCollectionFrom(Collection<E> collection,
                                              Iterator<E> iterator)
Copies all elements from the given Iterator into the given Collection
This traverses the Iterator

Parameters:
collection -
iterator -

copyIntoCollectionFrom

public static <E> void copyIntoCollectionFrom(Collection<E> collection,
                                              Iterator<E> iterator,
                                              int maxNumberOfElements)
Copies all elements from the given Iterator into the given Collection.This traverses the Iterator only as far as necessary.

Parameters:
collection -
iterator -
maxNumberOfElements -

adapter

public static <FROM,TO> Collection<TO> adapter(Collection<FROM> collection,
                                               ElementBidirectionalConverter<FROM,TO> elementBidirectionalConverter)
Returns a new CollectionToCollectionAdapter instance

Parameters:
collection - Collection
elementBidirectionalConverter - ElementBidirectionalConverter
Returns:
new CollectionToCollectionAdapter instance

composite

public static <E> Collection<E> composite(Collection<Collection<E>> collectionOfCollections)
Returns a new CollectionComposite instance for the given Collections

Parameters:
collectionOfCollections -
Returns:

composite

public static <E> Collection<E> composite(Collection<E>... collections)
Returns a new CollectionComposite instance for the given Collections

Parameters:
collections -
Returns:


Copyright © 2013. All Rights Reserved.