- java.lang.Object
-
- org.jgrapht.util.PrefetchIterator<E>
-
- Type Parameters:
E
- the element type
- All Implemented Interfaces:
Enumeration<E>
,Iterator<E>
public class PrefetchIterator<E> extends Object implements Iterator<E>, Enumeration<E>
Utility class to help implement an iterator/enumerator in which thehasNext()
method needs to calculate the next elements ahead of time.Many classes which implement an iterator face a common problem: if there is no easy way to calculate
hasNext()
other than to call getNext(), then they save the result for fetching in the next call to getNext(). This utility helps in doing just that.Usage: The new iterator class will hold this class as a member variable and forward the hasNext() and next() to it. When creating an instance of this class, you supply it with a functor that is doing the real job of calculating the next element.
//This class supplies enumeration of integer till 100. public class IteratorExample implements Enumeration{ private int counter=0; private PrefetchIterator nextSupplier; IteratorExample() { nextSupplier = new PrefetchIterator(new PrefetchIterator.NextElementFunctor(){ public Object nextElement() throws NoSuchElementException { counter++; if (counter <= 100) throw new NoSuchElementException(); else return new Integer(counter); } }); } // forwarding to nextSupplier and return its returned value public boolean hasMoreElements() { return this.nextSupplier.hasMoreElements(); } // forwarding to nextSupplier and return its returned value public Object nextElement() { return this.nextSupplier.nextElement(); } }
- Author:
- Assaf Lehr
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
PrefetchIterator.NextElementFunctor<EE>
A functor for the calculation of the next element.
-
Constructor Summary
Constructors Constructor Description PrefetchIterator(PrefetchIterator.NextElementFunctor<E> aEnum)
Construct a new prefetch iterator.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
hasMoreElements()
boolean
hasNext()
boolean
isEnumerationStartedEmpty()
Tests whether the enumeration started as an empty one.E
next()
E
nextElement()
void
remove()
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface java.util.Enumeration
asIterator
-
Methods inherited from interface java.util.Iterator
forEachRemaining
-
-
-
-
Constructor Detail
-
PrefetchIterator
public PrefetchIterator(PrefetchIterator.NextElementFunctor<E> aEnum)
Construct a new prefetch iterator.- Parameters:
aEnum
- the next element functor
-
-
Method Detail
-
nextElement
public E nextElement()
- Specified by:
nextElement
in interfaceEnumeration<E>
-
hasMoreElements
public boolean hasMoreElements()
- Specified by:
hasMoreElements
in interfaceEnumeration<E>
-
isEnumerationStartedEmpty
public boolean isEnumerationStartedEmpty()
Tests whether the enumeration started as an empty one. It does not matter if ithasMoreElements()
now, only at initialization time.Efficiency: if
nextElement()
,hasMoreElements()
were never used, it activates thehasMoreElements()
once. Else it is immediately $(O(1))$- Returns:
true
if the enumeration started as an empty one,false
otherwise.
-
remove
public void remove() throws UnsupportedOperationException
- Specified by:
remove
in interfaceIterator<E>
- Throws:
UnsupportedOperationException
-
-