Package com.complexible.common.collect
Class AbstractSkippingIterator<T>
- java.lang.Object
-
- com.complexible.common.collect.AbstractSkippingIterator<T>
-
- All Implemented Interfaces:
CloseableIterator<T>
,PeekingSkippingIterator<T>
,ResettableIterator<T>
,SkippingIterator<T>
,com.google.common.collect.PeekingIterator<T>
,java.lang.AutoCloseable
,java.util.Iterator<T>
- Direct Known Subclasses:
MarkResetSkippingIterator
,SpoolingMarkResetIterator
public abstract class AbstractSkippingIterator<T> extends java.lang.Object implements PeekingSkippingIterator<T>, CloseableIterator<T>
Abstract
SkippingIterator
implementation to help with concrete implementations. The remove function of this implementation throwsUnsupportedOperationException
and its size estimate isunknown
.- Version:
- 2.2.1
- Author:
- Evren Sirin, Pavel Klinov, Michael Grove
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface com.complexible.common.base.CloseableIterator
CloseableIterator.AbstractCloseableIterator<T>, CloseableIterator.DelegatingCloseableIterator<T>, CloseableIterator.EmptyCloseableIterator<T>
-
-
Field Summary
Fields Modifier and Type Field Description protected java.util.Comparator<T>
mComparator
The comparator which defines the order for skipping-
Fields inherited from interface com.complexible.common.collect.SkippingIterator
UNKNOWN_SIZE
-
-
Constructor Summary
Constructors Constructor Description AbstractSkippingIterator(java.util.Comparator<T> theComparator)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected void
assertOpen()
void
close()
java.util.Comparator<T>
comparator()
Returns the comparator for this iterator that specifies the order in which the elements are returned.protected abstract T
computeNext()
Return the next element to be returned by the iteratorprotected abstract T
computeSkipTo(T theTarget)
Returns the element that this iteration has skipped to ornull
if it ran off the end.protected T
computeSkipTo(T theTarget, T theBound)
protected T
endOfData()
Signal that you're done iterating.long
estimatedSize()
Returns the estimated size of this iterator without advancing the iterator orSkippingIterator.UNKNOWN_SIZE
if the estimate is not known.boolean
hasNext()
protected boolean
isClosed()
boolean
isLessOrEqualThan(T theElement, T theBound)
T
next()
T
peek()
Implementation of peeking to the next item returned by the iterator.protected abstract void
performReset()
void
remove()
void
reset()
Resets the iterator to the beginning so the followingIterator.next()
call will return the first element in this iteration.protected void
resetState()
T
skipTo(T theElement)
Returns the element which is equal or greater to theElement based on the ordering of the elements in the underlying data source, i.e., for two elements A, B s.t.T
skipTo(T theElement, T theBound)
Same contract asSkippingIterator.skipTo(Object)
but skipping is bounded: if the next element that is greater than or equal to the target is greater than the bound, the method returns null.protected void
validateSkipTarget(T theTarget)
-
-
-
Field Detail
-
mComparator
protected final java.util.Comparator<T> mComparator
The comparator which defines the order for skipping
-
-
Constructor Detail
-
AbstractSkippingIterator
public AbstractSkippingIterator(java.util.Comparator<T> theComparator)
-
-
Method Detail
-
comparator
public java.util.Comparator<T> comparator()
Returns the comparator for this iterator that specifies the order in which the elements are returned.- Specified by:
comparator
in interfaceSkippingIterator<T>
-
estimatedSize
public long estimatedSize()
Returns the estimated size of this iterator without advancing the iterator orSkippingIterator.UNKNOWN_SIZE
if the estimate is not known. The estimate is for the whole iterator and will not change after the iterator is advanced. There is no guarantee about the accuracy of the estimate.- Specified by:
estimatedSize
in interfaceSkippingIterator<T>
-
remove
public void remove()
-
computeNext
protected abstract T computeNext()
Return the next element to be returned by the iterator- Returns:
- the next element
-
computeSkipTo
protected abstract T computeSkipTo(T theTarget)
Returns the element that this iteration has skipped to ornull
if it ran off the end.- Parameters:
theTarget
- the skipping target- Returns:
- the first element that is greater or equal to the skipping
target, or
null
if it doesn't exist.
-
next
public final T next()
-
endOfData
protected final T endOfData()
Signal that you're done iterating.- Returns:
- nothing, the result is ignored
-
hasNext
public final boolean hasNext()
- Specified by:
hasNext
in interfacejava.util.Iterator<T>
-
close
public void close()
- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfaceCloseableIterator<T>
-
peek
public final T peek()
Implementation of peeking to the next item returned by the iterator. Anyone wishing to expose this method simply need to also implement thePeekingIterator
interface.- Specified by:
peek
in interfacecom.google.common.collect.PeekingIterator<T>
- Returns:
- the next element to be returned
-
skipTo
public final T skipTo(T theElement)
Description copied from interface:SkippingIterator
Returns the element which is equal or greater to theElement based on the ordering of the elements in the underlying data source, i.e., for two elements A, B s.t. B comes after A during iteration, theComparator should returnA <= B
. Note that skips are always in the forward direction. iftheElement
compares to less than the last returned element (or less than the bounding start of the iterator), then no skip will occur, and the returned value is the same as if `next()` was called.- Specified by:
skipTo
in interfaceSkippingIterator<T>
- Returns:
- the smallest element
A
s.t.A <= theElement
, iftheElement > last seen element
, or the output ofnext()
if not.
-
skipTo
public final T skipTo(T theElement, T theBound)
Same contract asSkippingIterator.skipTo(Object)
but skipping is bounded: if the next element that is greater than or equal to the target is greater than the bound, the method returns null. Implementations are free to not move their internal cursor beyond the bound which may enable them to make less work or skip IO. However, they must maintain their internal state to ensure that the smallest element that is greater than theBound is not consumed and is returned for the nextIterator.next()
call.- Specified by:
skipTo
in interfaceSkippingIterator<T>
- Parameters:
theElement
- the targettheBound
- the bound. null means no upper bound.- Returns:
- the smallest element that's greater than or equal to the target but not greater than the bound.
-
validateSkipTarget
protected void validateSkipTarget(T theTarget)
-
reset
public final void reset()
Resets the iterator to the beginning so the followingIterator.next()
call will return the first element in this iteration.- Specified by:
reset
in interfaceResettableIterator<T>
-
isClosed
protected boolean isClosed()
-
assertOpen
protected void assertOpen()
-
resetState
protected final void resetState()
-
performReset
protected abstract void performReset()
-
-