com.multicorebsp.util
Class CollectionUtils

java.lang.Object
  extended by com.multicorebsp.util.CollectionUtils

public class CollectionUtils
extends java.lang.Object

Supports various operations on various types of collections.


Constructor Summary
CollectionUtils()
           
 
Method Summary
static
<T> void
clear(java.util.Collection<T> x)
          Empties a collection.
static
<T> boolean
contains(java.util.Collection<T> a, java.util.Collection<T> c)
          Checks if any of the elements in c are contained in a.
static boolean contains(int[] a, java.util.Collection<java.lang.Integer> c)
          Checks if any of the elements in c are contained in a.
static boolean contains(int[] i, int add_1)
          Checks if an element is contained in a collection.
static
<T> boolean
contains(T[] a, java.util.Collection<T> c)
          Checks if any of the elements in c are contained in a.
static double[] copy(double[] original)
          Copies a vector of doubles.
static
<T extends com.multicorebsp.core.CompulsaryCloneable<? extends T>>
java.util.Collection<T>
copyCollection(java.util.Collection<T> toCopy)
          Copies elements from one collection into a new one.
static double[] copyCollection(double[] toCopy)
          Copies elements from one collection into a new one.
static int[] copyCollection(int[] toCopy)
          Copies elements from one collection into a new one.
static
<T extends com.multicorebsp.core.CompulsaryCloneable<? extends T>>
java.util.Collection<T>
copyCollection(T[] toCopy)
          Copies elements from one collection into a new one.
static
<T,S extends java.util.Collection<T>>
java.util.Collection<T>
flatten(java.util.Collection<S> x)
          Takes various collections of the same type and flattens them (takes consecutive unions) into a single collection.
static
<T,S extends java.util.Collection<T>>
void
flatten(java.util.Collection<S> from, java.util.Collection<T> into)
          Takes various collections of the same type and flattens them (takes consecutive unions) into a single collection.
static
<T,S extends java.util.Collection<T>>
java.util.Collection<T>
lazyAdd(S c, T x)
          Adds an element to a collection, but does this lazily (no actual addition takes place unless necessary).
static void printCollection(java.util.Collection<?> x)
          Print a collection to screen.
static void printCollection(java.lang.String prefix, double[] x)
          Prints a collection to screen, preceded by a given prefix.
static void printCollection(java.lang.String prefix, double[][] data)
          Prints a collection to screen, preceded by a given prefix.
static void printCollection(java.lang.String prefix, int[] x)
          Prints a collection to screen, preceded by a given prefix.
static
<T> void
printCollection(T[] js)
          Writes a collection to file.
static void writeCollection(java.lang.String fn, double[] data)
          Writes a collection to file.
static void writeCollection(java.lang.String fn, double[][] data)
          Writes a collection to file.
static void writeCollection(java.lang.String fn, int[] js)
          Writes a collection to file.
static void writeCollection(java.lang.String fn, java.lang.Iterable<?> x)
          Writes a collection to file.
static
<T> void
writeCollection(java.lang.String fn, T[] js)
          Writes a collection to file.
 
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

flatten

public static <T,S extends java.util.Collection<T>> java.util.Collection<T> flatten(java.util.Collection<S> x)
Takes various collections of the same type and flattens them (takes consecutive unions) into a single collection.

Type Parameters:
T - The type of elements stored in the collection
S - The type of collections to concatenate
Parameters:
x - The various collections to be flattened
Returns:
The flattened collection

flatten

public static <T,S extends java.util.Collection<T>> void flatten(java.util.Collection<S> from,
                                                                 java.util.Collection<T> into)
Takes various collections of the same type and flattens them (takes consecutive unions) into a single collection. This is the in-place variant.

Type Parameters:
T - The type of elements stored in the collection
S - The type of collections to concatenate
Parameters:
from - The various collections to be flattened
into - Where to store the flattened collection (while keeping the elements already there)

clear

public static <T> void clear(java.util.Collection<T> x)
Empties a collection.

Type Parameters:
T - The type of elements in the collection
Parameters:
x - The collection to clear

printCollection

public static void printCollection(java.util.Collection<?> x)
Print a collection to screen.

Parameters:
x - The collection to print

printCollection

public static <T> void printCollection(T[] js)
Writes a collection to file. Generic array variant.

Type Parameters:
T - What type of elements to write
Parameters:
js - Which elements to print

printCollection

public static void printCollection(java.lang.String prefix,
                                   double[] x)
Prints a collection to screen, preceded by a given prefix. Raw double array variant.

Parameters:
prefix - What to print to screen first
x - The elements to be printed to screen, after the prefix

printCollection

public static void printCollection(java.lang.String prefix,
                                   int[] x)
Prints a collection to screen, preceded by a given prefix. Raw integer array variant.

