org.omnaest.utils.structure.iterator
Class IterableUtils

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

public class IterableUtils
extends Object

Helper class related to Iterable instances

Author:
Omnaest
See Also:
IteratorUtils

Nested Class Summary
static interface IterableUtils.IterableReplicator<E>
          The IterableUtils.IterableReplicator allows to replicate elements to a given Collection
 
Constructor Summary
IterableUtils()
           
 
Method Summary
static
<FROM,TO> Iterable<TO>
adapter(Iterable<? extends FROM> iterable, ElementConverter<FROM,TO> elementConverter)
          Returns a new Iterable instance which uses an Iterator adapter based on the resolved Iterable.iterator() instance.
static
<E> Iterable<E>
chained(Iterable<E>... iterables)
          Returns an Iterable which iterates through the Iterators of each given Iterable every time the own Iterable.iterator() is called
static
<E> Iterable<E>
circular(Iterable<E> iterable)
          Returns a new Iterable instance for the given one which will return a circular Iterator which circulates endlessly.
static
<E> Iterable<E>
circular(Iterable<E> iterable, long limit)
          Returns a new Iterable instance for the given one which will return a circular Iterator.
static
<E> boolean
contains(Iterable<E> iterable, E value)
          Returns true if the given Iterable contains the given value.
static
<FROM,TO> Iterable<TO>
convert(Iterable<FROM> iterable, ElementConverter<FROM,TO> elementConverter)
          Converts a given Iterable to a new Iterable type using the given ElementConverter
static
<E> int
countEquals(Iterable<E> iterable, E element)
          Counts all elements within an Iterable which are equal to the given element.
static
<E> E
elementAt(Iterable<E> iterable, int indexPosition)
          Returns the element of the given Iterable at the given index position.
static
<E> Iterable<E>
empty()
          Returns an Iterable which returns always a new empty Iterator
static boolean equals(Iterable<?> iterable1, Iterable<?> iterable2)
          Returns true if...
static
<E> Iterable<E>
filtered(Iterable<E> iterable, BitSet filter)
          Returns an Iterable where the Iterable.iterator() instance returns only those elements where the respective bit within the filter BitSet is set to true
static
<E> E
firstElement(Iterable<E> iterable)
          Returns the first element of the given Iterable
static int hashCode(Iterable<?> iterable)
          Generates a Object.hashCode() for a given Iterable and its elements.
static boolean isEmpty(Iterable<?> iterable)
           isEmpty( null ) = true isEmpty( Arrays.asList()) = true isEmpty( Arrays.asList("") ) = false
static boolean isIterableType(Class<?> type)
          Returns true if the given type is assignable to the Iterable interface
static
<E> E
lastElement(Iterable<E> iterable)
          Returns the last element of the given Iterable
static
<E> IterableUtils.IterableReplicator<E>
replicate(Iterable<E> iterable)
          Returns a IterableUtils.IterableReplicator
static
<E> IterableUtils.IterableReplicator<E>
replicate(Iterator<E> iterator)
          Returns a IterableUtils.IterableReplicator
static
<E> Iterable<E>
roundRobin(Iterable<E>... iterables)
          Similar to IteratorUtils.roundRobin(Iterator...)
static int size(Iterable<?> iterable)
          Resolves the size of an Iterable by iterating over it and counting the elements.
static
<E> Map<E,Integer>
toCountedElementsMap(Iterable<E> iterable)
          Returns a Map containing the Set of elements as keys and the count of each element as value.
static
<E> Iterable<E>
valueOf(E... elements)
          Returns an Iterable over the given elements
static
<E> Iterable<E>
valueOf(ElementStream<E> elementStream)
          Returns an Iterable on an ElementStream
static
<E> Iterable<E>
valueOf(Factory<Iterator<E>> iteratorFactory)
          Returns a new Iterable based on a given Factory for Iterators
static
<E> Iterable<E>
valueOf(Iterator<E> iterator)
          Returns a new instance of an Iterable which returns the given Iterator instance
static
<E> Iterable<E>
valueOf(Iterator<E> iterator, boolean replicate)
          Returns an Factory of Iterator instances which is based on an internal List buffer which will contain the elements of the given Iterator.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

IterableUtils

public IterableUtils()
Method Detail

adapter

public static <FROM,TO> Iterable<TO> adapter(Iterable<? extends FROM> iterable,
                                             ElementConverter<FROM,TO> elementConverter)
Returns a new Iterable instance which uses an Iterator adapter based on the resolved Iterable.iterator() instance.

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

Parameters:
iterable - Iterable
elementConverter - ElementConverter
Returns:
new instance

circular

public static <E> Iterable<E> circular(Iterable<E> iterable)
Returns a new Iterable instance for the given one which will return a circular Iterator which circulates endlessly.

Be aware of the fact, that the given Iterable has to return new Iterator instances otherwise this will cause an infinite loop.

Parameters:
iterable -
Returns:
See Also:
circular(Iterable)

circular

public static <E> Iterable<E> circular(Iterable<E> iterable,
                                       long limit)
Returns a new Iterable instance for the given one which will return a circular Iterator. The Iterator will stop additionally if the given limit of cycles is reached. If no limit should be used set the parameter to -1

Parameters:
iterable -
limit -
Returns:
See Also:
circular(Iterable)

