org.apache.hadoop.metrics2.util
Class TryIterator<T>

java.lang.Object
  extended by org.apache.hadoop.metrics2.util.TryIterator<T>
Type Parameters:
T - the type of the iterator
All Implemented Interfaces:
Iterator<T>

public abstract class TryIterator<T>
extends Object
implements Iterator<T>

A base class for unmodifiable iterators (throws on remove) This class also makes writing filtering iterators easier, where the only way to discover the end of data is by trying to read it. The same applies to writing iterator wrappers around stream read calls. One only needs to implement the tryNext() method and call done() when done.


Constructor Summary
TryIterator()
           
 
Method Summary
 T current()
           
protected  T done()
          Implementations of tryNext() must call this method when there are no more elements left in the iteration.
 boolean hasNext()
           
 T next()
           
 void remove()
          Guaranteed to throw UnsupportedOperationException
protected abstract  T tryNext()
          Return the next element.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TryIterator

public TryIterator()
Method Detail

tryNext

protected abstract T tryNext()
Return the next element. Must call done() when done, otherwise infinite loop could occur. If this method throws an exception, any further attempts to use the iterator would result in an IllegalStateException.

Returns:
the next element if there is one or return done()

done

protected final T done()
Implementations of tryNext() must call this method when there are no more elements left in the iteration.

Returns:
null as a convenience to implement tryNext()

hasNext

public final boolean hasNext()
Specified by:
hasNext in interface Iterator<T>
Returns:
true if we have a next element or false otherwise.

next

public final T next()
Specified by:
next in interface Iterator<T>
Returns:
the next element if we have one.

current

public final T current()
Returns:
the current element without advancing the iterator

remove

public final void remove()
Guaranteed to throw UnsupportedOperationException

Specified by:
remove in interface Iterator<T>


Copyright © 2009 The Apache Software Foundation