Parameters:
prefix - What to print to screen first
x - The elements to be printed to screen, after the prefix

printCollection

public static void printCollection(java.lang.String prefix,
                                   double[][] data)
Prints a collection to screen, preceded by a given prefix. Raw double matrix variant.

Parameters:
prefix - What to print to screen first
data - The elements to be printed to screen, after the prefix

writeCollection

public static void writeCollection(java.lang.String fn,
                                   int[] js)
Writes a collection to file. Raw integer array variant.

Parameters:
fn - Which file to output to
js - Which elements to write to file

writeCollection

public static <T> void writeCollection(java.lang.String fn,
                                       T[] js)
Writes a collection to file. Generic array variant.

Type Parameters:
T - What type of elements to write.
Parameters:
fn - Which file to output to
js - Which elements to write to file

writeCollection

public static void writeCollection(java.lang.String fn,
                                   java.lang.Iterable<?> x)
Writes a collection to file.

Parameters:
fn - Which file to output to
x - Collection of elements to write to file

writeCollection

public static void writeCollection(java.lang.String fn,
                                   double[] data)
Writes a collection to file. Raw double array variant.

Parameters:
fn - Which file to output to
data - Which elements to write to file

writeCollection

public static void writeCollection(java.lang.String fn,
                                   double[][] data)
Writes a collection to file. Raw double matrix variant.

Parameters:
fn - Which file to output to
data - Which elements to write to file

copyCollection

public static <T extends com.multicorebsp.core.CompulsaryCloneable<? extends T>> java.util.Collection<T> copyCollection(java.util.Collection<T> toCopy)
Copies elements from one collection into a new one. Elements are copied deeply; they will not depend on common instances. This is ensured through the CompulsaryCloneable interface.

Type Parameters:
T - The type stored in the collections.
Parameters:
toCopy - The collection to copy.
Returns:
The copied collection, with deep copies of each element.
See Also:
CompulsaryCloneable

copyCollection

public static <T extends com.multicorebsp.core.CompulsaryCloneable<? extends T>> java.util.Collection<T> copyCollection(T[] toCopy)
Copies elements from one collection into a new one. Elements are copied deeply; they will not depend on common instances. This is ensured through the CompulsaryCloneable interface. Generic array variant.

Type Parameters:
T - The type stored in the collections.
Parameters:
toCopy - The collection to copy.
Returns:
The copied collection, with deep copies of each element.
See Also:
CompulsaryCloneable

copyCollection

public static int[] copyCollection(int[] toCopy)
Copies elements from one collection into a new one. Raw integer array variant.

Parameters:
toCopy - The collection to copy.
Returns:
The copied collection, with deep copies of each element.

copyCollection

public static double[] copyCollection(double[] toCopy)
Copies elements from one collection into a new one. Raw double array variant.

Parameters:
toCopy - The collection to copy.
Returns:
The copied collection, with deep copies of each element.

lazyAdd

public static <T,S extends java.util.Collection<T>> java.util.Collection<T> lazyAdd(S c,
                                                                                    T x)
                                       throws java.lang.InstantiationException,
                                              java.lang.IllegalAccessException
Adds an element to a collection, but does this lazily (no actual addition takes place unless necessary).

Type Parameters:
T - The type of elements stored
S - The type of collections used
Parameters:
c - The collection to add to
x - The element to add
Returns:
A LazyCollection containing c and x
Throws:
java.lang.InstantiationException
java.lang.IllegalAccessException

contains

public static <T> boolean contains(java.util.Collection<T> a,
                                   java.util.Collection<T> c)
Checks if any of the elements in c are contained in a.

Type Parameters:
T - The type of elements used
Parameters:
a - The collection to check
c - The collection for which to be checked
Returns:
Whether the intersection of a and c is nonempty

contains

public static <T> boolean contains(T[] a,
                                   java.util.Collection<T> c)
Checks if any of the elements in c are contained in a. Generic array variant.

Type Parameters:
T - The type of elements used
Parameters:
a - The collection to check
c - The collection for which to be checked
Returns:
Whether the intersection of a and c is nonempty

contains

public static boolean contains(int[] a,
                               java.util.Collection<java.lang.Integer> c)
Checks if any of the elements in c are contained in a. Raw integer array variant.

Parameters:
a - The collection to check
c - The collection for which to be checked
Returns:
Whether the intersection of a and c is nonempty

contains

public static boolean contains(int[] i,
                               int add_1)
Checks if an element is contained in a collection. Raw integer array variant.

Parameters:
i - Collection to check
add_1 - Value to check for
Returns:
If add_1 is contained in i

copy

public static double[] copy(double[] original)
Copies a vector of doubles.

Parameters:
original - The vector to copy
Returns:
The copied vector