Class ReverseIterator<E>

  • All Implemented Interfaces:
    java.util.Iterator<E>

    public class ReverseIterator<E>
    extends java.lang.Object
    implements java.util.Iterator<E>
    An iterator that wraps an existing list iterator and provides its elements in reverse order. The list list iterator will be placed at the end of the list. For added efficiency, the decorated list iterator should already be at the end of the list.
    Author:
    Garret Wilson
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected java.util.ListIterator<E> listIterator
      The list iterator this class decorates.
    • Constructor Summary

      Constructors 
      Constructor Description
      ReverseIterator​(java.lang.Iterable<E> iterable)
      Iterable constructor.
      ReverseIterator​(java.util.ListIterator<E> listIterator)
      List iterator constructor.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean hasNext()
      Returns true if the iteration has more elements.
      E next()
      Returns the next element in the iteration.
      void remove()
      Removes from the underlying collection the last element returned by the iterator (optional operation).
      protected static <T> java.util.ListIterator<T> toEndListIterator​(java.lang.Iterable<T> iterable)
      Returns a list iterator to the given iterable.
      • Methods inherited from class java.lang.Object

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

        forEachRemaining
    • Field Detail

      • listIterator

        protected final java.util.ListIterator<E> listIterator
        The list iterator this class decorates.
    • Constructor Detail

      • ReverseIterator

        public ReverseIterator​(java.lang.Iterable<E> iterable)
        Iterable constructor. A temporary collection will be created and filled with the contents of the given iterable unless the given iterable is a List .
        Parameters:
        iterable - The iterable this iterator should decorate.
        Throws:
        java.lang.NullPointerException - if the given iterable is null.
      • ReverseIterator

        public ReverseIterator​(java.util.ListIterator<E> listIterator)
        List iterator constructor. The list list iterator will be placed at the end of the list. For added efficiency, the decorated list iterator should already be at the end of the list.
        Parameters:
        listIterator - The list iterator this iterator should decorate.
        Throws:
        java.lang.NullPointerException - if the given iterator is null.
    • Method Detail

      • toEndListIterator

        protected static <T> java.util.ListIterator<T> toEndListIterator​(java.lang.Iterable<T> iterable)
        Returns a list iterator to the given iterable. The list iterator will be placed at the end of the elements for efficiency. If the given iterable is not a List, a temporary list will be created and filled with the contents of the given iterable.
        Type Parameters:
        T - The type of elements contained in the iterable.
        Parameters:
        iterable - The iterable to elements.
        Returns:
        A list iterator to the elements of the given iterable, pointing to the end of the list.
      • hasNext

        public boolean hasNext()
        Returns true if the iteration has more elements. This implementation delegates to the decorated iterator's opposite method, ListIterator.hasPrevious().
        Specified by:
        hasNext in interface java.util.Iterator<E>
        Returns:
        true if the iterator has more elements, which for this implementation is in the reverse direction.
      • next

        public E next()
        Returns the next element in the iteration. This implementation delegates to the decorated iterator's opposite method, ListIterator.previous().
        Specified by:
        next in interface java.util.Iterator<E>
        Returns:
        The next element in the iteration, which for this implementation is in the reverse direction.
        Throws:
        java.util.NoSuchElementException - iteration has no more elements.
      • remove

        public void remove()
        Removes from the underlying collection the last element returned by the iterator (optional operation). This implementation delegates to the decorated iterator's ListIterator.remove() method.
        Specified by:
        remove in interface java.util.Iterator<E>
        Throws:
        java.lang.UnsupportedOperationException - if the remove() operation is not supported by this iterator.
        java.lang.IllegalStateException - if the next() method has not yet been called, or the remove() method has already been called after the last call to the next() method.