1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.apache.commons.functor.generator;
16
17 import org.apache.commons.functor.UnaryProcedure;
18
19 import java.util.Iterator;
20
21
22
23
24
25
26
27
28
29 public final class IteratorToGeneratorAdapter<E> extends BaseGenerator<E> {
30
31
32
33 private final Iterator<? extends E> iter;
34
35
36
37
38
39
40
41 public IteratorToGeneratorAdapter(Iterator<? extends E> iter) {
42 if (null == iter) {
43 throw new IllegalArgumentException("Iterator argument was null");
44 }
45 this.iter = iter;
46 }
47
48
49
50
51
52
53 public void run(UnaryProcedure<? super E> proc) {
54 while (iter.hasNext()) {
55 proc.run(iter.next());
56 if (isStopped()) {
57 break;
58 }
59 }
60 }
61
62
63
64
65 public boolean equals(Object obj) {
66 if (obj == this) {
67 return true;
68 }
69 if (!(obj instanceof IteratorToGeneratorAdapter<?>)) {
70 return false;
71 }
72 IteratorToGeneratorAdapter<?> that = (IteratorToGeneratorAdapter<?>) obj;
73 return this.iter.equals(that.iter);
74 }
75
76
77
78
79 public int hashCode() {
80 int hash = "IteratorToGeneratorAdapater".hashCode();
81 hash <<= 2;
82 hash ^= iter.hashCode();
83 return hash;
84 }
85
86
87
88
89 public String toString() {
90 return "IteratorToGeneratorAdapter<" + iter + ">";
91 }
92
93
94
95
96
97
98
99
100 public static <E> IteratorToGeneratorAdapter<E> adapt(Iterator<? extends E> iter) {
101 return null == iter ? null : new IteratorToGeneratorAdapter<E>(iter);
102 }
103
104 }