Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
IntegerRange |
|
| 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.util; | |
16 | ||
17 | import org.apache.commons.functor.UnaryProcedure; | |
18 | import org.apache.commons.functor.generator.BaseGenerator; | |
19 | ||
20 | ||
21 | /** | |
22 | * A generator for the range <i>from</i> (inclusive) to <i>to</i> (exclusive). | |
23 | * | |
24 | * @since 1.0 | |
25 | * @version $Revision: 1156804 $ $Date: 2011-08-11 22:14:47 +0200 (Thu, 11 Aug 2011) $ | |
26 | * @author Jason Horman (jason@jhorman.org) | |
27 | * @author Rodney Waldhoff | |
28 | */ | |
29 | public final class IntegerRange extends BaseGenerator<Integer> { | |
30 | // attributes | |
31 | //--------------------------------------------------------------- | |
32 | ||
33 | private final int from; | |
34 | private final int to; | |
35 | private final int step; | |
36 | ||
37 | // constructors | |
38 | //--------------------------------------------------------------- | |
39 | /** | |
40 | * Create a new IntegerRange. | |
41 | * @param from start | |
42 | * @param to end | |
43 | */ | |
44 | public IntegerRange(Number from, Number to) { | |
45 | 4 | this(from.intValue(), to.intValue()); |
46 | 4 | } |
47 | ||
48 | /** | |
49 | * Create a new IntegerRange. | |
50 | * @param from start | |
51 | * @param to end | |
52 | * @param step increment | |
53 | */ | |
54 | public IntegerRange(Number from, Number to, Number step) { | |
55 | 4 | this(from.intValue(), to.intValue(), step.intValue()); |
56 | 4 | } |
57 | ||
58 | /** | |
59 | * Create a new IntegerRange. | |
60 | * @param from start | |
61 | * @param to end | |
62 | */ | |
63 | public IntegerRange(int from, int to) { | |
64 | 44 | this(from, to, defaultStep(from, to)); |
65 | 44 | } |
66 | ||
67 | /** | |
68 | * Create a new IntegerRange. | |
69 | * @param from start | |
70 | * @param to end | |
71 | * @param step increment | |
72 | */ | |
73 | 70 | public IntegerRange(int from, int to, int step) { |
74 | 70 | if (from != to && signOf(step) != signOf(to - from)) { |
75 | 6 | throw new IllegalArgumentException("Will never reach " + to + " from " + from + " using step " + step); |
76 | } | |
77 | 64 | this.from = from; |
78 | 64 | this.to = to; |
79 | 64 | this.step = step; |
80 | 64 | } |
81 | ||
82 | // methods | |
83 | //--------------------------------------------------------------- | |
84 | /** | |
85 | * {@inheritDoc} | |
86 | */ | |
87 | public void run(UnaryProcedure<? super Integer> proc) { | |
88 | 46 | if (signOf(step) == -1) { |
89 | 70 | for (int i = from; i > to; i += step) { |
90 | 60 | proc.run(i); |
91 | } | |
92 | } else { | |
93 | 1600152 | for (int i = from; i < to; i += step) { |
94 | 1600116 | proc.run(i); |
95 | } | |
96 | } | |
97 | 46 | } |
98 | ||
99 | /** | |
100 | * {@inheritDoc} | |
101 | */ | |
102 | public String toString() { | |
103 | 26 | return "IntegerRange<" + from + "," + to + "," + step + ">"; |
104 | } | |
105 | ||
106 | /** | |
107 | * {@inheritDoc} | |
108 | */ | |
109 | public boolean equals(Object obj) { | |
110 | 28 | if (obj == this) { |
111 | 6 | return true; |
112 | } | |
113 | 22 | if (!(obj instanceof IntegerRange)) { |
114 | 2 | return false; |
115 | } | |
116 | 20 | IntegerRange that = (IntegerRange) obj; |
117 | 20 | return this.from == that.from && this.to == that.to && this.step == that.step; |
118 | } | |
119 | ||
120 | /** | |
121 | * {@inheritDoc} | |
122 | */ | |
123 | public int hashCode() { | |
124 | 30 | int hash = "IntegerRange".hashCode(); |
125 | 30 | hash <<= 2; |
126 | 30 | hash ^= from; |
127 | 30 | hash <<= 2; |
128 | 30 | hash ^= to; |
129 | 30 | hash <<= 2; |
130 | 30 | hash ^= step; |
131 | 30 | return hash; |
132 | } | |
133 | ||
134 | // private methods | |
135 | //--------------------------------------------------------------- | |
136 | /** | |
137 | * Get <code>value/|value|</code> (0 when value == 0). | |
138 | * @param value to test | |
139 | * @return int | |
140 | */ | |
141 | private static int signOf(int value) { | |
142 | 174 | return value < 0 ? -1 : value > 0 ? 1 : 0; |
143 | } | |
144 | ||
145 | /** | |
146 | * Calculate default step to get from <code>from</code> to <code>to</code>. | |
147 | * @param from start | |
148 | * @param to end | |
149 | * @return int | |
150 | */ | |
151 | private static int defaultStep(int from, int to) { | |
152 | 44 | return from > to ? -1 : 1; |
153 | } | |
154 | ||
155 | } |