convert

public static <FROM,TO> Iterable<TO> convert(Iterable<FROM> iterable,
                                             ElementConverter<FROM,TO> elementConverter)
Converts a given Iterable to a new Iterable type using the given ElementConverter

Parameters:
iterable -
elementConverter -
Returns:

countEquals

public static <E> int countEquals(Iterable<E> iterable,
                                  E element)
Counts all elements within an Iterable which are equal to the given element.

Parameters:
iterable -
element -
Returns:

elementAt

public static <E> E elementAt(Iterable<E> iterable,
                              int indexPosition)
Returns the element of the given Iterable at the given index position. Does never throw an Exception instead returns null if no element could be resolved.

Parameters:
iterable -
indexPosition -
Returns:

empty

public static <E> Iterable<E> empty()
Returns an Iterable which returns always a new empty Iterator

Returns:
See Also:
IteratorUtils.empty()

equals

public static boolean equals(Iterable<?> iterable1,
                             Iterable<?> iterable2)
Returns true if...

Parameters:
iterable1 -
iterable2 -
Returns:

filtered

public static <E> Iterable<E> filtered(Iterable<E> iterable,
                                       BitSet filter)
Returns an Iterable where the Iterable.iterator() instance returns only those elements where the respective bit within the filter BitSet is set to true

Parameters:
iterable -
filter -
Returns:
new Iterable

firstElement

public static <E> E firstElement(Iterable<E> iterable)
Returns the first element of the given Iterable

Parameters:
iterable -
Returns:

hashCode

public static int hashCode(Iterable<?> iterable)
Generates a Object.hashCode() for a given Iterable and its elements. The hash code respects the hash codes of the element instances and their order.

Parameters:
iterable -
Returns:

isEmpty

public static boolean isEmpty(Iterable<?> iterable)
 isEmpty( null ) = true
 isEmpty( Arrays.asList()) = true
 isEmpty( Arrays.asList("") ) = false
 

Parameters:
iterable -
Returns:

lastElement

public static <E> E lastElement(Iterable<E> iterable)
Returns the last element of the given Iterable

Parameters:
iterable -
Returns:

size

public static int size(Iterable<?> iterable)
Resolves the size of an Iterable by iterating over it and counting the elements.

Parameters:
iterable -
Returns:

valueOf

public static <E> Iterable<E> valueOf(ElementStream<E> elementStream)
Returns an Iterable on an ElementStream

Parameters:
elementStream -
Returns:

valueOf

public static <E> Iterable<E> valueOf(Factory<Iterator<E>> iteratorFactory)
Returns a new Iterable based on a given Factory for Iterators

Parameters:
iteratorFactory -
Returns:

valueOf

public static <E> Iterable<E> valueOf(Iterator<E> iterator)
Returns a new instance of an Iterable which returns the given Iterator instance

Parameters:
iterator - Iterator
Returns:
See Also:
valueOf(Iterator, boolean)

isIterableType

public static boolean isIterableType(Class<?> type)
Returns true if the given type is assignable to the Iterable interface

Parameters:
type -
Returns:

valueOf

public static <E> Iterable<E> valueOf(E... elements)
Returns an Iterable over the given elements

Parameters:
elements -
Returns:

toCountedElementsMap

public static <E> Map<E,Integer> toCountedElementsMap(Iterable<E> iterable)
Returns a Map containing the Set of elements as keys and the count of each element as value.

E.g. [a,b,a,a] will be returned as [a=3,b=1]

Parameters:
iterable -
Returns:
Map

contains

public static <E> boolean contains(Iterable<E> iterable,
                                   E value)
Returns true if the given Iterable contains the given value. If the given Iterable is null, false is returned.

Parameters:
iterable -
value -
Returns:

replicate

public static <E> IterableUtils.IterableReplicator<E> replicate(Iterable<E> iterable)
Returns a IterableUtils.IterableReplicator

Parameters:
iterable - Iterable
Returns:
new IterableUtils.IterableReplicator instance

valueOf

public static <E> Iterable<E> valueOf(Iterator<E> iterator,
                                      boolean replicate)
Returns an Factory of Iterator instances which is based on an internal List buffer which will contain the elements of the given Iterator. This allows to replicate the content of an Iterator multiple times.

Parameters:
iterator - Iterator
replicate - if true, the Iterable will return always a new Iterator instance with the same elements based on an internal immutable List buffer, otherwise only the same given instance without using a buffer.
Returns:
new Factory instance for Iterators
See Also:
valueOf(Iterator)

replicate

public static <E> IterableUtils.IterableReplicator<E> replicate(Iterator<E> iterator)
Returns a IterableUtils.IterableReplicator

Parameters:
iterator - Iterator
Returns:
new IterableUtils.IterableReplicator instance

chained

public static <E> Iterable<E> chained(Iterable<E>... iterables)
Returns an Iterable which iterates through the Iterators of each given Iterable every time the own Iterable.iterator() is called

Parameters:
iterables -
Returns:
See Also:
ChainedIterable

roundRobin

public static <E> Iterable<E> roundRobin(Iterable<E>... iterables)
Similar to IteratorUtils.roundRobin(Iterator...)

Parameters:
iterables -
Returns:


Copyright © 2013. All Rights Reserved.