Class Iterators


  • public final class Iterators
    extends java.lang.Object
    Utility class for iterator treatment.
    Author:
    agz
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> T extractSingleElement​(java.util.Iterator<? extends T> iteratorInput, Criterion<? super T> criterion)
      Returns the one-and-only element of the specified specified input Iterator that satisfies the specified Criterion.
      static <T> void filter​(java.util.Iterator<T> iteratorInput, Criterion<? super T> criterion, java.util.Collection<? super T> colOutput)
      Iterates through the element of the specified input Iterator and adds those elements that satisfy the specified Criterion to the specified output Collection.
      static <T> java.util.ArrayList<T> newArrayList​(java.util.Iterator<? extends T> iteratorInput, Criterion<? super T> criterion, int sizeGuess)
      Creates a new ArrayList with those elements of the specified input Iterator that satisfy the specified Criterion.
      static boolean startsWith​(java.util.Iterator<?> iterator1, java.util.Iterator<?> iterator2)
      Returns whether the first specified Iterator starts with the same elements as the second specified Iterator (by Object.equals(Object)-comparison) and contains at least the same number of elements.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • filter

        public static <T> void filter​(java.util.Iterator<T> iteratorInput,
                                      Criterion<? super T> criterion,
                                      java.util.Collection<? super T> colOutput)
        Iterates through the element of the specified input Iterator and adds those elements that satisfy the specified Criterion to the specified output Collection.

        The specified input Iterator must not be null.

        The specified Criterion may be null, in which case all elements of the input Iterator are copied to the output Collection.

        The specified output Collection must not be null.

        After this call the specified input Iterator will be expired.

        Type Parameters:
        T - Type of elements of the input Iterator
        Parameters:
        iteratorInput - input Iterator whose elements are to filter with the specified Criterion and to add to the output Collection
        criterion - Criterion to filter the elements of the input Iterator with
        colOutput - output Collection to accept the filtered input
      • newArrayList

        public static <T> java.util.ArrayList<T> newArrayList​(java.util.Iterator<? extends T> iteratorInput,
                                                              Criterion<? super T> criterion,
                                                              int sizeGuess)
        Creates a new ArrayList with those elements of the specified input Iterator that satisfy the specified Criterion.

        The specified input Iterator must not be null.

        The specified Criterion may be null, in which case all elements of the input Iterator are copied to the new ArrayList.

        After this call the specified input Iterator will be expired.

        Type Parameters:
        T - Type of the elements of the new ArrayList
        Parameters:
        iteratorInput - input Iterator whose elements are to filter with the specified Criterion and to add to the new ArrayList
        criterion - Criterion to filter the input Iterator with
        sizeGuess - size guess of the new list/count of filtered elements
        Returns:
        new ArrayList with those elements of the specified input Iterator that satisfy the specified Criterion
      • extractSingleElement

        public static <T> T extractSingleElement​(java.util.Iterator<? extends T> iteratorInput,
                                                 Criterion<? super T> criterion)
        Returns the one-and-only element of the specified specified input Iterator that satisfies the specified Criterion.

        Returns null if no such element exists.

        Throws a runtime exception if more than one element satisfy the specified Criterion.

        The specified input Iterator must not be null.

        The specified Criterion may be null, in which case the Iterator must not iterate through more than one element.

        After this call the specified input Iterator will be expired.

        Type Parameters:
        T - Type of the element to return
        Parameters:
        iteratorInput - input Iterator whose elements are to filter with the specified Criterion
        criterion - Criterion to filter the input Iterator with
        Returns:
        the one-and-only element of the specified input Iterator that satisfies the specified Criterion
      • startsWith

        public static boolean startsWith​(java.util.Iterator<?> iterator1,
                                         java.util.Iterator<?> iterator2)
        Returns whether the first specified Iterator starts with the same elements as the second specified Iterator (by Object.equals(Object)-comparison) and contains at least the same number of elements.

        If this method returns true, the first Iterator points to the first element that is not contained in the second Iterator and the second Iterator has no more elements to iterate.

        If this method returns false, the positions of both Iterators are undefined.

        Parameters:
        iterator1 - Iterator to test the first objects for
        iterator2 - Iterator that contains the sequence of starting objects to test the first Iterator for
        Returns:
        whether the first specified Iterator starts with the same elements as the second specified Iterator