com.infomancers.collections.iterators
Class Iterators

java.lang.Object
  extended by com.infomancers.collections.iterators.Iterators

public final class Iterators
extends java.lang.Object

Utility class to create commonly used iterators.


Constructor Summary
Iterators()
           
 
Method Summary
static
<T> java.util.List<T>
asList(java.lang.Iterable<T> iterable)
          Used to get a List item from an iterable.
static
<T,K> java.lang.Iterable<K>
deepenIterable(java.lang.Iterable<T> iterable, Transformation<T,java.lang.Iterable<K>> transformation)
          Used when a certain type T contains an iterable of items of type K.
static
<T> java.lang.Iterable<T>
enumerationIterable(java.util.Enumeration<T> e)
          Transforms an enumeration into an iterable, by yielding all the enumeration's items.
static
<T> java.lang.Iterable<T>
filteredIterable(java.lang.Iterable<T> iterable, Predicate<T> filter)
          Used to create an iterable instance which yields only items answering a boolean query.
static
<T> java.lang.Iterable<T>
loopIterable(java.lang.Iterable<T> iterable, int times)
          Used to create an iterable which loops over a different iterable.
static
<T,K> java.lang.Iterable<K>
transformIterable(java.lang.Iterable<T> iterable, Transformation<T,K> transformation)
          Used to create an iterable instance which yields transformed items from an original iterable instance.
static
<T> java.lang.Iterable<T>
uniqueIterable(java.lang.Iterable<T> iterable)
          Used to iterate over only unique items from an already existing iteration.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Iterators

public Iterators()
Method Detail

asList

public static <T> java.util.List<T> asList(java.lang.Iterable<T> iterable)
Used to get a List item from an iterable.

This List is detached from the Iterable - Changes to the items returned by the Iterable do not change the List items, and vice versa.

If the iterable is a collection, an ArrayList is created with the ArrayList(Collection) method. Otherwise, a LinkedList is created and all elements are added one by one.

Parameters:
iterable - The iterable to convert to a List.
Returns:
A List containing all items in the iterable.

deepenIterable

public static <T,K> java.lang.Iterable<K> deepenIterable(java.lang.Iterable<T> iterable,
                                                         Transformation<T,java.lang.Iterable<K>> transformation)
Used when a certain type T contains an iterable of items of type K.

This iterable will iterate all contained K items within all T items returned by the original iterable instance.

Parameters:
iterable - The original iterable instance.
transformation - A transformation which returns the collection of K items contained within the T item.
Returns:
An iterable of all K items referenced by the T items.

enumerationIterable

public static <T> java.lang.Iterable<T> enumerationIterable(java.util.Enumeration<T> e)
Transforms an enumeration into an iterable, by yielding all the enumeration's items.

Parameters:
e - The enumeration.
Returns:
An iteration returning all the enumeration's items.

filteredIterable

public static <T> java.lang.Iterable<T> filteredIterable(java.lang.Iterable<T> iterable,
                                                         Predicate<T> filter)
Used to create an iterable instance which yields only items answering a boolean query.

Parameters:
iterable - The original iterable.
filter - The predicate to use to filter items of the original iterable.
Returns:
A filtered iterable instance.

loopIterable

public static <T> java.lang.Iterable<T> loopIterable(java.lang.Iterable<T> iterable,
                                                     int times)
Used to create an iterable which loops over a different iterable.

Meaning: When the original iterable reaches its end, the next element is the first element, starting the iteration all over again.

Parameters:
iterable - The original iterable.
times - The amount of times to loop over the iterable, or 0 for infinitely.
Returns:
A looping iterable.

transformIterable

public static <T,K> java.lang.Iterable<K> transformIterable(java.lang.Iterable<T> iterable,
                                                            Transformation<T,K> transformation)
Used to create an iterable instance which yields transformed items from an original iterable instance.

Parameters:
iterable - The original iterable instance.
transformation - The transformation used on each element of the iterable instance.
Returns:
An iterable containing transformed items.

uniqueIterable

public static <T> java.lang.Iterable<T> uniqueIterable(java.lang.Iterable<T> iterable)
Used to iterate over only unique items from an already existing iteration.

Meaning, that for the list: "Apple", "Orange", "Apple", "Banana", "Banana", "Orange", this iterator will yield: "Apple", "Orange", "Banana".

Parameters:
iterable - The iteration to get only unique values from.
Returns:
An iteration containing each value in the source iteration only once.