1 16 package org.apache.commons.lang; 17 18 34 public final class NumberRange { 35 36 37 private final Number min; 38 39 40 private final Number max; 41 42 43 51 public NumberRange(Number num) { 52 if (num == null) { 53 throw new NullPointerException ("The number must not be null"); 54 } 55 56 this.min = num; 57 this.max = num; 58 } 59 60 72 public NumberRange(Number min, Number max) { 73 if (min == null) { 74 throw new NullPointerException ("The minimum value must not be null"); 75 } else if (max == null) { 76 throw new NullPointerException ("The maximum value must not be null"); 77 } 78 79 if (max.doubleValue() < min.doubleValue()) { 80 this.min = this.max = min; 81 } else { 82 this.min = min; 83 this.max = max; 84 } 85 } 86 87 92 public Number getMinimum() { 93 return min; 94 } 95 96 101 public Number getMaximum() { 102 return max; 103 } 104 105 113 public boolean includesNumber(Number number) { 114 if (number == null) { 115 return false; 116 } else { 117 return !(min.doubleValue() > number.doubleValue()) && 118 !(max.doubleValue() < number.doubleValue()); 119 } 120 } 121 122 130 public boolean includesRange(NumberRange range) { 131 if (range == null) { 132 return false; 133 } else { 134 return includesNumber(range.min) && includesNumber(range.max); 135 } 136 } 137 138 146 public boolean overlaps(NumberRange range) { 147 if (range == null) { 148 return false; 149 } else { 150 return range.includesNumber(min) || range.includesNumber(max) || 151 includesRange(range); 152 } 153 } 154 155 163 public boolean equals(Object obj) { 164 if (obj == this) { 165 return true; 166 } else if (!(obj instanceof NumberRange)) { 167 return false; 168 } else { 169 NumberRange range = (NumberRange)obj; 170 return min.equals(range.min) && max.equals(range.max); 171 } 172 } 173 174 179 public int hashCode() { 180 int result = 17; 181 result = 37 * result + min.hashCode(); 182 result = 37 * result + max.hashCode(); 183 return result; 184 } 185 186 195 public String toString() { 196 StringBuffer sb = new StringBuffer (); 197 198 if (min.doubleValue() < 0) { 199 sb.append('(') 200 .append(min) 201 .append(')'); 202 } else { 203 sb.append(min); 204 } 205 206 sb.append('-'); 207 208 if (max.doubleValue() < 0) { 209 sb.append('(') 210 .append(max) 211 .append(')'); 212 } else { 213 sb.append(max); 214 } 215 216 return sb.toString(); 217 } 218 219 } 220 | Popular Tags |