Class ExtendedIterator<T>

java.lang.Object
org.apache.rat.utils.ExtendedIterator<T>
Type Parameters:
T - The type of object returned from the iterator.
All Implemented Interfaces:
Iterator<T>

public final class ExtendedIterator<T> extends Object implements Iterator<T>
An ExtendedIterator is an Iterator wrapping around a plain (or presented as plain) Iterator. The wrapping allows the usual operations found on streams (filtering, concatenating, mapping) to be done on an Iterator derived from some other source. It also provides convenience methods for common operations.
  • Method Details

    • createNoRemove

      public static <T> ExtendedIterator<T> createNoRemove(Iterator<T> it)
      Answer an ExtendedIterator wrapped round it, which does not permit .remove() even if it does.
      Parameters:
      it - The Iterator to wrap.
      Returns:
      an Extended iterator on it
    • create

      public static <T> ExtendedIterator<T> create(Stream<T> stream)
      Answer an ExtendedIterator wrapped round a Stream. The extended iterator does not permit .remove().

      The stream should not be used directly. The effect of doing so is undefined.

      Parameters:
      stream - the Stream to create an iterator from.
      Returns:
      an Extended iterator on the stream iterator.
    • flatten

      public static <T> ExtendedIterator<T> flatten(Iterator<Iterator<T>> it)
      Given an Iterator that returns Iterators, this creates an Iterator over the next level values. Similar to list splicing in lisp.
      Parameters:
      it - An iterator of iterators.
      Returns:
      An iterator over the logical concatenation of the inner iterators.
    • emptyIterator

      public static ExtendedIterator<?> emptyIterator()
      An empty Extended iterator
      Returns:
      An empty Extended iterator.
    • create

      public static <T> ExtendedIterator<T> create(Iterator<T> it)
      Answer an ExtendedIterator returning the elements of it. If it is itself an ExtendedIterator, return that; otherwise wrap it.
      Parameters:
      it - The iterator to wrap.
      Returns:
      An Extended iterator wrapping it
    • hasNext

      public boolean hasNext()
      Specified by:
      hasNext in interface Iterator<T>
    • next

      public T next()
      Specified by:
      next in interface Iterator<T>
    • forEachRemaining

      public void forEachRemaining(Consumer<? super T> action)
      Specified by:
      forEachRemaining in interface Iterator<T>
    • remove

      public void remove()
      Specified by:
      remove in interface Iterator<T>
    • removeNext

      public T removeNext()
      Returns the next item and removes it from the iterator.
      Returns:
      the next item from the iterator.
    • andThen

      public <X extends T> ExtendedIterator<T> andThen(Iterator<X> other)
      Chains the other iterator to the end of this one.
      Type Parameters:
      X - The type of object returned from the other iterator.
      Parameters:
      other - the other iterator to extend this iterator with.
      Returns:
      A new iterator returning the contents of this iterator followed by the contents of other{ iterator.}
    • filter

      public ExtendedIterator<T> filter(Predicate<T> predicate)
      Filter this iterator using a predicate. Only items for which the predicate returns true will be included in the result.
      Parameters:
      predicate - The predicate to filter the items with.
      Returns:
      An iterator filtered by the predicate.
    • map

      public <U> ExtendedIterator<U> map(Function<T,U> function)
      Map the elements of the iterator to a now type.
      Type Parameters:
      U - The object type to return.
      Parameters:
      function - The function to map elements of <T> to type <U>.
      Returns:
      An Extended iterator that returns a <U> for very <T> in the original iterator.
    • addTo

      public <U extends Collection<T>> U addTo(U collection)
      A method to add the remaining elements in the iterator an arbitrary collection. This method consumes the iterator.
      Type Parameters:
      U - A collection of objects of type <T>.
      Parameters:
      collection - THe collection to add elements to.
      Returns:
      the collection with the elements added.