Class ListIteratorWrapper
- java.lang.Object
-
- org.apache.commons.collections.iterators.ListIteratorWrapper
-
- All Implemented Interfaces:
java.util.Iterator
,java.util.ListIterator
,ResettableIterator
,ResettableListIterator
public class ListIteratorWrapper extends java.lang.Object implements ResettableListIterator
Converts an iterator into a list iterator by caching the returned entries.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.This class implements ResettableListIterator from Commons Collections 3.2.
- Since:
- Commons Collections 2.1
- Version:
- $Revision: 646777 $ $Date: 2008-04-10 14:33:15 +0200 (Thu, 10 Apr 2008) $
-
-
Field Summary
Fields Modifier and Type Field Description private int
currentIndex
The current index of this iterator.private java.util.Iterator
iterator
The underlying iterator being decorated.private java.util.List
list
The list being used to cache the iterator.private static java.lang.String
UNSUPPORTED_OPERATION_MESSAGE
Message used when remove, set or add are called.private int
wrappedIteratorIndex
The current index of the wrapped iterator.
-
Constructor Summary
Constructors Constructor Description ListIteratorWrapper(java.util.Iterator 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(java.lang.Object obj)
ThrowsUnsupportedOperationException
.boolean
hasNext()
Returns true if there are more elements in the iterator.boolean
hasPrevious()
Returns true if there are previous elements in the iterator.java.lang.Object
next()
Returns the next element from the iterator.int
nextIndex()
Returns in the index of the next element.java.lang.Object
previous()
Returns the the previous element.int
previousIndex()
Returns the index of the previous element.void
remove()
ThrowsUnsupportedOperationException
.void
reset()
Resets this iterator back to the position at which the iterator was created.void
set(java.lang.Object obj)
ThrowsUnsupportedOperationException
.
-
-
-
Field Detail
-
UNSUPPORTED_OPERATION_MESSAGE
private static final java.lang.String UNSUPPORTED_OPERATION_MESSAGE
Message used when remove, set or add are called.- See Also:
- Constant Field Values
-
iterator
private final java.util.Iterator iterator
The underlying iterator being decorated.
-
list
private final java.util.List list
The list being used to cache the iterator.
-
currentIndex
private int currentIndex
The current index of this iterator.
-
wrappedIteratorIndex
private int wrappedIteratorIndex
The current index of the wrapped iterator.
-
-
Method Detail
-
add
public void add(java.lang.Object obj) throws java.lang.UnsupportedOperationException
ThrowsUnsupportedOperationException
.- Specified by:
add
in interfacejava.util.ListIterator
- Parameters:
obj
- the object to add, ignored- Throws:
java.lang.UnsupportedOperationException
- always
-
hasNext
public boolean hasNext()
Returns true if there are more elements in the iterator.- Specified by:
hasNext
in interfacejava.util.Iterator
- Specified by:
hasNext
in interfacejava.util.ListIterator
- Returns:
- true if there are more elements
-
hasPrevious
public boolean hasPrevious()
Returns true if there are previous elements in the iterator.- Specified by:
hasPrevious
in interfacejava.util.ListIterator
- Returns:
- true if there are previous elements
-
next
public java.lang.Object next() throws java.util.NoSuchElementException
Returns the next element from the iterator.- Specified by:
next
in interfacejava.util.Iterator
- Specified by:
next
in interfacejava.util.ListIterator
- Returns:
- the next element from the iterator
- Throws:
java.util.NoSuchElementException
- if there are no more elements
-
nextIndex
public int nextIndex()
Returns in the index of the next element.- Specified by:
nextIndex
in interfacejava.util.ListIterator
- Returns:
- the index of the next element
-
previous
public java.lang.Object previous() throws java.util.NoSuchElementException
Returns the the previous element.- Specified by:
previous
in interfacejava.util.ListIterator
- Returns:
- the previous element
- Throws:
java.util.NoSuchElementException
- if there are no previous elements
-
previousIndex
public int previousIndex()
Returns the index of the previous element.- Specified by:
previousIndex
in interfacejava.util.ListIterator
- Returns:
- the index of the previous element
-
remove
public void remove() throws java.lang.UnsupportedOperationException
ThrowsUnsupportedOperationException
.- Specified by:
remove
in interfacejava.util.Iterator
- Specified by:
remove
in interfacejava.util.ListIterator
- Throws:
java.lang.UnsupportedOperationException
- always
-
set
public void set(java.lang.Object obj) throws java.lang.UnsupportedOperationException
ThrowsUnsupportedOperationException
.- Specified by:
set
in interfacejava.util.ListIterator
- Parameters:
obj
- the object to set, ignored- Throws:
java.lang.UnsupportedOperationException
- always
-
reset
public void reset()
Resets this iterator back to the position at which the iterator was created.- Specified by:
reset
in interfaceResettableIterator
- Specified by:
reset
in interfaceResettableListIterator
- Since:
- Commons Collections 3.2
-
-