KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > lang > math > FloatRange


1 /*
2  * Copyright 2002-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.lang.math;
17
18 import java.io.Serializable JavaDoc;
19
20 /**
21  * <p><code>FloatRange</code> represents an inclusive range of <code>float</code>s.</p>
22  *
23  * @author Stephen Colebourne
24  * @since 2.0
25  * @version $Id: FloatRange.java 161243 2005-04-14 04:30:28Z ggregory $
26  */

27 public final class FloatRange extends Range implements Serializable JavaDoc {
28     
29     private static final long serialVersionUID = 71849363892750L;
30
31     /**
32      * The minimum number in this range (inclusive).
33      */

34     private final float min;
35     /**
36      * The maximum number in this range (inclusive).
37      */

38     private final float max;
39     
40     /**
41      * Cached output minObject (class is immutable).
42      */

43     private transient Float JavaDoc minObject = null;
44     /**
45      * Cached output maxObject (class is immutable).
46      */

47     private transient Float JavaDoc maxObject = null;
48     /**
49      * Cached output hashCode (class is immutable).
50      */

51     private transient int hashCode = 0;
52     /**
53      * Cached output toString (class is immutable).
54      */

55     private transient String JavaDoc toString = null;
56     
57     /**
58      * <p>Constructs a new <code>FloatRange</code> using the specified
59      * number as both the minimum and maximum in this range.</p>
60      *
61      * @param number the number to use for this range
62      * @throws IllegalArgumentException if the number is <code>NaN</code>
63      */

64     public FloatRange(float number) {
65         super();
66         if (Float.isNaN(number)) {
67             throw new IllegalArgumentException JavaDoc("The number must not be NaN");
68         }
69         this.min = number;
70         this.max = number;
71     }
72
73     /**
74      * <p>Constructs a new <code>FloatRange</code> using the specified
75      * number as both the minimum and maximum in this range.</p>
76      *
77      * @param number the number to use for this range, must not
78      * be <code>null</code>
79      * @throws IllegalArgumentException if the number is <code>null</code>
80      * @throws IllegalArgumentException if the number is <code>NaN</code>
81      */

82     public FloatRange(Number JavaDoc number) {
83         super();
84         if (number == null) {
85             throw new IllegalArgumentException JavaDoc("The number must not be null");
86         }
87         this.min = number.floatValue();
88         this.max = number.floatValue();
89         if (Float.isNaN(min) || Float.isNaN(max)) {
90             throw new IllegalArgumentException JavaDoc("The number must not be NaN");
91         }
92         if (number instanceof Float JavaDoc) {
93             this.minObject = (Float JavaDoc) number;
94             this.maxObject = (Float JavaDoc) number;
95         }
96     }
97
98     /**
99      * <p>Constructs a new <code>FloatRange</code> with the specified
100      * minimum and maximum numbers (both inclusive).</p>
101      *
102      * <p>The arguments may be passed in the order (min,max) or (max,min). The
103      * getMinimum and getMaximum methods will return the correct values.</p>
104      *
105      * @param number1 first number that defines the edge of the range, inclusive
106      * @param number2 second number that defines the edge of the range, inclusive
107      * @throws IllegalArgumentException if either number is <code>NaN</code>
108      */

109     public FloatRange(float number1, float number2) {
110         super();
111         if (Float.isNaN(number1) || Float.isNaN(number2)) {
112             throw new IllegalArgumentException JavaDoc("The numbers must not be NaN");
113         }
114         if (number2 < number1) {
115             this.min = number2;
116             this.max = number1;
117         } else {
118             this.min = number1;
119             this.max = number2;
120         }
121     }
122
123     /**
124      * <p>Constructs a new <code>FloatRange</code> with the specified
125      * minimum and maximum numbers (both inclusive).</p>
126      *
127      * <p>The arguments may be passed in the order (min,max) or (max,min). The
128      * getMinimum and getMaximum methods will return the correct values.</p>
129      *
130      * @param number1 first number that defines the edge of the range, inclusive
131      * @param number2 second number that defines the edge of the range, inclusive
132      * @throws IllegalArgumentException if either number is <code>null</code>
133      * @throws IllegalArgumentException if either number is <code>NaN</code>
134      */

135     public FloatRange(Number JavaDoc number1, Number JavaDoc number2) {
136         super();
137         if (number1 == null || number2 == null) {
138             throw new IllegalArgumentException JavaDoc("The numbers must not be null");
139         }
140         float number1val = number1.floatValue();
141         float number2val = number2.floatValue();
142         if (Float.isNaN(number1val) || Float.isNaN(number2val)) {
143             throw new IllegalArgumentException JavaDoc("The numbers must not be NaN");
144         }
145         if (number2val < number1val) {
146             this.min = number2val;
147             this.max = number1val;
148             if (number2 instanceof Float JavaDoc) {
149                 this.minObject = (Float JavaDoc) number2;
150             }
151             if (number1 instanceof Float JavaDoc) {
152                 this.maxObject = (Float JavaDoc) number1;
153             }
154         } else {
155             this.min = number1val;
156             this.max = number2val;
157             if (number1 instanceof Float JavaDoc) {
158                 this.minObject = (Float JavaDoc) number1;
159             }
160             if (number2 instanceof Float JavaDoc) {
161                 this.maxObject = (Float JavaDoc) number2;
162             }
163         }
164     }
165
166     // Accessors
167
//--------------------------------------------------------------------
168

169     /**
170      * <p>Returns the minimum number in this range.</p>
171      *
172      * @return the minimum number in this range
173      */

174     public Number JavaDoc getMinimumNumber() {
175         if (minObject == null) {
176             minObject = new Float JavaDoc(min);
177         }
178         return minObject;
179     }
180
181     /**
182      * <p>Gets the minimum number in this range as a <code>long</code>.</p>
183      *
184      * <p>This conversion can lose information for large values or decimals.</p>
185      *
186      * @return the minimum number in this range
187      */

188     public long getMinimumLong() {
189         return (long) min;
190     }
191
192     /**
193      * <p>Gets the minimum number in this range as a <code>int</code>.</p>
194      *
195      * <p>This conversion can lose information for large values or decimals.</p>
196      *
197      * @return the minimum number in this range
198      */

199     public int getMinimumInteger() {
200         return (int) min;
201     }
202
203     /**
204      * <p>Gets the minimum number in this range as a <code>double</code>.</p>
205      *
206      * @return the minimum number in this range
207      */

208     public double getMinimumDouble() {
209         return min;
210     }
211
212     /**
213      * <p>Gets the minimum number in this range as a <code>float</code>.</p>
214      *
215      * @return the minimum number in this range
216      */

217     public float getMinimumFloat() {
218         return min;
219     }
220
221     /**
222      * <p>Returns the maximum number in this range.</p>
223      *
224      * @return the maximum number in this range
225      */

226     public Number JavaDoc getMaximumNumber() {
227         if (maxObject == null) {
228             maxObject = new Float JavaDoc(max);
229         }
230         return maxObject;
231     }
232
233     /**
234      * <p>Gets the maximum number in this range as a <code>long</code>.</p>
235      *
236      * <p>This conversion can lose information for large values or decimals.</p>
237      *
238      * @return the maximum number in this range
239      */

240     public long getMaximumLong() {
241         return (long) max;
242     }
243
244     /**
245      * <p>Gets the maximum number in this range as a <code>int</code>.</p>
246      *
247      * <p>This conversion can lose information for large values or decimals.</p>
248      *
249      * @return the maximum number in this range
250      */

251     public int getMaximumInteger() {
252         return (int) max;
253     }
254
255     /**
256      * <p>Gets the maximum number in this range as a <code>double</code>.</p>
257      *
258      * @return the maximum number in this range
259      */

260     public double getMaximumDouble() {
261         return max;
262     }
263
264     /**
265      * <p>Gets the maximum number in this range as a <code>float</code>.</p>
266      *
267      * @return the maximum number in this range
268      */

269     public float getMaximumFloat() {
270         return max;
271     }
272
273     // Tests
274
//--------------------------------------------------------------------
275

276     /**
277      * <p>Tests whether the specified <code>number</code> occurs within
278      * this range using <code>float</code> comparison.</p>
279      *
280      * <p><code>null</code> is handled and returns <code>false</code>.</p>
281      *
282      * @param number the number to test, may be <code>null</code>
283      * @return <code>true</code> if the specified number occurs within this range
284      */

285     public boolean containsNumber(Number JavaDoc number) {
286         if (number == null) {
287             return false;
288         }
289         return containsFloat(number.floatValue());
290     }
291
292     /**
293      * <p>Tests whether the specified <code>float</code> occurs within
294      * this range using <code>float</code> comparison.</p>
295      *
296      * <p>This implementation overrides the superclass for performance as it is
297      * the most common case.</p>
298      *
299      * @param value the float to test
300      * @return <code>true</code> if the specified number occurs within this
301      * range by <code>float</code> comparison
302      */

303     public boolean containsFloat(float value) {
304         return value >= min && value <= max;
305     }
306
307     // Range tests
308
//--------------------------------------------------------------------
309

310     /**
311      * <p>Tests whether the specified range occurs entirely within this range
312      * using <code>float</code> comparison.</p>
313      *
314      * <p><code>null</code> is handled and returns <code>false</code>.</p>
315      *
316      * @param range the range to test, may be <code>null</code>
317      * @return <code>true</code> if the specified range occurs entirely within this range
318      * @throws IllegalArgumentException if the range is not of this type
319      */

320     public boolean containsRange(Range range) {
321         if (range == null) {
322             return false;
323         }
324         return containsFloat(range.getMinimumFloat()) &&
325                containsFloat(range.getMaximumFloat());
326     }
327
328     /**
329      * <p>Tests whether the specified range overlaps with this range
330      * using <code>float</code> comparison.</p>
331      *
332      * <p><code>null</code> is handled and returns <code>false</code>.</p>
333      *
334      * @param range the range to test, may be <code>null</code>
335      * @return <code>true</code> if the specified range overlaps with this range
336      */

337     public boolean overlapsRange(Range range) {
338         if (range == null) {
339             return false;
340         }
341         return range.containsFloat(min) ||
342                range.containsFloat(max) ||
343                containsFloat(range.getMinimumFloat());
344     }
345
346     // Basics
347
//--------------------------------------------------------------------
348

349     /**
350      * <p>Compares this range to another object to test if they are equal.</p>.
351      *
352      * <p>To be equal, the class, minimum and maximum must be equal.</p>
353      *
354      * @param obj the reference object with which to compare
355      * @return <code>true</code> if this object is equal
356      */

357     public boolean equals(Object JavaDoc obj) {
358         if (obj == this) {
359             return true;
360         }
361         if (obj instanceof FloatRange == false) {
362             return false;
363         }
364         FloatRange range = (FloatRange) obj;
365         return (Float.floatToIntBits(min) == Float.floatToIntBits(range.min) &&
366                 Float.floatToIntBits(max) == Float.floatToIntBits(range.max));
367     }
368
369     /**
370      * <p>Gets a hashCode for the range.</p>
371      *
372      * @return a hash code value for this object
373      */

374     public int hashCode() {
375         if (hashCode == 0) {
376             hashCode = 17;
377             hashCode = 37 * hashCode + getClass().hashCode();
378             hashCode = 37 * hashCode + Float.floatToIntBits(min);
379             hashCode = 37 * hashCode + Float.floatToIntBits(max);
380         }
381         return hashCode;
382     }
383
384     /**
385      * <p>Gets the range as a <code>String</code>.</p>
386      *
387      * <p>The format of the String is 'Range[<i>min</i>,<i>max</i>]'.</p>
388      *
389      * @return the <code>String</code> representation of this range
390      */

391     public String JavaDoc toString() {
392         if (toString == null) {
393             StringBuffer JavaDoc buf = new StringBuffer JavaDoc(32);
394             buf.append("Range[");
395             buf.append(min);
396             buf.append(',');
397             buf.append(max);
398             buf.append(']');
399             toString = buf.toString();
400         }
401         return toString;
402     }
403
404 }
405
Popular Tags