-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathIteration.java
59 lines (47 loc) · 1.59 KB
/
Iteration.java
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
package graph;
/* See restrictions in Graph.java. */
import java.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
*/
public abstract class Iteration<Type>
implements Iterator<Type>, Iterable<Type> {
@Override
public Iterator<Type> iterator() {
return this;
}
@Override
public void remove() {
throw new UnsupportedOperationException("remove not supported");
}
/** A wrapper class that turns an Iterator<TYPE> into an Iteration<TYPE>. */
private static class SimpleIteration<Type> extends Iteration<Type> {
/** ITER as an iteration. */
SimpleIteration(Iterator<Type> iter) {
_iter = iter;
}
@Override
public boolean hasNext() {
return _iter.hasNext();
}
@Override
public Type next() {
return _iter.next();
}
/** The iterator with which I was constructed. */
private Iterator<Type> _iter;
}
/** Returns an Iteration<TYPE> that delegates to IT. */
static <Type> Iteration<Type> iteration(Iterator<Type> it) {
return new SimpleIteration<>(it);
}
/** Returns an Iteration<TYPE> that delegates to ITERABLE. */
static <Type> Iteration<Type> iteration(Iterable<Type> iterable) {
return new SimpleIteration<>(iterable.iterator());
}
}