org.omnaest.utils.structure.iterator
Class IteratorUtils

java.lang.Object
  extended by org.omnaest.utils.structure.iterator.IteratorUtils

public class IteratorUtils
extends Object

Helper related to Iterator

Author:
Omnaest
See Also:
IterableUtils

Constructor Summary
IteratorUtils()
           
 
Method Summary
static
<E> Iterator<E>
adapter(ElementStream<E> elementStream)
          Returns an Iterator adapter on a given ElementStream
static
<TO,FROM> Iterator<TO>
adapter(Iterator<? extends FROM> iterator, ElementConverter<FROM,TO> elementConverter)
          Returns a new decorator instance of the given Iterator which uses the given ElementConverter to convert the result of the Iterator.next() method.
static
<TO,FROM,T>
Iterator<TO>
adapter(Iterator<FROM> iterator, ElementConverter<FROM,? extends T> elementConverterFirst, ElementConverter<T,? extends TO> elementConverterSecond)
          Similar to adapter(Iterator, ElementConverter)
static
<TO,FROM,T1,T2>
Iterator<TO>
adapter(Iterator<FROM> iterator, ElementConverter<FROM,? extends T1> elementConverterFirst, ElementConverter<T1,? extends T2> elementConverterSecond, ElementConverter<T2,? extends TO> elementConverterThird)
          Similar to adapter(Iterator, ElementConverter)
static
<E> Iterator<E>
addToNewIterator(Iterator<E> iterator, E... elements)
          Returns a new Iterator instance which contains all elements from the given Iterator and the additional given elements.
static
<E> Iterator<E>
chained(Collection<? extends Iterator<E>> iterators)
          Similar to chained(Iterator...)
static
<E> Iterator<E>
chained(Iterator<E>... iterators)
          Returns an Iterator wrapper which chains the given Iterator instances.
static
<E> void
drainTo(Iterator<E> iterator, Collection<E> collection)
          Drains the given Iterator to the given Collection
This invokes the Iterator.remove() method for every element drained.
static
<E> void
drainTo(Iterator<E> iterator, Collection<E> collection, int maxNumberOfElements)
          Drains the given Iterator by the given maximum number of elements to the given Collection.
static
<E> Iterator<E>
empty()
          Returns an empty Iterator
static
<E> Iterator<E>
factoryBasedIterator(Factory<Iterator<E>> iteratorFactory)
          Returns a new Iterator instance which will iterate over all Iterator instances created by the given Factory.
static
<E> Iterator<E>
filtered(Iterator<E> iterator, BitSet filter)
          Returns an Iterator which returns a filtered subset of the given Iterator based on the given filter BitSet
static
<E> Iterator<E>
limitingIteratorDecorator(Iterator<E> iterator, int limit)
          Returns a new decorator instance wrapping the given Iterator which will limit the elements which can be retrieved to the given number
static
<E> Iterator<E>
lockedByReentrantLockIterator(Iterator<E> iterator)
          Returns a view on the given Iterator which uses a ReentrantLock to synchronize all its methods.
static
<E> Iterator<E>
lockedIterator(Iterator<E> iterator, Lock lock)
          Returns a view on the given Iterator which uses a Lock to synchronize all its methods.
static
<E> Iterator<E>
merge(Iterator<E>... iterators)
          Merges all elements immediately into a single and new Iterator instance.
static
<E> Iterator<E>
roundRobin(Iterator<E>... iterators)
          Returns an Iterator which calls Iterator.next() of all given Iterators in a round robin way
static int size(Iterator<?> iterator)
          Resolves the size of a given Iterator by iterating over it.
static
<E> Iterator<E>
threadLocalCachedIterator(Iterator<E> iterator)
          Returns a view on the given Iterator which uses a ThreadLocal instance to cache resolved elements per Thread.
static
<E> Iterator<E>
valueOf(E... elements)
          Returns an Iterator instance for the given elements
static
<E> Iterator<E>[]
valueOf(Iterable<E>... iterables)
          Returns the Iterator instances of the given Iterables.
static
<E> ListIterator<E>[]
valueOf(List<E>... lists)
          Returns the ListIterator instances of the given Lists.
static
<E> Iterator<E>[]
valueOfMultiple(Iterable<E>... iterables)
          Returns a List of the Iterators related to the given Iterables
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

IteratorUtils

public IteratorUtils()
Method Detail

size

public static int size(Iterator<?> iterator)
Resolves the size of a given Iterator by iterating over it.

Parameters:
iterator -
Returns:

lockedIterator

public static <E> Iterator<E> lockedIterator(Iterator<E> iterator,
                                             Lock lock)
Returns a view on the given Iterator which uses a Lock to synchronize all its methods.

Parameters:
iterator -
lock -
Returns:

threadLocalCachedIterator

public static <E> Iterator<E> threadLocalCachedIterator(Iterator<E> iterator)
Returns a view on the given Iterator which uses a ThreadLocal instance to cache resolved elements per Thread. The underlying Iterator will by locked to be usable by multiple threads.

Parameters:
iterator -
Returns:
See Also:
lockedIterator(Iterator, Lock), ThreadLocalCachedIterator

lockedByReentrantLockIterator

public static <E> Iterator<E> lockedByReentrantLockIterator(Iterator<E> iterator)
Returns a view on the given Iterator which uses a ReentrantLock to synchronize all its methods.

