com.neoworks.util
Class CollectionUtils

java.lang.Object
  |
  +--com.neoworks.util.CollectionUtils

public class CollectionUtils
extends java.lang.Object

Static methods for working with Collections and function blocks.

Version:
$Revision: 1.4 $
Author:
Nigel Atkinson nigel@neoworks.com
, Nicko Cadell nicko@neoworks.com

Method Summary
static java.util.Collection arrayCollection(java.lang.Object[] array, java.lang.Class collectionType)
          Return a Collection from an array
static java.util.Collection collect(java.util.Collection collection, OneBlock block)
          return a collection containing only the elements from the collection passed which generated the result true when passed to the block.
static boolean detect(java.util.Collection collection, OneBlock block)
          Detect if a collection meets some test.
static java.util.Collection Enumeration2Collection(java.util.Enumeration e)
          Put an Enumeration into something useful (a Collection, implemented as an ArrayList)
static java.util.Collection Enumeration2Collection(java.util.Enumeration e, java.util.Collection c)
          Put an Enumeration into a Collection of your choice
static java.lang.Object findFirst(java.util.Collection collection, OneBlock block)
          Find the first value in a collection that meets some test.
static java.lang.Object fold(java.util.Collection collection, java.lang.Object defaultValue, TwoBlock block)
          This provides a fold function for collections.
static java.util.Set getIntersection(java.util.Collection a, java.util.Collection b)
          Get the intersection of two sets of homogeneous objects
static java.util.Collection map(java.util.Collection collection, OneBlock block)
          Return a collection which is each element in the original collection mapped through the one argument function in the block The one argument block must take an element from the collection as an argument and must return an Object type.
static java.util.Collection quickSort(java.util.Collection collection, TwoBlock block)
          An implementation of Quicksort using medians of 3 for partitions.
static void quickSort(java.lang.Object[] s, int lo, int hi, TwoBlock block)
          An implementation of Quicksort using medians of 3 for partitions.
static boolean reject(java.util.Collection collection, OneBlock block)
          Detect if a collection fails some test.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

Enumeration2Collection

public static java.util.Collection Enumeration2Collection(java.util.Enumeration e)
Put an Enumeration into something useful (a Collection, implemented as an ArrayList)

Parameters:
e - The Enumeration
Returns:
A Collection containing the contents of the Enumeration

Enumeration2Collection

public static java.util.Collection Enumeration2Collection(java.util.Enumeration e,
                                                          java.util.Collection c)
Put an Enumeration into a Collection of your choice

Parameters:
e - The Enumeration
c - The Collection to put the contents of the Enumeration into
Returns:
A Collection containing the contents of the Enumeration

getIntersection

public static java.util.Set getIntersection(java.util.Collection a,
                                            java.util.Collection b)
Get the intersection of two sets of homogeneous objects

Parameters:
a - The first Collection
b - The second Collection
Returns:
Set A set containing the intersection of the Collections

collect

public static java.util.Collection collect(java.util.Collection collection,
                                           OneBlock block)
return a collection containing only the elements from the collection passed which generated the result true when passed to the block. The one argument block must take an element from the collection as an arguemnt and must return a Boolean type.

Parameters:
collection - The input data collection
block - The predicate block
Returns:
A Collection containg only the input values accepted by the predicate block

fold

public static java.lang.Object fold(java.util.Collection collection,
                                    java.lang.Object defaultValue,
                                    TwoBlock block)
This provides a fold function for collections. It requires a two argument block which takes an Object as its first argument and an element from the data collection as the second. It must return an Object which is passed to the next block with the next element.

Parameters:
collection - The input data collection
defaultValue - The initial value to pass to the first call to the block
block - The two argument block that folds the input data
Returns:
the Collection containing the result of the fold

detect

public static boolean detect(java.util.Collection collection,
                             OneBlock block)
Detect if a collection meets some test. Each element in the collection is passed to the block. If the block returns true for any element then this function returns true. This will return as soon a positive result is found. Otherwise a result of false is returned. The one argument block must take an element from the collection as an arguemnt and must return a Boolean type.

Parameters:
collection - The input data collection
block - The predicate block
Returns:
true if any item in the input collection passes the predicate block test

reject

public static boolean reject(java.util.Collection collection,
                             OneBlock block)
Detect if a collection fails some test. Each element in the collection is passed to the block. If the block returns false for any element then this function returns false. This will return as soon a negative result is found. Otherwise a result of true is returned. The one argument block must take an element from the collection as an arguemnt and must return a Boolean type.

Parameters:
collection - The input data collection
block - The predicate block
Returns:
false if any item in the input collection fails the predicate bolck test

findFirst

public static java.lang.Object findFirst(java.util.Collection collection,
                                         OneBlock block)
Find the first value in a collection that meets some test. Each element in the collection is passed to the block. If the block returns true for the element then this function returns the element. This will return as soon a positive result is found. Otherwise a result of null is returned. The one argument block must take an element from the collection as an arguemnt and must return a Boolean type.

Parameters:
collection - The input data collection
block - The predicate block
Returns:
The first item from the input collection that passes the predicate block test

map

public static java.util.Collection map(java.util.Collection collection,
                                       OneBlock block)
Return a collection which is each element in the original collection mapped through the one argument function in the block The one argument block must take an element from the collection as an argument and must return an Object type.

Parameters:
collection - The input data collection
block - The remapping block
Returns:
A collection of the results of remapping the input data

arrayCollection

public static java.util.Collection arrayCollection(java.lang.Object[] array,
                                                   java.lang.Class collectionType)
Return a Collection from an array

Parameters:
array - The input data array
collectionType - The type of Collection sub class to create
Returns:
A Collection object holding the data from the input array

quickSort

public static java.util.Collection quickSort(java.util.Collection collection,
                                             TwoBlock block)
An implementation of Quicksort using medians of 3 for partitions. It is public and static so it can be used to sort any plain arrays. Time complexity: O(n log n). Example: to sort a string into lexical order (uses inner class)
 Collection data = ...  ;
 Collection sorted = Utils.quickSort(data ,new TwoBlock() {
    public Object value(Object fst, Object snd)
    {return new Integer(((String)fst).compareTo((String)snd));}});
 

Parameters:
block - A two argument block returning an Integer object that compares the two arguments. It should have the same symantics as the java.lang.Comparable.compareTo() method.
collection - the collection to sort
Returns:
The sorted collection
See Also:
TwoBlock

quickSort

public static void quickSort(java.lang.Object[] s,
                             int lo,
                             int hi,
                             TwoBlock block)
An implementation of Quicksort using medians of 3 for partitions. It is public and static so it can be used to sort any plain arrays. Time complexity: O(n log n). Example: to sort a string into lexical order (uses inner class)
 String s[] = {"hello","sports","fans"};
 Utils.quickSort(s,0,s.length-1,new TwoBlock() {
     public Object value(Object fst, Object snd)
     {return new Integer(((String)fst).compareTo((String)snd));}});
 

Parameters:
s - the array to sort
lo - the least index to sort from
hi - the greatest index
block - A two argument block returning an Integer object that compares the two arguments. It should have the same symantics as the java.lang.Comparable.compareTo() method.
See Also:
TwoBlock