org.javimmutable.collections.cursors
Class AbstractStartedCursor<T>
java.lang.Object
org.javimmutable.collections.cursors.AbstractStartedCursor<T>
- Type Parameters:
T
-
- All Implemented Interfaces:
- Iterable<T>, Cursor<T>
@Immutable
public abstract class AbstractStartedCursor<T>
- extends Object
- implements Cursor<T>
Base class for started (i.e. actively traversing the collection) Cursors.
Derived classes usually only need to override next() and getValue().
Once the end of the traversal is reached returning super.next() will
end the traversal.
Method Summary |
boolean |
hasValue()
Read-only method with no side effects that determines if the Cursor currently has a value. |
Iterator<T> |
iterator()
|
Cursor<T> |
next()
Advances to the next (possibly first) value. |
Cursor<T> |
start()
All Cursors are created in a pre-start position pointing "before" the first element. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Methods inherited from interface org.javimmutable.collections.Cursor |
getValue |
AbstractStartedCursor
public AbstractStartedCursor()
start
@Nonnull
public Cursor<T> start()
- Description copied from interface:
Cursor
- All Cursors are created in a pre-start position pointing "before" the first element. Once traversal has begun a
Cursor points to some element in the collection or to end ("after" the last element). The start() method
advances to the first element if traversal has not yet started or does nothing if traversal has
already started. Either next() or start() can be used to initiate a traversal however start() is
safer since it can be used safely on already started cursors as well as not-started ones. This distinction
is useful when passing a Cursor as parameter to a method that will traverse from the Cursor's current position
forward and using start() prevents it from skipping the current value.
Must always return a non-null Cursor.
- Specified by:
start
in interface Cursor<T>
- Returns:
- Cursor for first position or this if already started
next
@Nonnull
public Cursor<T> next()
- Description copied from interface:
Cursor
- Advances to the next (possibly first) value. Must always return a non-null Cursor.
A newly created Cursor must always point to "before" the first value because next() (or start()) must
always be called once before retrieving the first value. If the Cursor is already at the end
of its sequence then it should return a Cursor that will always return false for hasValue().
- Specified by:
next
in interface Cursor<T>
- Returns:
- Cursor for next position
hasValue
public boolean hasValue()
- Description copied from interface:
Cursor
- Read-only method with no side effects that determines if the Cursor currently has a value.
Users of the Cursor will always call this after calling next() to see if they have reached
the end of the sequence. If hasValue() returns true then next() will be called. If hasValue()
returns false then next() must not be called.
- Specified by:
hasValue
in interface Cursor<T>
- Returns:
- true iff getValue() can be called
iterator
public Iterator<T> iterator()
- Specified by:
iterator
in interface Iterable<T>
Copyright © 2014 Burton Computer Corporation. All rights reserved.