01: /* TransformingIteratorWrapper
02: *
03: * $Id: LookaheadIterator.java 4650 2006-09-25 18:09:42Z paul_jack $
04: *
05: * Created on Mar 25, 2005
06: *
07: * Copyright (C) 2005 Internet Archive.
08: *
09: * This file is part of the Heritrix web crawler (crawler.archive.org).
10: *
11: * Heritrix is free software; you can redistribute it and/or modify
12: * it under the terms of the GNU Lesser Public License as published by
13: * the Free Software Foundation; either version 2.1 of the License, or
14: * any later version.
15: *
16: * Heritrix is distributed in the hope that it will be useful,
17: * but WITHOUT ANY WARRANTY; without even the implied warranty of
18: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19: * GNU Lesser Public License for more details.
20: *
21: * You should have received a copy of the GNU Lesser Public License
22: * along with Heritrix; if not, write to the Free Software
23: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24: */
25: package org.archive.util.iterator;
26:
27: import java.util.Iterator;
28: import java.util.NoSuchElementException;
29:
30: /**
31: * Superclass for Iterators which must probe ahead to know if
32: * a 'next' exists, and thus have a cached next between a call
33: * to hasNext() and next().
34: *
35: * @author gojomo
36: *
37: */
38: public abstract class LookaheadIterator<T> implements Iterator<T> {
39: protected T next;
40:
41: /**
42: * Test whether any items remain; loads next item into
43: * holding 'next' field.
44: *
45: * @see java.util.Iterator#hasNext()
46: */
47: public boolean hasNext() {
48: return (this .next != null) ? true : lookahead();
49: }
50:
51: /**
52: * Caches the next item if available.
53: *
54: * @return true if there was a next item to cache, false otherwise
55: */
56: protected abstract boolean lookahead();
57:
58: /**
59: * Return the next item.
60: *
61: * @see java.util.Iterator#next()
62: */
63: public T next() {
64: if (!hasNext()) {
65: throw new NoSuchElementException();
66: }
67: // 'next' is guaranteed non-null by a hasNext() which returned true
68: T returnObj = this .next;
69: this .next = null;
70: return returnObj;
71: }
72:
73: /* (non-Javadoc)
74: * @see java.util.Iterator#remove()
75: */
76: public void remove() {
77: throw new UnsupportedOperationException();
78: }
79: }
|