Class ReverseIterator<E>
- java.lang.Object
-
- com.globalmentor.collections.iterators.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()
Returnstrue
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.
-
-
-
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 aList
.- Parameters:
iterable
- The iterable this iterator should decorate.- Throws:
java.lang.NullPointerException
- if the given iterable isnull
.
-
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 isnull
.
-
-
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 aList
, 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()
Returnstrue
if the iteration has more elements. This implementation delegates to the decorated iterator's opposite method,ListIterator.hasPrevious()
.- Specified by:
hasNext
in interfacejava.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 interfacejava.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'sListIterator.remove()
method.- Specified by:
remove
in interfacejava.util.Iterator<E>
- Throws:
java.lang.UnsupportedOperationException
- if theremove()
operation is not supported by this iterator.java.lang.IllegalStateException
- if thenext()
method has not yet been called, or theremove()
method has already been called after the last call to thenext()
method.
-
-