Class ListIteratorWrapper<E>
- java.lang.Object
-
- org.apache.commons.collections4.iterators.ListIteratorWrapper<E>
-
- All Implemented Interfaces:
Iterator<E>
,ListIterator<E>
,OrderedIterator<E>
,ResettableIterator<E>
,ResettableListIterator<E>
public class ListIteratorWrapper<E> extends Object implements ResettableListIterator<E>
Converts anIterator
into aResettableListIterator
. For plainIterator
s this is accomplished by caching the returned elements. This class can also be used to simply addResettableIterator
functionality to a givenListIterator
.The
ListIterator
interface has additional useful methods for navigation -previous()
and the index methods. This class allows a regularIterator
to behave as aListIterator
. It achieves this by building a list internally of as the underlying iterator is traversed.The optional operations of
ListIterator
are not supported for plainIterator
s.This class implements ResettableListIterator from Commons Collections 3.2.
- Since:
- 2.1
-
-
Constructor Summary
Constructors Constructor Description ListIteratorWrapper(Iterator<? extends E> iterator)
Constructs a newListIteratorWrapper
that will wrap the given iterator.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
add(E obj)
boolean
hasNext()
Returns true if there are more elements in the iterator.boolean
hasPrevious()
Returns true if there are previous elements in the iterator.E
next()
Returns the next element from the iterator.int
nextIndex()
Returns the index of the next element.E
previous()
Returns the previous element.int
previousIndex()
Returns the index of the previous element.void
remove()
ThrowsUnsupportedOperationException
ifprevious()
has ever been called.void
reset()
Resets this iterator back to the position at which the iterator was created.void
set(E obj)
-
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
-
-
-
-
Constructor Detail
-
ListIteratorWrapper
public ListIteratorWrapper(Iterator<? extends E> iterator)
Constructs a newListIteratorWrapper
that will wrap the given iterator.- Parameters:
iterator
- the iterator to wrap- Throws:
NullPointerException
- if the iterator is null
-
-
Method Detail
-
add
public void add(E obj) throws UnsupportedOperationException
- Specified by:
add
in interfaceListIterator<E>
- Parameters:
obj
- the object to add- Throws:
UnsupportedOperationException
- if the underlying iterator is not of typeListIterator
-
hasNext
public boolean hasNext()
Returns true if there are more elements in the iterator.
-
hasPrevious
public boolean hasPrevious()
Returns true if there are previous elements in the iterator.- Specified by:
hasPrevious
in interfaceListIterator<E>
- Specified by:
hasPrevious
in interfaceOrderedIterator<E>
- Returns:
- true if there are previous elements
-
next
public E next() throws NoSuchElementException
Returns the next element from the iterator.- Specified by:
next
in interfaceIterator<E>
- Specified by:
next
in interfaceListIterator<E>
- Returns:
- the next element from the iterator
- Throws:
NoSuchElementException
- if there are no more elements
-
nextIndex
public int nextIndex()
Returns the index of the next element.- Specified by:
nextIndex
in interfaceListIterator<E>
- Returns:
- the index of the next element
-
previous
public E previous() throws NoSuchElementException
Returns the previous element.- Specified by:
previous
in interfaceListIterator<E>
- Specified by:
previous
in interfaceOrderedIterator<E>
- Returns:
- the previous element
- Throws:
NoSuchElementException
- if there are no previous elements
-
previousIndex
public int previousIndex()
Returns the index of the previous element.- Specified by:
previousIndex
in interfaceListIterator<E>
- Returns:
- the index of the previous element
-
remove
public void remove() throws UnsupportedOperationException
ThrowsUnsupportedOperationException
ifprevious()
has ever been called.- Specified by:
remove
in interfaceIterator<E>
- Specified by:
remove
in interfaceListIterator<E>
- Throws:
UnsupportedOperationException
- always
-
set
public void set(E obj) throws UnsupportedOperationException
- Specified by:
set
in interfaceListIterator<E>
- Parameters:
obj
- the object to set- Throws:
UnsupportedOperationException
- if the underlying iterator is not of typeListIterator
-
reset
public void reset()
Resets this iterator back to the position at which the iterator was created.- Specified by:
reset
in interfaceResettableIterator<E>
- Since:
- 3.2
-
-