Class MinimalIterable<E>

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

    public final class MinimalIterable<E>
    extends java.lang.Object
    implements java.lang.Iterable<E>
    An implementation of Iterable which throws an exception on all invocations of the iterator() method after the first, and whose iterator is always unmodifiable.

    The Iterable specification does not make it absolutely clear what should happen on a second invocation, so implementors have made various choices, including:

    • returning the same iterator again
    • throwing an exception of some kind
    • or the usual, robust behavior, which all known Collection implementations have, of returning a new, independent iterator

    Because of this situation, any public method accepting an iterable should invoke the iterator method only once, and should be tested using this class. Exceptions to this rule should be clearly documented.

    Note that although your APIs should be liberal in what they accept, your methods which return iterables should make every attempt to return ones of the robust variety.

    This testing utility is not thread-safe.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.util.Iterator<E> iterator  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private MinimalIterable​(java.util.Iterator<E> iterator)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static <E> MinimalIterable<E> from​(java.util.Collection<E> elements)
      Returns an iterable whose iterator returns the given elements in order.
      java.util.Iterator<E> iterator()  
      static <E> MinimalIterable<E> of​(E... elements)
      Returns an iterable whose iterator returns the given elements in order.
      • 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 java.util.Iterator<E> iterator
    • Constructor Detail

      • MinimalIterable

        private MinimalIterable​(java.util.Iterator<E> iterator)
    • Method Detail

      • of

        public static <E> MinimalIterable<E> of​(E... elements)
        Returns an iterable whose iterator returns the given elements in order.
      • from

        public static <E> MinimalIterable<E> from​(java.util.Collection<E> elements)
        Returns an iterable whose iterator returns the given elements in order. The elements are copied out of the source collection at the time this method is called.
      • iterator

        public java.util.Iterator<E> iterator()
        Specified by:
        iterator in interface java.lang.Iterable<E>