Top Description Fields Constructors Methods
org.apache.commons.collections4.iterators

public Class ListIteratorWrapper<E>

extends Object
implements ResettableListIterator<E>
Class Inheritance
All Implemented Interfaces
org.apache.commons.collections4.ResettableListIterator, org.apache.commons.collections4.OrderedIterator, java.util.Iterator, org.apache.commons.collections4.ResettableIterator, java.util.ListIterator
Imports
java.text.MessageFormat, java.util.ArrayList, .Iterator, .List, .ListIterator, .NoSuchElementException, org.apache.commons.collections4.ResettableListIterator

Converts an Iterator into a ResettableListIterator. For plain Iterators this is accomplished by caching the returned elements. This class can also be used to simply add ResettableIterator functionality to a given ListIterator.

The ListIterator interface has additional useful methods for navigation - previous() and the index methods. This class allows a regular Iterator to behave as a ListIterator. It achieves this by building a list internally of as the underlying iterator is traversed.

The optional operations of ListIterator are not supported for plain Iterators.

This class implements ResettableListIterator from Commons Collections 3.2.

Since
2.1

Field Summary

Modifier and TypeField and Description
private static final String
CANNOT_REMOVE_MESSAGE

Message used when set or add are called.

private int
currentIndex

The current index of this iterator.

private final Iterator<? extends E>
iterator

The underlying iterator being decorated.

private final List<E>
list

The list being used to cache the iterator.

private boolean
removeState

recall whether the wrapped iterator's "cursor" is in such a state as to allow remove() to be called

private static final String
UNSUPPORTED_OPERATION_MESSAGE

Message used when set or add are called.

private int
wrappedIteratorIndex

The current index of the wrapped iterator.

Constructor Summary

AccessConstructor and Description
public
ListIteratorWrapper(final Iterator<? extends E>
the iterator to wrap
iterator
)

Constructs a new ListIteratorWrapper that will wrap the given iterator.

Method Summary

Modifier and TypeMethod and Description
public void
add(final E
the object to add
obj
)

Implements java.util.ListIterator.add.

Throws UnsupportedOperationException unless the underlying Iterator is a ListIterator.
public boolean

Returns:

true if there are more elements
hasNext
()

Implements java.util.ListIterator.hasNext, java.util.Iterator.hasNext.

Returns true if there are more elements in the iterator.
public boolean

Returns:

true if there are previous elements
hasPrevious
()

Implements java.util.ListIterator.hasPrevious, org.apache.commons.collections4.OrderedIterator.hasPrevious.

Returns true if there are previous elements in the iterator.
public E

Returns:

the next element from the iterator
next
()

Implements java.util.ListIterator.next, java.util.Iterator.next.

Returns the next element from the iterator.
public int

Returns:

the index of the next element
nextIndex
()

Implements java.util.ListIterator.nextIndex.

Returns the index of the next element.
public E

Returns:

the previous element
previous
()

Implements java.util.ListIterator.previous, org.apache.commons.collections4.OrderedIterator.previous.

Returns the previous element.
public int

Returns:

the index of the previous element
previousIndex
()

Implements java.util.ListIterator.previousIndex.

Returns the index of the previous element.
public void
remove()

Overrides default java.util.Iterator.remove.

Implements java.util.ListIterator.remove.

Throws UnsupportedOperationException if previous() has ever been called.
public void
reset()

Implements org.apache.commons.collections4.ResettableIterator.reset.

Resets this iterator back to the position at which the iterator was created.
public void
set(final E
the object to set
obj
)

Implements java.util.ListIterator.set.

Throws UnsupportedOperationException unless the underlying Iterator is a ListIterator.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

CANNOT_REMOVE_MESSAGEback to summary
private static final String CANNOT_REMOVE_MESSAGE

Message used when set or add are called.

currentIndexback to summary
private int currentIndex

The current index of this iterator.

iteratorback to summary
private final Iterator<? extends E> iterator

The underlying iterator being decorated.

listback to summary
private final List<E> list

The list being used to cache the iterator.

removeStateback to summary
private boolean removeState

recall whether the wrapped iterator's "cursor" is in such a state as to allow remove() to be called

UNSUPPORTED_OPERATION_MESSAGEback to summary
private static final String UNSUPPORTED_OPERATION_MESSAGE

Message used when set or add are called.

wrappedIteratorIndexback to summary
private int wrappedIteratorIndex

The current index of the wrapped iterator.

Constructor Detail

ListIteratorWrapperback to summary
public ListIteratorWrapper(final Iterator<? extends E> iterator)

Constructs a new ListIteratorWrapper that will wrap the given iterator.

Parameters
iterator:Iterator<? extends E>

the iterator to wrap

Exceptions
NullPointerException:
if the iterator is null

Method Detail

addback to summary
public void add(final E obj) throws UnsupportedOperationException

Implements java.util.ListIterator.add.

Throws UnsupportedOperationException unless the underlying Iterator is a ListIterator.

Parameters
obj:E

the object to add

Annotations
@Override
Exceptions
UnsupportedOperationException:
if the underlying iterator is not of type ListIterator
hasNextback to summary
public boolean hasNext()

Implements java.util.ListIterator.hasNext, java.util.Iterator.hasNext.

Returns true if there are more elements in the iterator.

Returns:boolean

true if there are more elements

Annotations
@Override
hasPreviousback to summary
public boolean hasPrevious()

Implements java.util.ListIterator.hasPrevious, org.apache.commons.collections4.OrderedIterator.hasPrevious.

Returns true if there are previous elements in the iterator.

Returns:boolean

true if there are previous elements

Annotations
@Override
nextback to summary
public E next() throws NoSuchElementException

Implements java.util.ListIterator.next, java.util.Iterator.next.

Returns the next element from the iterator.

Returns:E

the next element from the iterator

Annotations
@Override
Exceptions
NoSuchElementException:
if there are no more elements
nextIndexback to summary
public int nextIndex()

Implements java.util.ListIterator.nextIndex.

Returns the index of the next element.

Returns:int

the index of the next element

Annotations
@Override
previousback to summary
public E previous() throws NoSuchElementException

Implements java.util.ListIterator.previous, org.apache.commons.collections4.OrderedIterator.previous.

Returns the previous element.

Returns:E

the previous element

Annotations
@Override
Exceptions
NoSuchElementException:
if there are no previous elements
previousIndexback to summary
public int previousIndex()

Implements java.util.ListIterator.previousIndex.

Returns the index of the previous element.

Returns:int

the index of the previous element

Annotations
@Override
removeback to summary
public void remove() throws UnsupportedOperationException

Overrides default java.util.Iterator.remove.

Implements java.util.ListIterator.remove.

Throws UnsupportedOperationException if previous() has ever been called.

Annotations
@Override
Exceptions
UnsupportedOperationException:
always
resetback to summary
public void reset()

Implements org.apache.commons.collections4.ResettableIterator.reset.

Resets this iterator back to the position at which the iterator was created.

Annotations
@Override
Since
3.2
setback to summary
public void set(final E obj) throws UnsupportedOperationException

Implements java.util.ListIterator.set.

Throws UnsupportedOperationException unless the underlying Iterator is a ListIterator.

Parameters
obj:E

the object to set

Annotations
@Override
Exceptions
UnsupportedOperationException:
if the underlying iterator is not of type ListIterator