org.openbp.common
Class CollectionUtil

java.lang.Object
  extended by org.openbp.common.CollectionUtil

public final class CollectionUtil
extends java.lang.Object

Various static utility methods for collections, lists, maps etc.

Author:
Heiko Erhardt

Field Summary
static java.util.Vector EMPTY_VECTOR
          Empty vector
 
Method Summary
static void add(java.util.List list, int index, java.lang.Object element)
          Adds a object to a list and extends the list by null values if the index is greater than the size.
static void addAll(java.util.Collection c, java.util.Enumeration en)
          Adds the elements of an enumeration to a collection.
static void addAll(java.util.Collection c, java.util.Iterator it)
          Adds the elements of an enumeration to a collection.
static void addAll(java.util.Collection c, java.lang.Object[] array)
          Adds the elements of an array to a collection.
static void addReverseList(java.util.Collection col, java.util.List result)
          Adds the elements of the given collection to the given list in reverse order.
static java.util.Collection collection(java.lang.Object o)
          This method returns a collection built based on the passed object.
static boolean containsReference(java.util.Collection c, java.lang.Object object)
          Checks if a collection contains the specified element.
static java.util.Iterator iterator(java.lang.Object o)
          This method returns an iterator built based on the passed object.
static java.util.ArrayList iteratorToArrayList(java.util.Iterator it)
          Creates ArrayList from a iterator
static void removeReference(java.util.Collection c, java.lang.Object object)
          Removes an object from a collection.
static java.lang.Object[] toArray(java.util.Collection c, java.lang.Class cls)
          Gets the objects of a collection as type-safe array (copy).
static java.lang.String[] toStringArray(java.util.Collection c)
          Gets the objects of a collection as array of strings.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

EMPTY_VECTOR

public static final java.util.Vector EMPTY_VECTOR
Empty vector

Method Detail

containsReference

public static boolean containsReference(java.util.Collection c,
                                        java.lang.Object object)
Checks if a collection contains the specified element. In contrast to Collection.contains, the comparison is done by reference ("==" instead of "equals" check), which improves performance.

Parameters:
c - Collection to search or null
object - Object to search for
Returns:
true The collection contains the specified object.
false The object has not been found in the collection

removeReference

public static void removeReference(java.util.Collection c,
                                   java.lang.Object object)
Removes an object from a collection. In contrast to Collection.contains, the comparison is done by reference ("==" instead of "equals" check), which improves performance.
Nothing happens if the collection does not contain the object.

Parameters:
c - Collection to search or null
object - Object to remove
Throws:
java.lang.UnsupportedOperationException - if the \cremove\c operation is not supported by the iterator returned by c.iterator ().

iterator

public static java.util.Iterator iterator(java.lang.Object o)
This method returns an iterator built based on the passed object. If the object is null, an empty iterator will be returned. If the object is an iterator, it is returned without conversion. If a collection or an array is passed, an iterator over the elements is returned. For any other object, an iterator containing the object as only element will be returned.

Parameters:
o - The object to be converted into an iterator
Returns:
The iterator (constructed as described above)

collection

public static java.util.Collection collection(java.lang.Object o)
This method returns a collection built based on the passed object. If the object is null, null will be returned. A collection will be passed back directly. If the object is an iterator or an array, an array list containing its contents is returned. Otherwise, an array list containing the argument will be returned.

Parameters:
o - The object to be converted into an iterator
Returns:
The iterator (constructed as described above)

addAll

public static void addAll(java.util.Collection c,
                          java.util.Iterator it)
Adds the elements of an enumeration to a collection. In order to add the elements of a collection itself, rather use the Collection.addAll method, which is optimized for this.

Parameters:
c - Collection to add the objects to
it - Iterator to add

addAll

public static void addAll(java.util.Collection c,
                          java.util.Enumeration en)
Adds the elements of an enumeration to a collection. In order to add the elements of a collection itself, rather use the Collection.addAll method, which is optimized for this.

Parameters:
c - Collection to add the objects to
en - Enumeration to add

addAll

public static void addAll(java.util.Collection c,
                          java.lang.Object[] array)
Adds the elements of an array to a collection.

Parameters:
c - Collection to add the objects to
array - Array to add

iteratorToArrayList

public static java.util.ArrayList iteratorToArrayList(java.util.Iterator it)
Creates ArrayList from a iterator

Parameters:
it - Iterator
Returns:
ArrayList to create

toArray

public static java.lang.Object[] toArray(java.util.Collection c,
                                         java.lang.Class cls)
Gets the objects of a collection as type-safe array (copy). The array elements are of the specified class. If the collection contains an element of a different class, the method will generate a ClassCastException.

Parameters:
c - Collection of objects or null
cls - The type of the array elements or null to generate a generic Object array
Returns:
The object array of type \ccls[]\c or null if the collection is empty

toStringArray

public static java.lang.String[] toStringArray(java.util.Collection c)
Gets the objects of a collection as array of strings. The toString of each object is used to generate the object string representations.

Parameters:
c - Collection of objects or null
Returns:
The string array or null if the collection is empty

addReverseList

public static void addReverseList(java.util.Collection col,
                                  java.util.List result)
Adds the elements of the given collection to the given list in reverse order.

Parameters:
col - Collection to add
result - Result list
The collection elements will be added in reverse order to the end of the list.
Due to performance reasons, we suggest that the result list should be a LinkedList.

add

public static void add(java.util.List list,
                       int index,
                       java.lang.Object element)
Adds a object to a list and extends the list by null values if the index is greater than the size.

Parameters:
list - Any list object
index - The index position within the list
element - The element to add


Copyright © 2011. All Rights Reserved.