1 16 package org.apache.commons.lang.math; 17 18 29 public abstract class Range { 30 31 34 public Range() { 35 super(); 36 } 37 38 41 46 public abstract Number getMinimumNumber(); 47 48 56 public long getMinimumLong() { 57 return getMinimumNumber().longValue(); 58 } 59 60 68 public int getMinimumInteger() { 69 return getMinimumNumber().intValue(); 70 } 71 72 80 public double getMinimumDouble() { 81 return getMinimumNumber().doubleValue(); 82 } 83 84 92 public float getMinimumFloat() { 93 return getMinimumNumber().floatValue(); 94 } 95 96 101 public abstract Number getMaximumNumber(); 102 103 111 public long getMaximumLong() { 112 return getMaximumNumber().longValue(); 113 } 114 115 123 public int getMaximumInteger() { 124 return getMaximumNumber().intValue(); 125 } 126 127 135 public double getMaximumDouble() { 136 return getMaximumNumber().doubleValue(); 137 } 138 139 147 public float getMaximumFloat() { 148 return getMaximumNumber().floatValue(); 149 } 150 151 154 168 public abstract boolean containsNumber(Number number); 169 170 182 public boolean containsLong(Number value) { 183 if (value == null) { 184 return false; 185 } 186 return containsLong(value.longValue()); 187 } 188 189 200 public boolean containsLong(long value) { 201 return value >= getMinimumLong() && value <= getMaximumLong(); 202 } 203 204 216 public boolean containsInteger(Number value) { 217 if (value == null) { 218 return false; 219 } 220 return containsInteger(value.intValue()); 221 } 222 223 234 public boolean containsInteger(int value) { 235 return value >= getMinimumInteger() && value <= getMaximumInteger(); 236 } 237 238 250 public boolean containsDouble(Number value) { 251 if (value == null) { 252 return false; 253 } 254 return containsDouble(value.doubleValue()); 255 } 256 257 268 public boolean containsDouble(double value) { 269 int compareMin = NumberUtils.compare(getMinimumDouble(), value); 270 int compareMax = NumberUtils.compare(getMaximumDouble(), value); 271 return compareMin <= 0 && compareMax >= 0; 272 } 273 274 286 public boolean containsFloat(Number value) { 287 if (value == null) { 288 return false; 289 } 290 return containsFloat(value.floatValue()); 291 } 292 293 304 public boolean containsFloat(float value) { 305 int compareMin = NumberUtils.compare(getMinimumFloat(), value); 306 int compareMax = NumberUtils.compare(getMaximumFloat(), value); 307 return compareMin <= 0 && compareMax >= 0; 308 } 309 310 313 330 public boolean containsRange(Range range) { 331 if (range == null) { 332 return false; 333 } 334 return containsNumber(range.getMinimumNumber()) 335 && containsNumber(range.getMaximumNumber()); 336 } 337 338 356 public boolean overlapsRange(Range range) { 357 if (range == null) { 358 return false; 359 } 360 return range.containsNumber(getMinimumNumber()) 361 || range.containsNumber(getMaximumNumber()) 362 || containsNumber(range.getMinimumNumber()); 363 } 364 365 368 380 public boolean equals(Object obj) { 381 if (obj == this) { 382 return true; 383 } else if (obj == null || obj.getClass() != getClass()) { 384 return false; 385 } else { 386 Range range = (Range) obj; 387 return getMinimumNumber().equals(range.getMinimumNumber()) && 388 getMaximumNumber().equals(range.getMaximumNumber()); 389 } 390 } 391 392 401 public int hashCode() { 402 int result = 17; 403 result = 37 * result + getClass().hashCode(); 404 result = 37 * result + getMinimumNumber().hashCode(); 405 result = 37 * result + getMaximumNumber().hashCode(); 406 return result; 407 } 408 409 420 public String toString() { 421 StringBuffer buf = new StringBuffer (32); 422 buf.append("Range["); 423 buf.append(getMinimumNumber()); 424 buf.append(','); 425 buf.append(getMaximumNumber()); 426 buf.append(']'); 427 return buf.toString(); 428 } 429 430 } 431 | Popular Tags |