com.ge.research.utils
Class CollectionUtils

java.lang.Object
  extended bycom.ge.research.utils.CollectionUtils
Direct Known Subclasses:
ListUtils

public class CollectionUtils
extends java.lang.Object

A utility class with common, useful methods for dealing with Collections.

Version:
$Revision: 1.1 $ $Date: 2006/08/11 00:17:39 $
Author:
garbiras
Created on:
Aug 20, 2002

Constructor Summary
CollectionUtils()
           
 
Method Summary
static boolean allElementsOfType(java.util.Collection c, java.lang.Class type)
          Checks each entry in the Collection to see if the entry is assignable to the argument class type.
static void moveItem(java.util.Collection c, int moveFrom, int moveTo)
          Moves the element in the collection c from the position moveFrom to the position moveTo, and preserves the order of the collection.
static void moveItem(java.util.Collection c, java.lang.Object element, int moveTo)
          Moves the element in the collection c from its current position to the position moveTo, and preserves the order of the collection.
static void moveItemAfter(java.util.Collection c, java.lang.Object elementToMove, java.lang.Object referenceElement)
          Moves the elementToMove and places it directly after the referenceElement in the collection c, and preserves the order of the collection.
static void moveItemBefore(java.util.Collection c, java.lang.Object elementToMove, java.lang.Object referenceElement)
          Moves the elementToMove and places it directly before the referenceElement in the collection c, and preserves the order of the collection.
static void moveItemToEnd(java.util.Collection c, int moveFrom)
          Moves the element in the collection c currently at position moveFrom to the end of the collection, and preserves the order of the collection.
static void moveItemToEnd(java.util.Collection c, java.lang.Object element)
          Moves the element in the collection c to the end of the collection, and preserves the order of the collection.
static void moveItemToStart(java.util.Collection c, int moveFrom)
          Moves the element in the collection c currently at position moveFrom to the beginning of the collection, and preserves the order of the collection.
static void moveItemToStart(java.util.Collection c, java.lang.Object element)
          Moves the element in the collection c to the beginning of the collection, and preserves the order of the collection.
static void orderItems(java.util.Collection c, boolean orderAscending)
          Orders the elements of the collection based upon the collection elements object types.
static void removeDuplicates(java.util.Collection c)
          Eliminates duplicate elements from the colection, and only retains a single instance of each unique element.
static void reverseOrder(java.util.Collection c)
          Reverses the order of the elements of the Collection.
static void swapItems(java.util.Collection c, java.lang.Object elementOne, java.lang.Object elementTwo)
          Moves two elements of the collection such that each element occupies the others original position.
 
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

allElementsOfType

public static boolean allElementsOfType(java.util.Collection c,
                                        java.lang.Class type)
Checks each entry in the Collection to see if the entry is assignable to the argument class type.

Parameters:
c - the datastructure that extends Collection and contains the elements of interest
type - the Class type to check each entry in the Collection against, to see if the entries are an instance of that class or one of its subclasses.
Returns:
true if all entries are of the type specified, or if the Collection is null or empty; false otherwise

moveItem

public static void moveItem(java.util.Collection c,
                            int moveFrom,
                            int moveTo)
Moves the element in the collection c from the position moveFrom to the position moveTo, and preserves the order of the collection.

Parameters:
c - the datastructure that extends Collection and contains the elements of interest
moveFrom - the index within the collection the element to move currently resides at
moveTo - the index within the collection the element is to be moved to

moveItem

public static void moveItem(java.util.Collection c,
                            java.lang.Object element,
                            int moveTo)
Moves the element in the collection c from its current position to the position moveTo, and preserves the order of the collection.

Parameters:
c - the datastructure that extends Collection and contains the elements of interest
element - the collection element to move
moveTo - the index within the collection the element is to be moved to

moveItemAfter

public static void moveItemAfter(java.util.Collection c,
                                 java.lang.Object elementToMove,
                                 java.lang.Object referenceElement)
Moves the elementToMove and places it directly after the referenceElement in the collection c, and preserves the order of the collection.

Parameters:
c - the datastructure that extends Collection and contains the elements of interest
elementToMove - the element to move
referenceElement - the element that marks the position within the collection that the moving element should immediately follow

moveItemToStart

public static void moveItemToStart(java.util.Collection c,
                                   int moveFrom)
Moves the element in the collection c currently at position moveFrom to the beginning of the collection, and preserves the order of the collection.

Parameters:
c - the datastructure that extends Collection and contains the elements of interest
moveFrom - the index within the collection the element to move currently resides at
See Also:
moveItem(java.util.Collection, int, int)

moveItemBefore

public static void moveItemBefore(java.util.Collection c,
                                  java.lang.Object elementToMove,
                                  java.lang.Object referenceElement)
Moves the elementToMove and places it directly before the referenceElement in the collection c, and preserves the order of the collection.

Parameters:
c - the datastructure that extends Collection and contains the elements of interest
elementToMove - the element to move
referenceElement - the element that marks the position within the collection that the moving element should immediately precede

moveItemToEnd

public static void moveItemToEnd(java.util.Collection c,
                                 int moveFrom)
Moves the element in the collection c currently at position moveFrom to the end of the collection, and preserves the order of the collection.

Parameters:
c - the datastructure that extends Collection and contains the elements of interest
moveFrom - the index within the collection the element to move currently resides at
See Also:
moveItem(java.util.Collection, int, int)

moveItemToEnd

public static void moveItemToEnd(java.util.Collection c,
                                 java.lang.Object element)
Moves the element in the collection c to the end of the collection, and preserves the order of the collection.

Parameters:
c - the datastructure that extends Collection and contains the elements of interest
element - the element to move to the end of the collection
See Also:
moveItem(java.util.Collection, java.lang.Object, int)

moveItemToStart

public static void moveItemToStart(java.util.Collection c,
                                   java.lang.Object element)
Moves the element in the collection c to the beginning of the collection, and preserves the order of the collection.

Parameters:
c - the datastructure that extends Collection and contains the elements of interest
element - the element to move to the beginning of the collection
See Also:
moveItem(java.util.Collection, java.lang.Object, int)

orderItems

public static void orderItems(java.util.Collection c,
                              boolean orderAscending)
Orders the elements of the collection based upon the collection elements object types. The collection can be ordered ascending or descending depending on the value of the orderAscending parameter. Each element of the collection must be of the same type and must implement the Comparable interface. If the collection type is determined to be String, then an attempt is made to see if the Strings represent numbers, and if so the collection is sorted by the numerical values, otherwise by the lexigraphical values.

Parameters:
c - the datastructure that extends Collection and contains the elements of interest
orderAscending - true if the collection should be sorting in ASCending fashion; false if the collection should be sorted in DESCending fasion

removeDuplicates

public static void removeDuplicates(java.util.Collection c)
Eliminates duplicate elements from the colection, and only retains a single instance of each unique element. The element uniqueness is determined by using the Object.equals(java.lang.Object) method as the comparison operator.

Parameters:
c - the datastructure that extends Collection and contains the elements of interest

reverseOrder

public static void reverseOrder(java.util.Collection c)
Reverses the order of the elements of the Collection.

Parameters:
c - the datastructure that extends Collection and contains the elements of interest

swapItems

public static void swapItems(java.util.Collection c,
                             java.lang.Object elementOne,
                             java.lang.Object elementTwo)
Moves two elements of the collection such that each element occupies the others original position.

Parameters:
c - the datastructure that extends Collection and contains the elements of interest
elementOne - the first element to move. Its current position will be occupied by elementTwo after the swap is performed
elementTwo - the second element to move Its current position will be occupied by elementOne after the swap is performed