1 16 package org.apache.commons.lang.math; 17 18 import java.io.Serializable ; 19 20 27 public final class FloatRange extends Range implements Serializable { 28 29 private static final long serialVersionUID = 71849363892750L; 30 31 34 private final float min; 35 38 private final float max; 39 40 43 private transient Float minObject = null; 44 47 private transient Float maxObject = null; 48 51 private transient int hashCode = 0; 52 55 private transient String toString = null; 56 57 64 public FloatRange(float number) { 65 super(); 66 if (Float.isNaN(number)) { 67 throw new IllegalArgumentException ("The number must not be NaN"); 68 } 69 this.min = number; 70 this.max = number; 71 } 72 73 82 public FloatRange(Number number) { 83 super(); 84 if (number == null) { 85 throw new IllegalArgumentException ("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 ("The number must not be NaN"); 91 } 92 if (number instanceof Float ) { 93 this.minObject = (Float ) number; 94 this.maxObject = (Float ) number; 95 } 96 } 97 98 109 public FloatRange(float number1, float number2) { 110 super(); 111 if (Float.isNaN(number1) || Float.isNaN(number2)) { 112 throw new IllegalArgumentException ("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 135 public FloatRange(Number number1, Number number2) { 136 super(); 137 if (number1 == null || number2 == null) { 138 throw new IllegalArgumentException ("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 ("The numbers must not be NaN"); 144 } 145 if (number2val < number1val) { 146 this.min = number2val; 147 this.max = number1val; 148 if (number2 instanceof Float ) { 149 this.minObject = (Float ) number2; 150 } 151 if (number1 instanceof Float ) { 152 this.maxObject = (Float ) number1; 153 } 154 } else { 155 this.min = number1val; 156 this.max = number2val; 157 if (number1 instanceof Float ) { 158 this.minObject = (Float ) number1; 159 } 160 if (number2 instanceof Float ) { 161 this.maxObject = (Float ) number2; 162 } 163 } 164 } 165 166 169 174 public Number getMinimumNumber() { 175 if (minObject == null) { 176 minObject = new Float (min); 177 } 178 return minObject; 179 } 180 181 188 public long getMinimumLong() { 189 return (long) min; 190 } 191 192 199 public int getMinimumInteger() { 200 return (int) min; 201 } 202 203 208 public double getMinimumDouble() { 209 return min; 210 } 211 212 217 public float getMinimumFloat() { 218 return min; 219 } 220 221 226 public Number getMaximumNumber() { 227 if (maxObject == null) { 228 maxObject = new Float (max); 229 } 230 return maxObject; 231 } 232 233 240 public long getMaximumLong() { 241 return (long) max; 242 } 243 244 251 public int getMaximumInteger() { 252 return (int) max; 253 } 254 255 260 public double getMaximumDouble() { 261 return max; 262 } 263 264 269 public float getMaximumFloat() { 270 return max; 271 } 272 273 276 285 public boolean containsNumber(Number number) { 286 if (number == null) { 287 return false; 288 } 289 return containsFloat(number.floatValue()); 290 } 291 292 303 public boolean containsFloat(float value) { 304 return value >= min && value <= max; 305 } 306 307 310 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 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 349 357 public boolean equals(Object 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 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 391 public String toString() { 392 if (toString == null) { 393 StringBuffer buf = new StringBuffer (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 |