- Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathIteration.java
More file actions
Latest commit
59 lines (47 loc) · 1.59 KB
/
Copy pathIteration.java
File metadata and controls
59 lines (47 loc) · 1.59 KB
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
packagegraph;
/* See restrictions in Graph.java. */
importjava.util.Iterator;
/** An Iteration<TYPE> is an Iterator<TYPE> that may also be used in a foreach
* loop. That is, it implements the Interable<TYPE> interface by simply
* returning itself. For example, this allows one to write
* for (int[] e: G.edges()) {
* ...
* }
* @author P. N. Hilfinger
*/
publicabstractclassIteration<Type>
implementsIterator<Type>, Iterable<Type> {
@Override
publicIterator<Type> iterator() {
returnthis;
}
@Override
publicvoidremove() {
thrownewUnsupportedOperationException("remove not supported");
}
/** A wrapper class that turns an Iterator<TYPE> into an Iteration<TYPE>. */
privatestaticclassSimpleIteration<Type> extendsIteration<Type> {
/** ITER as an iteration. */
SimpleIteration(Iterator<Type> iter) {
_iter = iter;
}
@Override
publicbooleanhasNext() {
return_iter.hasNext();
}
@Override
publicTypenext() {
return_iter.next();
}
/** The iterator with which I was constructed. */
privateIterator<Type> _iter;
}
/** Returns an Iteration<TYPE> that delegates to IT. */
static <Type> Iteration<Type> iteration(Iterator<Type> it) {
returnnewSimpleIteration<>(it);
}
/** Returns an Iteration<TYPE> that delegates to ITERABLE. */
static <Type> Iteration<Type> iteration(Iterable<Type> iterable) {
returnnewSimpleIteration<>(iterable.iterator());
}
}