public class MutableIterator<E> extends Object implements Iterator
Iterator
behaving like a FIFO queue. Adheres to the general
contract of the Iterator
interface, but offers methods to
safely append items to the iterator while the iteration is ongoing.
This iterator/queue does not support null
elements
(for performance reasons).
Constructor and Description |
---|
MutableIterator()
Creates a new instance as empty FIFO queue.
|
Modifier and Type | Method and Description |
---|---|
void |
add(E item)
Appends the offered element to the tail of this queue.
|
void |
addAll(Collection<? extends E> items)
Appends the offered elements to the tail of this queue (in the order
they are returned by their iterator).
|
boolean |
hasNext()
Returns
true if the iteration has more elements. |
E |
next()
Retrieves and removes the head of this queue.
|
void |
remove()
Always throws an
UnsupportedOperationException . |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
forEachRemaining
public MutableIterator()
hasNext()
returns false and next()
throws an
exception. Use add(Object)
to append items to the queue.public void add(E item)
NullPointerException
- iff item == null
public void addAll(Collection<? extends E> items)
NullPointerException
- if any of the elements are null
Collection.iterator()
public boolean hasNext()
true
if the iteration has more elements. (In other
words, returns true
if next would return an element rather
than throwing an exception.)
If this method returns false
, it means that the queue
is currently empty. Even after calls to hasNext()
or
next()
have returned false
or thrown
NoSuchElementException
s, respectively, it is allowed to
use add(Object)
and addAll()
to append some more
elements to the queue. At that point, this method will once again return
true
and calling next()
will work as expected.
public E next()
public void remove()
UnsupportedOperationException
.
Because of this class's queue-like character, a call to
next()
automatically removes the element returned from
the backing collection. (This happens internally, the collection(s)
used to initialise this instance are not affected.) The
Iterator.remove()
method being specified to remove
the last element previously returned by calling
next()
, calling this method would mean to remove it twice.
It throws an exception instead of just doing nothing for forward
compatibility.
remove
in interface Iterator
UnsupportedOperationException