net.sf.molae.pipe.basic
Class PipeUtil

java.lang.Object
  extended by net.sf.molae.pipe.basic.PipeUtil

public final class PipeUtil
extends Object

A collection of iterator related utility methods.


Method Summary
static void assertAssignableFrom(Iterator<?> it, Class<?> cls)
          Checks that all elements of the specified iterator are assignable to the specified class.
static void assertIndexes(int fromIndex, int toIndex, int size)
          Utility method for SubLists.
static
<E> Map<E,Integer>
getCardinalityMap(Iterator<E> it)
          Returns a Map mapping each unique element in the given Iterable to an Integer representing the number of occurrences of that element in the Iterable.
static
<E> E
last(Iterator<E> it)
          Iterates through an iterator to find its last element.
static boolean removeAll(Iterator<?> it)
          Removes all elements from the specified iterator.
static int size(Iterator<?> it)
          Counts all elments of an iterator.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

size

public static int size(Iterator<?> it)
Counts all elments of an iterator.

Parameters:
it - an Iterator object
Returns:
the number of elements of the iterator

last

public static <E> E last(Iterator<E> it)
Iterates through an iterator to find its last element.

Parameters:
it - an Iterator object
Returns:
the last element returned by the specified iterator
Throws:
NoSuchElementException - if iterator is empty

removeAll

public static boolean removeAll(Iterator<?> it)
Removes all elements from the specified iterator.

Parameters:
it - an Iterator object
Returns:
true, if the iterator was not empty before.
Throws:
UnsupportedOperationException - if the specified iterator does not implement the remove method.

assertAssignableFrom

public static void assertAssignableFrom(Iterator<?> it,
                                        Class<?> cls)
Checks that all elements of the specified iterator are assignable to the specified class.

Parameters:
it - an Iterator object
cls - the intended element class of the iterator
Throws:
ClassCastException, - if any element of the specified iterator is not assignable to the specified class.
NullPointerException - if the specified Class parameter is null.

assertIndexes

public static void assertIndexes(int fromIndex,
                                 int toIndex,
                                 int size)
Utility method for SubLists. Checks if
  1. fromIndex < 0
  2. toIndex > size
  3. fromIndex > toIndex

Parameters:
fromIndex - low endpoint (inclusive) of the subList
toIndex - high endpoint (exclusive) of the subList
size - size of the base list
Throws:
IndexOutOfBoundsException - for an illegal endpoint index value (fromIndex < 0 || toIndex > size || fromIndex > toIndex).

getCardinalityMap

public static <E> Map<E,Integer> getCardinalityMap(Iterator<E> it)
Returns a Map mapping each unique element in the given Iterable to an Integer representing the number of occurrences of that element in the Iterable.

Only those elements present in the iterable will appear as keys in the map.

Parameters:
it - the Iterable to get the cardinality map for, must not be null
Returns:
the populated cardinality map
Since:
2.0