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 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
68 T returnObj = this.next;
69 this.next = null;
70 return returnObj;
71 }
72
73
74
75
76 public void remove() {
77 throw new UnsupportedOperationException();
78 }
79 }