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

public Class PushbackIterator<E>

extends Object
implements Iterator<E>
Class Inheritance
All Implemented Interfaces
java.util.Iterator
Imports
java.util.ArrayDeque, .Deque, .Iterator

Decorates an iterator to support pushback of elements.

The decorator stores the pushed back elements in a LIFO manner: the last element that has been pushed back, will be returned as the next element in a call to next().

The decorator does not support the removal operation. Any call to remove() will result in an UnsupportedOperationException.

Since
4.0

Field Summary

Modifier and TypeField and Description
private final Deque<E>
items

The LIFO queue containing the pushed back items.

private final Iterator<? extends E>
iterator

The iterator being decorated.

Constructor Summary

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

Constructor.

Method Summary

Modifier and TypeMethod and Description
public boolean
hasNext()

Implements java.util.Iterator.hasNext.

Returns true if the iteration has more elements.
public E
next()

Implements java.util.Iterator.next.

Returns the next element in the iteration.
public void
pushback(final E
the element to push back to the iterator
item
)

Push back the given element to the iterator.

public static <
the element type
E
>
PushbackIterator<E>

Returns:

a new peeking iterator
pushbackIterator
(final Iterator<? extends E>
the iterator to decorate
iterator
)

Decorates the specified iterator to support one-element lookahead.

public void
remove()

Overrides default java.util.Iterator.remove.

This iterator will always throw an UnsupportedOperationException.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

itemsback to summary
private final Deque<E> items

The LIFO queue containing the pushed back items.

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

The iterator being decorated.

Constructor Detail

PushbackIteratorback to summary
public PushbackIterator(final Iterator<? extends E> iterator)

Constructor.

Parameters
iterator:Iterator<? extends E>

the iterator to decorate

Method Detail

hasNextback to summary
public boolean hasNext()

Implements java.util.Iterator.hasNext.

Doc from java.util.Iterator.hasNext.

Returns true if the iteration has more elements. (In other words, returns true if next would return an element rather than throwing an exception.)

Returns:boolean

true if the iteration has more elements

Annotations
@Override
nextback to summary
public E next()

Implements java.util.Iterator.next.

Doc from java.util.Iterator.next.

Returns the next element in the iteration.

Returns:E

the next element in the iteration

Annotations
@Override
pushbackback to summary
public void pushback(final E item)

Push back the given element to the iterator.

Calling next() immediately afterwards will return exactly this element.

Parameters
item:E

the element to push back to the iterator

pushbackIteratorback to summary
public static <E> PushbackIterator<E> pushbackIterator(final Iterator<? extends E> iterator)

Decorates the specified iterator to support one-element lookahead.

If the iterator is already a PushbackIterator it is returned directly.

Parameters
<E>
the element type
iterator:Iterator<? extends E>

the iterator to decorate

Returns:PushbackIterator<E>

a new peeking iterator

Exceptions
NullPointerException:
if the iterator is null
removeback to summary
public void remove()

Overrides default java.util.Iterator.remove.

This iterator will always throw an UnsupportedOperationException.

Annotations
@Override
Exceptions
UnsupportedOperationException:
always