Parameters:
iterator -
Returns:

factoryBasedIterator

public static <E> Iterator<E> factoryBasedIterator(Factory<Iterator<E>> iteratorFactory)
Returns a new Iterator instance which will iterate over all Iterator instances created by the given Factory. If the Factory.newInstance() returns null, the Iterator ends.

Parameters:
iteratorFactory -
Returns:

merge

public static <E> Iterator<E> merge(Iterator<E>... iterators)
Merges all elements immediately into a single and new Iterator instance.
All given Iterator instances will be traversed after calling this method.

Parameters:
iterators -
Returns:
See Also:
chained(Iterator...)

addToNewIterator

public static <E> Iterator<E> addToNewIterator(Iterator<E> iterator,
                                               E... elements)
Returns a new Iterator instance which contains all elements from the given Iterator and the additional given elements.
All given Iterator instances will be traversed after calling this method.

Parameters:
iterator -
elements -
Returns:
See Also:
chained(Iterator...)

chained

public static <E> Iterator<E> chained(Iterator<E>... iterators)
Returns an Iterator wrapper which chains the given Iterator instances. The single Iterator instances will only executed when the returned wrapper iterator points to them.

Parameters:
iterators -
Returns:
See Also:
merge(Iterator...)

chained

public static <E> Iterator<E> chained(Collection<? extends Iterator<E>> iterators)
Similar to chained(Iterator...)

Parameters:
iterators -
Returns:

roundRobin

public static <E> Iterator<E> roundRobin(Iterator<E>... iterators)
Returns an Iterator which calls Iterator.next() of all given Iterators in a round robin way

Parameters:
iterators -
Returns:

valueOfMultiple

public static <E> Iterator<E>[] valueOfMultiple(Iterable<E>... iterables)
Returns a List of the Iterators related to the given Iterables

Parameters:
iterables -
Returns:

adapter

public static <TO,FROM> Iterator<TO> adapter(Iterator<? extends FROM> iterator,
                                             ElementConverter<FROM,TO> elementConverter)
Returns a new decorator instance of the given Iterator which uses the given ElementConverter to convert the result of the Iterator.next() method.

If the given ElementConverter or Iterator is null, this method return null.

The elements will be converted at traversal time not in advance.

Parameters:
iterator -
elementConverter -
Returns:

adapter

public static <TO,FROM,T> Iterator<TO> adapter(Iterator<FROM> iterator,
                                               ElementConverter<FROM,? extends T> elementConverterFirst,
                                               ElementConverter<T,? extends TO> elementConverterSecond)
Similar to adapter(Iterator, ElementConverter)

Parameters:
iterator -
elementConverterFirst -
elementConverterSecond -
Returns:

adapter

public static <TO,FROM,T1,T2> Iterator<TO> adapter(Iterator<FROM> iterator,
                                                   ElementConverter<FROM,? extends T1> elementConverterFirst,
                                                   ElementConverter<T1,? extends T2> elementConverterSecond,
                                                   ElementConverter<T2,? extends TO> elementConverterThird)
Similar to adapter(Iterator, ElementConverter)

Parameters:
iterator -
elementConverterFirst -
elementConverterSecond -
elementConverterThird -
Returns:

adapter

public static <E> Iterator<E> adapter(ElementStream<E> elementStream)
Returns an Iterator adapter on a given ElementStream

Parameters:
elementStream -
Returns:

valueOf

public static <E> Iterator<E>[] valueOf(Iterable<E>... iterables)
Returns the Iterator instances of the given Iterables. If an Iterator instance is null it will not be added to the returned array. This circumstance can lead to different array sizes.

Parameters:
iterables -
Returns:

valueOf

public static <E> ListIterator<E>[] valueOf(List<E>... lists)
Returns the ListIterator instances of the given Lists. If an ListIterator instance is null it will not be added to the returned array. This circumstance can lead to different array sizes.

Parameters:
lists -
Returns:

valueOf

public static <E> Iterator<E> valueOf(E... elements)
Returns an Iterator instance for the given elements

Parameters:
elements -
Returns:
new instance of an Iterator

drainTo

public static <E> void drainTo(Iterator<E> iterator,
                               Collection<E> collection)
Drains the given Iterator to the given Collection
This invokes the Iterator.remove() method for every element drained.

Parameters:
iterator -
collection -

drainTo

public static <E> void drainTo(Iterator<E> iterator,
                               Collection<E> collection,
                               int maxNumberOfElements)
Drains the given Iterator by the given maximum number of elements to the given Collection.
This invokes the Iterator.remove() method for every element drained.

Parameters:
iterator -
collection -
maxNumberOfElements -

limitingIteratorDecorator

public static <E> Iterator<E> limitingIteratorDecorator(Iterator<E> iterator,
                                                        int limit)
Returns a new decorator instance wrapping the given Iterator which will limit the elements which can be retrieved to the given number

Parameters:
iterator - Iterator
limit -
Returns:

filtered

public static <E> Iterator<E> filtered(Iterator<E> iterator,
                                       BitSet filter)
Returns an Iterator which returns a filtered subset of the given Iterator based on the given filter BitSet

Parameters:
iterator -
filter -
Returns:
new Iterator instance

empty

public static <E> Iterator<E> empty()
Returns an empty Iterator

Returns:


Copyright © 2013. All Rights Reserved.