litesoft
@ 938
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
// This Source Code is in the Public Domain per: http://litesoft.org/License.txt package org.litesoft.core.util; import java.util.*; /** * An Iterator, of another wrapped Iterator, that adds the ability to * <i>pushBack</i> (or repeat) any ONE object just retrieved via * <i>next</i>.<p> * * @author George Smith * @version 1.0 7/28/01 */ public final class OnePushBackIterator<T> extends AbstractWrappingIterator<T> { /** * Construct an Iterator (by wrapping another Iterator) that adds the * ability of a one-deep push back.<p> * * @param pIterator the wrapped Iterator (!null). */ public OnePushBackIterator( Iterator<T> pIterator ) throws NullPointerException { super( pIterator ); } private T lastValue = null; private boolean lastValid = false; private boolean lastEverSet = false; /** * Flag <i>this</i> to return from next() the last thing (Onject) that * next() returned. * * @return an <tt>true/false</tt> indicating if the puch-back succeeded.<p> */ public boolean pushBack() { return (lastEverSet && !lastValid) ? (lastValid = true) : false; // Note Assignment! } /** * Returns true if the iteration has more elements. * * @see <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/Util/Iterator.html#hasNext()">java.util.Iterator#hasNext()</a> */ @Override public boolean hasNext() { return lastValid || super.hasNext(); } /** * Returns the next element in the interation. * * @see <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/Util/Iterator.html#next()">java.util.Iterator#next()</a> */ @Override public T next() { if ( !hasNext() ) { super.next(); // throw exception } if ( lastValid ) { lastValid = false; return lastValue; } lastValue = super.next(); lastEverSet = true; return lastValue; } } |