javatools.datatypes
Class PeekIterator<T>

java.lang.Object
  extended by javatools.datatypes.PeekIterator<T>
All Implemented Interfaces:
java.io.Closeable, java.lang.Iterable<T>, java.util.Iterator<T>
Direct Known Subclasses:
CSVLines, DeepFileSet, FileLines, FilteredIterator, MatchReader, PeekIterator.ElementaryPeekIterator, PeekIterator.SimplePeekIterator, ResultIterator

public abstract class PeekIterator<T>
extends java.lang.Object
implements java.util.Iterator<T>, java.lang.Iterable<T>, java.io.Closeable

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();
    ---> 2    

The class is also suited to create an Interator by overriding. The only method that needs top be overwritten is "internalNext()".
Example:
    // 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
<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
 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)
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

next

public T next
Holds the next element (to be peeked)


initialized

public boolean initialized
TRUE if next has received its first value


closed

public boolean closed
TRUE if the iterator has been closed

Constructor Detail

PeekIterator

public PeekIterator()
Method Detail

hasNext

public final boolean hasNext()
TRUE if there are more elements to get with getNext

Specified by:
hasNext in interface java.util.Iterator<T>

next

public final T next()
Returns the next element and advances. Overwrite internalNext instead!

Specified by:
next in interface java.util.Iterator<T>

nextOrNull

public final T nextOrNull()
Returns the next element and advances. Overwrite internalNext instead!


remove

public void remove()
Removes the current element, if supported by the underlying iterator

Specified by:
remove in interface java.util.Iterator<T>

peek

public final T peek()
returns the next element without advancing


iterator

public java.util.Iterator<T> iterator()
returns this

Specified by:
iterator in interface java.lang.Iterable<T>

close

public void close()
Closes the underlying resource

Specified by:
close in interface java.io.Closeable

asList

public static <T> java.util.List<T> asList(java.util.Iterator<T> i)
Returns an arraylist of an iterator (killing the iterator)


asList

public java.util.List<T> asList()
Returns an arraylist of this iterator (killing this iterator)


asSet

public 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)


asSet

public static <T> java.util.Set<T> asSet(java.util.Iterator<T> i)
Returns a hashset of an iterator (killing the iterator)


asSet

public java.util.Set<T> asSet()
Returns a hashset of this iterator (killing this iterator)


toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

emptyIterator

public static <K> PeekIterator<K> emptyIterator()
returns a constant empty iterator


numElements

public static <S> int numElements(java.util.Iterator<S> it)
Counts the number of elements in an iterator (and destroys it)


numElements

public static <S> int numElements(java.lang.Iterable<S> it)
Counts the number of elements in an iterable


toString

public static <S> java.lang.StringBuilder toString(java.lang.Iterable<S> it)
Lists the elements in an iterable


toString

public static <S> java.lang.StringBuilder toString(java.util.Iterator<S> it)
Lists the elements in an iterator (and destroys it)


list

public static <S> java.util.List<S> list(java.lang.Iterable<S> it)
Lists the elements in an iterable


list

public static <S> java.util.List<S> list(java.util.Iterator<S> it)
Lists the elements in an iterator (and destroys it)


main

public static void main(java.lang.String[] args)
                 throws java.lang.Exception
test routine

Throws:
java.lang.Exception