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 this(from.intValue(), to.intValue());
46 }
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 this(from.intValue(), to.intValue(), step.intValue());
56 }
57
58 /**
59 * Create a new IntegerRange.
60 * @param from start
61 * @param to end
62 */
63 public IntegerRange(int from, int to) {
64 this(from, to, defaultStep(from, to));
65 }
66
67 /**
68 * Create a new IntegerRange.
69 * @param from start
70 * @param to end
71 * @param step increment
72 */
73 public IntegerRange(int from, int to, int step) {
74 if (from != to && signOf(step) != signOf(to - from)) {
75 throw new IllegalArgumentException("Will never reach " + to + " from " + from + " using step " + step);
76 }
77 this.from = from;
78 this.to = to;
79 this.step = step;
80 }
81
82 // methods
83 //---------------------------------------------------------------
84 /**
85 * {@inheritDoc}
86 */
87 public void run(UnaryProcedure<? super Integer> proc) {
88 if (signOf(step) == -1) {
89 for (int i = from; i > to; i += step) {
90 proc.run(i);
91 }
92 } else {
93 for (int i = from; i < to; i += step) {
94 proc.run(i);
95 }
96 }
97 }
98
99 /**
100 * {@inheritDoc}
101 */
102 public String toString() {
103 return "IntegerRange<" + from + "," + to + "," + step + ">";
104 }
105
106 /**
107 * {@inheritDoc}
108 */
109 public boolean equals(Object obj) {
110 if (obj == this) {
111 return true;
112 }
113 if (!(obj instanceof IntegerRange)) {
114 return false;
115 }
116 IntegerRange that = (IntegerRange) obj;
117 return this.from == that.from && this.to == that.to && this.step == that.step;
118 }
119
120 /**
121 * {@inheritDoc}
122 */
123 public int hashCode() {
124 int hash = "IntegerRange".hashCode();
125 hash <<= 2;
126 hash ^= from;
127 hash <<= 2;
128 hash ^= to;
129 hash <<= 2;
130 hash ^= step;
131 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 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 return from > to ? -1 : 1;
153 }
154
155 }