|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object javatools.datatypes.PeekIterator<T>
public abstract class PeekIterator<T>
This class is part of the Java Tools (see http://mpii.de/yago-naga/javatools).
It is licensed under the Creative Commons Attribution License
(see http://creativecommons.org/licenses/by/3.0) by
the YAGO-NAGA team (see http://mpii.de/yago-naga).
This class provides an Iterator that can look ahead. With the method peek(), you
can retrieve the next element without advancing the iterator.
Example:
PeekIterator i=new SimplePeekIterator(1,2,3,4); i.peek(); ---> 1 i.peek(); ---> 1 i.next(); ---> 1 i.peek(); ---> 2The 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
Nested Class Summary | |
---|---|
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 |
Field Summary | |
---|---|
boolean |
closed
TRUE if the iterator has been closed |
boolean |
initialized
TRUE if next has received its first value |
T |
next
Holds the next element (to be peeked) |
Constructor Summary | |
---|---|
PeekIterator()
|
Method Summary | ||
---|---|---|
java.util.List<T> |
asList()
Returns an arraylist of this iterator (killing this iterator) |
|
static
|
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
|
asSet(java.util.Iterator<T> i)
Returns a hashset of an iterator (killing the iterator) |
|
static
|
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
|
emptyIterator()
returns a constant empty iterator |
|
boolean |
hasNext()
TRUE if there are more elements to get with getNext |
|
java.util.Iterator<T> |
iterator()
returns this |
|
static
|
list(java.lang.Iterable<S> it)
Lists the elements in an iterable |
|
static
|
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
|
numElements(java.lang.Iterable<S> it)
Counts the number of elements in an iterable |
|
static
|
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
|
toString(java.lang.Iterable<S> it)
Lists the elements in an iterable |
|
static
|
toString(java.util.Iterator<S> it)
Lists the elements in an iterator (and destroys it) |
Methods inherited from class java.lang.Object |
---|
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
public T next
public boolean initialized
public boolean closed
Constructor Detail |
---|
public PeekIterator()
Method Detail |
---|
public final boolean hasNext()
hasNext
in interface java.util.Iterator<T>
public 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.Closeable
public 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.Object
public 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
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |