org.apache.stanbol.entityhub.servicesapi.util
Class AdaptingIterator<T,A>

java.lang.Object
  extended by org.apache.stanbol.entityhub.servicesapi.util.AdaptingIterator<T,A>
Type Parameters:
T - The type of the incoming elements
A - The type of the elements returned by this iterator
All Implemented Interfaces:
Iterator<A>
Direct Known Subclasses:
FilteringIterator, TextIterator, TypeSafeIterator

public class AdaptingIterator<T,A>
extends Object
implements Iterator<A>

Uses the parsed Adapter to convert values of type T to values of type A. If an instance of T can not be converted to A, than such values are filtered. This means that this implementation can be used for both filtering and converting of values of the base iterator. In fact the FilteringIterator is implemented based on this class.

Note that Iterator.remove() only works as long as Iterator.hasNext() was not called to determine if there are further elements. The reason for that is, that in order to filter elements of the parent iterator Iterator.next() has to be called to check weather any further element is valid against the used Filter. This call to Iterator.next() causes the parent Iterator to switch to the next element, meaning that after that the remove() method would delete a different element. To avoid that this Iterator throws an IllegalStateException in such cases. If the parent Iterator does not support remove() at all an UnsupportedOperationException is thrown.

Author:
Rupert Westenthaler

Nested Class Summary
static interface AdaptingIterator.Adapter<T,A>
          Adapts values of type T to values of type A.
 
Constructor Summary
AdaptingIterator(Iterator<T> it, AdaptingIterator.Adapter<T,A> adapter, Class<A> type)
          Constructs an instance based on an iterator of type T, an adapter and the target type
 
Method Summary
 boolean hasNext()
           
 A next()
           
protected  A prepareNext()
           
 void remove()
          This implementation of remove does have an additional restriction.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AdaptingIterator

public AdaptingIterator(Iterator<T> it,
                        AdaptingIterator.Adapter<T,A> adapter,
                        Class<A> type)
Constructs an instance based on an iterator of type T, an adapter and the target type

Parameters:
it - the base iterator
adapter - the adapter
type - the target type
Method Detail

hasNext

public final boolean hasNext()
Specified by:
hasNext in interface Iterator<A>

next

public final A next()
Specified by:
next in interface Iterator<A>

remove

public final void remove()
This implementation of remove does have an additional restriction. It is only able to remove the current element of the parent Iterator (parsed in the constructor) if hasNext() was not yet called. This is because hasNext() needs to call Iterator.next() on the parent iterator to check if there are further elements that can be adapted successfully. This causes that the current element of this Iterator (stored in an local variable) is no longer the current element of the parent iterator and therefore calls to remove() would delete an other object within the collection holding the elements used for this iteration. To prevent this this method throws an IllegalStateException ins such cases. Users of this method need therefore to ensure, that there are no calls to remove between a call to hasNext() and next() (what is not the case in typical use cases).

Specified by:
remove in interface Iterator<A>
See Also:
Iterator.remove()

prepareNext

protected A prepareNext()


Copyright © 2010-2013 The Apache Software Foundation. All Rights Reserved.