Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
IteratorToGeneratorAdapter |
|
| 2.5;2.5 |
1 | /* | |
2 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
3 | * you may not use this file except in compliance with the License. | |
4 | * You may obtain a copy of the License at | |
5 | * | |
6 | * http://www.apache.org/licenses/LICENSE-2.0 | |
7 | * | |
8 | * Unless required by applicable law or agreed to in writing, software | |
9 | * distributed under the License is distributed on an "AS IS" BASIS, | |
10 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
11 | * See the License for the specific language governing permissions and | |
12 | * limitations under the License. | |
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 | * Adapts an {@link Iterator} to the {@link Generator} interface. | |
23 | * | |
24 | * @since 1.0 | |
25 | * @version $Revision: 1156798 $ $Date: 2011-08-11 22:11:07 +0200 (Thu, 11 Aug 2011) $ | |
26 | * @author Jason Horman (jason@jhorman.org) | |
27 | * @author Rodney Waldhoff | |
28 | */ | |
29 | public final class IteratorToGeneratorAdapter<E> extends BaseGenerator<E> { | |
30 | // instance variables | |
31 | //----------------------------------------------------- | |
32 | ||
33 | private final Iterator<? extends E> iter; | |
34 | ||
35 | // constructors | |
36 | //----------------------------------------------------- | |
37 | /** | |
38 | * Create a new IteratorToGeneratorAdapter. | |
39 | * @param iter Iterator to adapt | |
40 | */ | |
41 | 86 | public IteratorToGeneratorAdapter(Iterator<? extends E> iter) { |
42 | 86 | if (null == iter) { |
43 | 2 | throw new IllegalArgumentException("Iterator argument was null"); |
44 | } | |
45 | 84 | this.iter = iter; |
46 | 84 | } |
47 | ||
48 | // instance methods | |
49 | //----------------------------------------------------- | |
50 | /** | |
51 | * {@inheritDoc} | |
52 | */ | |
53 | public void run(UnaryProcedure<? super E> proc) { | |
54 | 532 | while (iter.hasNext()) { |
55 | 478 | proc.run(iter.next()); |
56 | 478 | if (isStopped()) { |
57 | 8 | break; |
58 | } | |
59 | } | |
60 | 62 | } |
61 | ||
62 | /** | |
63 | * {@inheritDoc} | |
64 | */ | |
65 | public boolean equals(Object obj) { | |
66 | 24 | if (obj == this) { |
67 | 8 | return true; |
68 | } | |
69 | 16 | if (!(obj instanceof IteratorToGeneratorAdapter<?>)) { |
70 | 4 | return false; |
71 | } | |
72 | 12 | IteratorToGeneratorAdapter<?> that = (IteratorToGeneratorAdapter<?>) obj; |
73 | 12 | return this.iter.equals(that.iter); |
74 | } | |
75 | ||
76 | /** | |
77 | * {@inheritDoc} | |
78 | */ | |
79 | public int hashCode() { | |
80 | 20 | int hash = "IteratorToGeneratorAdapater".hashCode(); |
81 | 20 | hash <<= 2; |
82 | 20 | hash ^= iter.hashCode(); |
83 | 20 | return hash; |
84 | } | |
85 | ||
86 | /** | |
87 | * {@inheritDoc} | |
88 | */ | |
89 | public String toString() { | |
90 | 20 | return "IteratorToGeneratorAdapter<" + iter + ">"; |
91 | } | |
92 | ||
93 | // static methods | |
94 | //----------------------------------------------------- | |
95 | /** | |
96 | * Adapt an Iterator to the Generator interface. | |
97 | * @param iter to adapt | |
98 | * @return IteratorToGeneratorAdapter | |
99 | */ | |
100 | public static <E> IteratorToGeneratorAdapter<E> adapt(Iterator<? extends E> iter) { | |
101 | 48 | return null == iter ? null : new IteratorToGeneratorAdapter<E>(iter); |
102 | } | |
103 | ||
104 | } |