1 | /*
|
---|
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
|
---|
3 | * contributor license agreements. See the NOTICE file distributed with
|
---|
4 | * this work for additional information regarding copyright ownership.
|
---|
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
|
---|
6 | * (the "License"); you may not use this file except in compliance with
|
---|
7 | * the License. You may obtain a copy of the License at
|
---|
8 | *
|
---|
9 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
10 | *
|
---|
11 | * Unless required by applicable law or agreed to in writing, software
|
---|
12 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
14 | * See the License for the specific language governing permissions and
|
---|
15 | * limitations under the License.
|
---|
16 | */
|
---|
17 | package agents.org.apache.commons.lang.math;
|
---|
18 |
|
---|
19 | import java.io.Serializable;
|
---|
20 |
|
---|
21 | import agents.org.apache.commons.lang.text.StrBuilder;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * <p><code>IntRange</code> represents an inclusive range of <code>int</code>s.</p>
|
---|
25 | *
|
---|
26 | * @author Apache Software Foundation
|
---|
27 | * @since 2.0
|
---|
28 | * @version $Id: IntRange.java 1057072 2011-01-10 01:55:57Z niallp $
|
---|
29 | */
|
---|
30 | public final class IntRange extends Range implements Serializable {
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Required for serialization support.
|
---|
34 | *
|
---|
35 | * @see java.io.Serializable
|
---|
36 | */
|
---|
37 | private static final long serialVersionUID = 71849363892730L;
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * The minimum number in this range (inclusive).
|
---|
41 | */
|
---|
42 | private final int min;
|
---|
43 | /**
|
---|
44 | * The maximum number in this range (inclusive).
|
---|
45 | */
|
---|
46 | private final int max;
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * Cached output minObject (class is immutable).
|
---|
50 | */
|
---|
51 | private transient Integer minObject = null;
|
---|
52 | /**
|
---|
53 | * Cached output maxObject (class is immutable).
|
---|
54 | */
|
---|
55 | private transient Integer maxObject = null;
|
---|
56 | /**
|
---|
57 | * Cached output hashCode (class is immutable).
|
---|
58 | */
|
---|
59 | private transient int hashCode = 0;
|
---|
60 | /**
|
---|
61 | * Cached output toString (class is immutable).
|
---|
62 | */
|
---|
63 | private transient String toString = null;
|
---|
64 |
|
---|
65 | /**
|
---|
66 | * <p>Constructs a new <code>IntRange</code> using the specified
|
---|
67 | * number as both the minimum and maximum in this range.</p>
|
---|
68 | *
|
---|
69 | * @param number the number to use for this range
|
---|
70 | */
|
---|
71 | public IntRange(int number) {
|
---|
72 | super();
|
---|
73 | this.min = number;
|
---|
74 | this.max = number;
|
---|
75 | }
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * <p>Constructs a new <code>IntRange</code> using the specified
|
---|
79 | * number as both the minimum and maximum in this range.</p>
|
---|
80 | *
|
---|
81 | * @param number the number to use for this range, must not be <code>null</code>
|
---|
82 | * @throws IllegalArgumentException if the number is <code>null</code>
|
---|
83 | */
|
---|
84 | public IntRange(Number number) {
|
---|
85 | super();
|
---|
86 | if (number == null) {
|
---|
87 | throw new IllegalArgumentException("The number must not be null");
|
---|
88 | }
|
---|
89 | this.min = number.intValue();
|
---|
90 | this.max = number.intValue();
|
---|
91 | if (number instanceof Integer) {
|
---|
92 | this.minObject = (Integer) number;
|
---|
93 | this.maxObject = (Integer) number;
|
---|
94 | }
|
---|
95 | }
|
---|
96 |
|
---|
97 | /**
|
---|
98 | * <p>Constructs a new <code>IntRange</code> with the specified
|
---|
99 | * minimum and maximum numbers (both inclusive).</p>
|
---|
100 | *
|
---|
101 | * <p>The arguments may be passed in the order (min,max) or (max,min). The
|
---|
102 | * getMinimum and getMaximum methods will return the correct values.</p>
|
---|
103 | *
|
---|
104 | * @param number1 first number that defines the edge of the range, inclusive
|
---|
105 | * @param number2 second number that defines the edge of the range, inclusive
|
---|
106 | */
|
---|
107 | public IntRange(int number1, int number2) {
|
---|
108 | super();
|
---|
109 | if (number2 < number1) {
|
---|
110 | this.min = number2;
|
---|
111 | this.max = number1;
|
---|
112 | } else {
|
---|
113 | this.min = number1;
|
---|
114 | this.max = number2;
|
---|
115 | }
|
---|
116 | }
|
---|
117 |
|
---|
118 | /**
|
---|
119 | * <p>Constructs a new <code>IntRange</code> with the specified
|
---|
120 | * minimum and maximum numbers (both inclusive).</p>
|
---|
121 | *
|
---|
122 | * <p>The arguments may be passed in the order (min,max) or (max,min). The
|
---|
123 | * getMinimum and getMaximum methods will return the correct values.</p>
|
---|
124 | *
|
---|
125 | * @param number1 first number that defines the edge of the range, inclusive
|
---|
126 | * @param number2 second number that defines the edge of the range, inclusive
|
---|
127 | * @throws IllegalArgumentException if either number is <code>null</code>
|
---|
128 | */
|
---|
129 | public IntRange(Number number1, Number number2) {
|
---|
130 | super();
|
---|
131 | if (number1 == null || number2 == null) {
|
---|
132 | throw new IllegalArgumentException("The numbers must not be null");
|
---|
133 | }
|
---|
134 | int number1val = number1.intValue();
|
---|
135 | int number2val = number2.intValue();
|
---|
136 | if (number2val < number1val) {
|
---|
137 | this.min = number2val;
|
---|
138 | this.max = number1val;
|
---|
139 | if (number2 instanceof Integer) {
|
---|
140 | this.minObject = (Integer) number2;
|
---|
141 | }
|
---|
142 | if (number1 instanceof Integer) {
|
---|
143 | this.maxObject = (Integer) number1;
|
---|
144 | }
|
---|
145 | } else {
|
---|
146 | this.min = number1val;
|
---|
147 | this.max = number2val;
|
---|
148 | if (number1 instanceof Integer) {
|
---|
149 | this.minObject = (Integer) number1;
|
---|
150 | }
|
---|
151 | if (number2 instanceof Integer) {
|
---|
152 | this.maxObject = (Integer) number2;
|
---|
153 | }
|
---|
154 | }
|
---|
155 | }
|
---|
156 |
|
---|
157 | // Accessors
|
---|
158 | //--------------------------------------------------------------------
|
---|
159 |
|
---|
160 | /**
|
---|
161 | * <p>Returns the minimum number in this range.</p>
|
---|
162 | *
|
---|
163 | * @return the minimum number in this range
|
---|
164 | */
|
---|
165 | public Number getMinimumNumber() {
|
---|
166 | if (minObject == null) {
|
---|
167 | minObject = new Integer(min);
|
---|
168 | }
|
---|
169 | return minObject;
|
---|
170 | }
|
---|
171 |
|
---|
172 | /**
|
---|
173 | * <p>Gets the minimum number in this range as a <code>long</code>.</p>
|
---|
174 | *
|
---|
175 | * @return the minimum number in this range
|
---|
176 | */
|
---|
177 | public long getMinimumLong() {
|
---|
178 | return min;
|
---|
179 | }
|
---|
180 |
|
---|
181 | /**
|
---|
182 | * <p>Gets the minimum number in this range as a <code>int</code>.</p>
|
---|
183 | *
|
---|
184 | * @return the minimum number in this range
|
---|
185 | */
|
---|
186 | public int getMinimumInteger() {
|
---|
187 | return min;
|
---|
188 | }
|
---|
189 |
|
---|
190 | /**
|
---|
191 | * <p>Gets the minimum number in this range as a <code>double</code>.</p>
|
---|
192 | *
|
---|
193 | * @return the minimum number in this range
|
---|
194 | */
|
---|
195 | public double getMinimumDouble() {
|
---|
196 | return min;
|
---|
197 | }
|
---|
198 |
|
---|
199 | /**
|
---|
200 | * <p>Gets the minimum number in this range as a <code>float</code>.</p>
|
---|
201 | *
|
---|
202 | * @return the minimum number in this range
|
---|
203 | */
|
---|
204 | public float getMinimumFloat() {
|
---|
205 | return min;
|
---|
206 | }
|
---|
207 |
|
---|
208 | /**
|
---|
209 | * <p>Returns the maximum number in this range.</p>
|
---|
210 | *
|
---|
211 | * @return the maximum number in this range
|
---|
212 | */
|
---|
213 | public Number getMaximumNumber() {
|
---|
214 | if (maxObject == null) {
|
---|
215 | maxObject = new Integer(max);
|
---|
216 | }
|
---|
217 | return maxObject;
|
---|
218 | }
|
---|
219 |
|
---|
220 | /**
|
---|
221 | * <p>Gets the maximum number in this range as a <code>long</code>.</p>
|
---|
222 | *
|
---|
223 | * @return the maximum number in this range
|
---|
224 | */
|
---|
225 | public long getMaximumLong() {
|
---|
226 | return max;
|
---|
227 | }
|
---|
228 |
|
---|
229 | /**
|
---|
230 | * <p>Gets the maximum number in this range as a <code>int</code>.</p>
|
---|
231 | *
|
---|
232 | * @return the maximum number in this range
|
---|
233 | */
|
---|
234 | public int getMaximumInteger() {
|
---|
235 | return max;
|
---|
236 | }
|
---|
237 |
|
---|
238 | /**
|
---|
239 | * <p>Gets the maximum number in this range as a <code>double</code>.</p>
|
---|
240 | *
|
---|
241 | * @return the maximum number in this range
|
---|
242 | */
|
---|
243 | public double getMaximumDouble() {
|
---|
244 | return max;
|
---|
245 | }
|
---|
246 |
|
---|
247 | /**
|
---|
248 | * <p>Gets the maximum number in this range as a <code>float</code>.</p>
|
---|
249 | *
|
---|
250 | * @return the maximum number in this range
|
---|
251 | */
|
---|
252 | public float getMaximumFloat() {
|
---|
253 | return max;
|
---|
254 | }
|
---|
255 |
|
---|
256 | // Tests
|
---|
257 | //--------------------------------------------------------------------
|
---|
258 |
|
---|
259 | /**
|
---|
260 | * <p>Tests whether the specified <code>number</code> occurs within
|
---|
261 | * this range using <code>int</code> comparison.</p>
|
---|
262 | *
|
---|
263 | * <p><code>null</code> is handled and returns <code>false</code>.</p>
|
---|
264 | *
|
---|
265 | * @param number the number to test, may be <code>null</code>
|
---|
266 | * @return <code>true</code> if the specified number occurs within this range
|
---|
267 | */
|
---|
268 | public boolean containsNumber(Number number) {
|
---|
269 | if (number == null) {
|
---|
270 | return false;
|
---|
271 | }
|
---|
272 | return containsInteger(number.intValue());
|
---|
273 | }
|
---|
274 |
|
---|
275 | /**
|
---|
276 | * <p>Tests whether the specified <code>int</code> occurs within
|
---|
277 | * this range using <code>int</code> comparison.</p>
|
---|
278 | *
|
---|
279 | * <p>This implementation overrides the superclass for performance as it is
|
---|
280 | * the most common case.</p>
|
---|
281 | *
|
---|
282 | * @param value the int to test
|
---|
283 | * @return <code>true</code> if the specified number occurs within this
|
---|
284 | * range by <code>int</code> comparison
|
---|
285 | */
|
---|
286 | public boolean containsInteger(int value) {
|
---|
287 | return value >= min && value <= max;
|
---|
288 | }
|
---|
289 |
|
---|
290 | // Range tests
|
---|
291 | //--------------------------------------------------------------------
|
---|
292 |
|
---|
293 | /**
|
---|
294 | * <p>Tests whether the specified range occurs entirely within this range
|
---|
295 | * using <code>int</code> comparison.</p>
|
---|
296 | *
|
---|
297 | * <p><code>null</code> is handled and returns <code>false</code>.</p>
|
---|
298 | *
|
---|
299 | * @param range the range to test, may be <code>null</code>
|
---|
300 | * @return <code>true</code> if the specified range occurs entirely within this range
|
---|
301 | * @throws IllegalArgumentException if the range is not of this type
|
---|
302 | */
|
---|
303 | public boolean containsRange(Range range) {
|
---|
304 | if (range == null) {
|
---|
305 | return false;
|
---|
306 | }
|
---|
307 | return containsInteger(range.getMinimumInteger()) &&
|
---|
308 | containsInteger(range.getMaximumInteger());
|
---|
309 | }
|
---|
310 |
|
---|
311 | /**
|
---|
312 | * <p>Tests whether the specified range overlaps with this range
|
---|
313 | * using <code>int</code> comparison.</p>
|
---|
314 | *
|
---|
315 | * <p><code>null</code> is handled and returns <code>false</code>.</p>
|
---|
316 | *
|
---|
317 | * @param range the range to test, may be <code>null</code>
|
---|
318 | * @return <code>true</code> if the specified range overlaps with this range
|
---|
319 | */
|
---|
320 | public boolean overlapsRange(Range range) {
|
---|
321 | if (range == null) {
|
---|
322 | return false;
|
---|
323 | }
|
---|
324 | return range.containsInteger(min) ||
|
---|
325 | range.containsInteger(max) ||
|
---|
326 | containsInteger(range.getMinimumInteger());
|
---|
327 | }
|
---|
328 |
|
---|
329 | // Basics
|
---|
330 | //--------------------------------------------------------------------
|
---|
331 |
|
---|
332 | /**
|
---|
333 | * <p>Compares this range to another object to test if they are equal.</p>.
|
---|
334 | *
|
---|
335 | * <p>To be equal, the class, minimum and maximum must be equal.</p>
|
---|
336 | *
|
---|
337 | * @param obj the reference object with which to compare
|
---|
338 | * @return <code>true</code> if this object is equal
|
---|
339 | */
|
---|
340 | public boolean equals(Object obj) {
|
---|
341 | if (obj == this) {
|
---|
342 | return true;
|
---|
343 | }
|
---|
344 | if (obj instanceof IntRange == false) {
|
---|
345 | return false;
|
---|
346 | }
|
---|
347 | IntRange range = (IntRange) obj;
|
---|
348 | return min == range.min && max == range.max;
|
---|
349 | }
|
---|
350 |
|
---|
351 | /**
|
---|
352 | * <p>Gets a hashCode for the range.</p>
|
---|
353 | *
|
---|
354 | * @return a hash code value for this object
|
---|
355 | */
|
---|
356 | public int hashCode() {
|
---|
357 | if (hashCode == 0) {
|
---|
358 | hashCode = 17;
|
---|
359 | hashCode = 37 * hashCode + getClass().hashCode();
|
---|
360 | hashCode = 37 * hashCode + min;
|
---|
361 | hashCode = 37 * hashCode + max;
|
---|
362 | }
|
---|
363 | return hashCode;
|
---|
364 | }
|
---|
365 |
|
---|
366 | /**
|
---|
367 | * <p>Gets the range as a <code>String</code>.</p>
|
---|
368 | *
|
---|
369 | * <p>The format of the String is 'Range[<i>min</i>,<i>max</i>]'.</p>
|
---|
370 | *
|
---|
371 | * @return the <code>String</code> representation of this range
|
---|
372 | */
|
---|
373 | public String toString() {
|
---|
374 | if (toString == null) {
|
---|
375 | StrBuilder buf = new StrBuilder(32);
|
---|
376 | buf.append("Range[");
|
---|
377 | buf.append(min);
|
---|
378 | buf.append(',');
|
---|
379 | buf.append(max);
|
---|
380 | buf.append(']');
|
---|
381 | toString = buf.toString();
|
---|
382 | }
|
---|
383 | return toString;
|
---|
384 | }
|
---|
385 |
|
---|
386 | /**
|
---|
387 | * <p>Returns an array containing all the integer values in the range.</p>
|
---|
388 | *
|
---|
389 | * @return the <code>int[]</code> representation of this range
|
---|
390 | * @since 2.4
|
---|
391 | */
|
---|
392 | public int[] toArray() {
|
---|
393 | int[] array = new int[max - min + 1];
|
---|
394 | for (int i = 0; i < array.length; i++) {
|
---|
395 | array[i] = min + i;
|
---|
396 | }
|
---|
397 |
|
---|
398 | return array;
|
---|
399 | }
|
---|
400 | }
|
---|