public abstract class PeekIterator<T>
extends java.lang.Object
implements java.util.Iterator<T>, java.lang.Iterable<T>, java.io.Closeable
PeekIterator i=new SimplePeekIterator(1,2,3,4);
i.peek();
---> 1
i.peek();
---> 1
i.next();
---> 1
i.peek();
---> 2
The class is also suited to create an Interator by overriding. The only method that
needs top be overwritten is "internalNext()".
// An iterator over the numbers 0,1,2
PeekIterator it=new PeekIterator() {
int counter=0;
// Returns null if there are no more elements
protected Integer internalNext() throws Exception {
if(counter==3) return(null);
return(counter++);
}
};
for(Integer i : it) D.p(i);
--->
0
1
2
| Modifier and Type | Class and Description |
|---|---|
static class |
PeekIterator.ElementaryPeekIterator<T>
A Peek iterator with one single element
|
static class |
PeekIterator.SimplePeekIterator<T>
A PeekIterator that can iterate over another iterator or over a list of elements
|
| Modifier and Type | Field and Description |
|---|---|
boolean |
closed
TRUE if the iterator has been closed
|
protected static PeekIterator<java.lang.Object> |
EMPTY
An empty PeekIterator
|
boolean |
initialized
TRUE if next has received its first value
|
T |
next
Holds the next element (to be peeked)
|
| Constructor and Description |
|---|
PeekIterator() |
| Modifier and Type | Method and Description |
|---|---|
java.util.List<T> |
asList()
Returns an arraylist of this iterator (killing this iterator)
|
static <T> java.util.List<T> |
asList(java.util.Iterator<T> i)
Returns an arraylist of an iterator (killing the iterator)
|
java.util.Set<T> |
asSet()
Returns a hashset of this iterator (killing this iterator)
|
static <T> java.util.Set<T> |
asSet(java.util.Iterator<T> i)
Returns a hashset of an iterator (killing the iterator)
|
static <T> java.util.Set<T> |
asSet(java.util.Iterator<T> i,
java.util.Set<T> set)
Fills the elements of an iterator into a given set (killing the iterator)
|
void |
close()
Closes the underlying resource
|
static <K> PeekIterator<K> |
emptyIterator()
returns a constant empty iterator
|
boolean |
hasNext()
TRUE if there are more elements to get with getNext
|
protected abstract T |
internalNext()
Returns the next or NULL if no next element is available
|
protected T |
internalSilentNext()
Wraps the Exceptions of internalNext into RuntimeExceptions
|
java.util.Iterator<T> |
iterator()
returns this
|
static <S> java.util.List<S> |
list(java.lang.Iterable<S> it)
Lists the elements in an iterable
|
static <S> java.util.List<S> |
list(java.util.Iterator<S> it)
Lists the elements in an iterator (and destroys it)
|
static void |
main(java.lang.String[] args)
test routine
|
T |
next()
Returns the next element and advances.
|
T |
nextOrNull()
Returns the next element and advances.
|
static <S> int |
numElements(java.lang.Iterable<S> it)
Counts the number of elements in an iterable
|
static <S> int |
numElements(java.util.Iterator<S> it)
Counts the number of elements in an iterator (and destroys it)
|
T |
peek()
returns the next element without advancing
|
void |
remove()
Removes the current element, if supported by the underlying iterator
|
java.lang.String |
toString() |
static <S> java.lang.StringBuilder |
toString(java.lang.Iterable<S> it)
Lists the elements in an iterable
|
static <S> java.lang.StringBuilder |
toString(java.util.Iterator<S> it)
Lists the elements in an iterator (and destroys it)
|
public T next
public boolean initialized
public boolean closed
protected static PeekIterator<java.lang.Object> EMPTY
public final boolean hasNext()
hasNext in interface java.util.Iterator<T>protected final T internalSilentNext()
protected abstract T internalNext() throws java.lang.Exception
java.lang.Exceptionpublic final T next()
next in interface java.util.Iterator<T>public final T nextOrNull()
public void remove()
remove in interface java.util.Iterator<T>public final T peek()
public java.util.Iterator<T> iterator()
iterator in interface java.lang.Iterable<T>public void close()
close in interface java.io.Closeableclose in interface java.lang.AutoCloseablepublic static <T> java.util.List<T> asList(java.util.Iterator<T> i)
public java.util.List<T> asList()
public static <T> java.util.Set<T> asSet(java.util.Iterator<T> i,
java.util.Set<T> set)
public static <T> java.util.Set<T> asSet(java.util.Iterator<T> i)
public java.util.Set<T> asSet()
public java.lang.String toString()
toString in class java.lang.Objectpublic static <K> PeekIterator<K> emptyIterator()
public static <S> int numElements(java.util.Iterator<S> it)
public static <S> int numElements(java.lang.Iterable<S> it)
public static <S> java.lang.StringBuilder toString(java.lang.Iterable<S> it)
public static <S> java.lang.StringBuilder toString(java.util.Iterator<S> it)
public static <S> java.util.List<S> list(java.lang.Iterable<S> it)
public static <S> java.util.List<S> list(java.util.Iterator<S> it)
public static void main(java.lang.String[] args)
throws java.lang.Exception
java.lang.Exception