Class IteratorIterable<E>

  • All Implemented Interfaces:
    java.lang.Iterable<E>

    public class IteratorIterable<E>
    extends java.lang.Object
    implements java.lang.Iterable<E>
    Adapter to make an Iterator instance appear to be an Iterable instance. The iterable can be constructed in one of two variants: single use, multiple use.

    In the single use iterable case, the iterable is only usable for one iterative operation over the source iterator. Subsequent iterative operations use the same, exhausted source iterator. To create a single use iterable, construct a new IteratorIterable using a Iterator that is NOT a ResettableIterator iterator:

       Iterator<Integer> iterator = // some non-resettable iterator
       Iterable<Integer> iterable = new IteratorIterable<Integer>(iterator);
     

    In the multiple use iterable case, the iterable is usable for any number of iterative operations over the source iterator. Of special note, even though the iterable supports multiple iterations, it does not support concurrent iterations. To implicitly create a multiple use iterable, construct a new IteratorIterable using a ResettableIterator iterator:

       Integer[] array = {Integer.valueOf(1),Integer.valueOf(2),Integer.valueOf(3)};
       Iterator<Integer> iterator = IteratorUtils.arrayIterator(array); // a resettable iterator
       Iterable<Integer> iterable = new IteratorIterable<Integer>(iterator);
     

    A multiple use iterable can also be explicitly constructed using any Iterator and specifying true for the multipleUse flag:

       Iterator<Integer> iterator = // some non-resettable iterator
       Iterable<Integer> iterable = new IteratorIterable<Integer>(iterator, true);
     
    Since:
    4.0
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.util.Iterator<? extends E> iterator
      the iterator being adapted into an iterable.
      private java.util.Iterator<E> typeSafeIterator
      the iterator parameterized as the iterator() return type.
    • Constructor Summary

      Constructors 
      Constructor Description
      IteratorIterable​(java.util.Iterator<? extends E> iterator)
      Constructs a new IteratorIterable that will use the given iterator.
      IteratorIterable​(java.util.Iterator<? extends E> iterator, boolean multipleUse)
      Constructs a new IteratorIterable that will use the given iterator.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private static <E> java.util.Iterator<E> createTypesafeIterator​(java.util.Iterator<? extends E> iterator)
      Factory method to create an Iterator from another iterator over objects of a different subtype.
      java.util.Iterator<E> iterator()
      Gets the iterator wrapped by this iterable.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface java.lang.Iterable

        forEach, spliterator
    • Field Detail

      • iterator

        private final java.util.Iterator<? extends E> iterator
        the iterator being adapted into an iterable.
      • typeSafeIterator

        private final java.util.Iterator<E> typeSafeIterator
        the iterator parameterized as the iterator() return type.
    • Constructor Detail

      • IteratorIterable

        public IteratorIterable​(java.util.Iterator<? extends E> iterator)
        Constructs a new IteratorIterable that will use the given iterator.
        Parameters:
        iterator - the iterator to use.
      • IteratorIterable

        public IteratorIterable​(java.util.Iterator<? extends E> iterator,
                                boolean multipleUse)
        Constructs a new IteratorIterable that will use the given iterator.
        Parameters:
        iterator - the iterator to use.
        multipleUse - true if the new iterable can be used in multiple iterations
    • Method Detail

      • createTypesafeIterator

        private static <E> java.util.Iterator<E> createTypesafeIterator​(java.util.Iterator<? extends E> iterator)
        Factory method to create an Iterator from another iterator over objects of a different subtype.
      • iterator

        public java.util.Iterator<E> iterator()
        Gets the iterator wrapped by this iterable.
        Specified by:
        iterator in interface java.lang.Iterable<E>
        Returns:
        the iterator