1 16 package org.apache.commons.lang.math; 17 18 import java.io.Serializable ; 19 20 27 public final class DoubleRange extends Range implements Serializable { 28 29 private static final long serialVersionUID = 71849363892740L; 30 31 34 private final double min; 35 38 private final double max; 39 40 43 private transient Double minObject = null; 44 47 private transient Double maxObject = null; 48 51 private transient int hashCode = 0; 52 55 private transient String toString = null; 56 57 64 public DoubleRange(double number) { 65 super(); 66 if (Double.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 DoubleRange(Number number) { 83 super(); 84 if (number == null) { 85 throw new IllegalArgumentException ("The number must not be null"); 86 } 87 this.min = number.doubleValue(); 88 this.max = number.doubleValue(); 89 if (Double.isNaN(min) || Double.isNaN(max)) { 90 throw new IllegalArgumentException ("The number must not be NaN"); 91 } 92 if (number instanceof Double ) { 93 this.minObject = (Double ) number; 94 this.maxObject = (Double ) number; 95 } 96 } 97 98 109 public DoubleRange(double number1, double number2) { 110 super(); 111 if (Double.isNaN(number1) || Double.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 DoubleRange(Number number1, Number number2) { 136 super(); 137 if (number1 == null || number2 == null) { 138 throw new IllegalArgumentException ("The numbers must not be null"); 139 } 140 double number1val = number1.doubleValue(); 141 double number2val = number2.doubleValue(); 142 if (Double.isNaN(number1val) || Double.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 Double ) { 149 this.minObject = (Double ) number2; 150 } 151 if (number1 instanceof Double ) { 152 this.maxObject = (Double ) number1; 153 } 154 } else { 155 this.min = number1val; 156 this.max = number2val; 157 if (number1 instanceof Double ) { 158 this.minObject = (Double ) number1; 159 } 160 if (number2 instanceof Double ) { 161 this.maxObject = (Double ) number2; 162 } 163 } 164 } 165 166 169 174 public Number getMinimumNumber() { 175 if (minObject == null) { 176 minObject = new Double (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 219 public float getMinimumFloat() { 220 return (float) min; 221 } 222 223 228 public Number getMaximumNumber() { 229 if (maxObject == null) { 230 maxObject = new Double (max); 231 } 232 return maxObject; 233 } 234 235 242 public long getMaximumLong() { 243 return (long) max; 244 } 245 246 253 public int getMaximumInteger() { 254 return (int) max; 255 } 256 257 262 public double getMaximumDouble() { 263 return max; 264 } 265 266 273 public float getMaximumFloat() { 274 return (float) max; 275 } 276 277 280 289 public boolean containsNumber(Number number) { 290 if (number == null) { 291 return false; 292 } 293 return containsDouble(number.doubleValue()); 294 } 295 296 307 public boolean containsDouble(double value) { 308 return value >= min && value <= max; 309 } 310 311 314 324 public boolean containsRange(Range range) { 325 if (range == null) { 326 return false; 327 } 328 return containsDouble(range.getMinimumDouble()) 329 && containsDouble(range.getMaximumDouble()); 330 } 331 332 341 public boolean overlapsRange(Range range) { 342 if (range == null) { 343 return false; 344 } 345 return range.containsDouble(min) 346 || range.containsDouble(max) 347 || containsDouble(range.getMinimumDouble()); 348 } 349 350 353 361 public boolean equals(Object obj) { 362 if (obj == this) { 363 return true; 364 } 365 if (obj instanceof DoubleRange == false) { 366 return false; 367 } 368 DoubleRange range = (DoubleRange) obj; 369 return (Double.doubleToLongBits(min) == Double.doubleToLongBits(range.min) && 370 Double.doubleToLongBits(max) == Double.doubleToLongBits(range.max)); 371 } 372 373 378 public int hashCode() { 379 if (hashCode == 0) { 380 hashCode = 17; 381 hashCode = 37 * hashCode + getClass().hashCode(); 382 long lng = Double.doubleToLongBits(min); 383 hashCode = 37 * hashCode + ((int) (lng ^ (lng >> 32))); 384 lng = Double.doubleToLongBits(max); 385 hashCode = 37 * hashCode + ((int) (lng ^ (lng >> 32))); 386 } 387 return hashCode; 388 } 389 390 397 public String toString() { 398 if (toString == null) { 399 StringBuffer buf = new StringBuffer (32); 400 buf.append("Range["); 401 buf.append(min); 402 buf.append(','); 403 buf.append(max); 404 buf.append(']'); 405 toString = buf.toString(); 406 } 407 return toString; 408 } 409 410 } 411 | Popular Tags |