1 29 30 package nextapp.echo2.app; 31 import java.io.Serializable ; 32 33 45 public class Extent 46 implements Comparable , Serializable { 47 48 58 public static Extent add(Extent a, Extent b) { 59 if (a == null) { 60 return b; 61 } 62 if (b == null) { 63 return a; 64 } 65 if (a.getUnits() == b.getUnits()) { 66 return new Extent(a.getValue() + b.getValue(), a.getUnits()); 67 } 68 if (a.isPrint() && b.isPrint()) { 69 if (a.isEnglish() && b.isEnglish()) { 70 return new Extent(a.toPoint() + b.toPoint(), PT); 71 } 72 return new Extent(a.toMm() + b.toMm(), MM); 73 } 74 return null; 75 } 76 77 87 public static void validate(Extent value, int validUnits) { 88 if (value != null && (value.getUnits() & validUnits) == 0) { 89 throw new IllegalArgumentException ("Specified units are unsupported in this context."); 90 } 91 } 92 93 96 public static final int PX = 1; 97 98 101 public static final int PERCENT = 2; 102 103 106 public static final int PT = 4; 107 108 111 public static final int CM = 8; 112 113 116 public static final int MM = 16; 117 118 121 public static final int IN = 32; 122 123 126 public static final int EM = 64; 127 128 131 public static final int EX = 128; 132 133 136 public static final int PC = 256; 137 138 139 private int value; 140 private int units; 141 142 147 public Extent(int value) { 148 this.value = value; 149 this.units = Extent.PX; 150 } 151 152 170 public Extent(int value, int units) { 171 this.value = value; 172 this.units = units; 173 } 174 175 178 public int compareTo(Object o) { 179 Extent that = (Extent) o; 180 if (this.units == that.units) { 181 return this.value - that.value; 182 } 183 if (this.isPrint() && that.isPrint()) { 184 return this.toPoint() - that.toPoint(); 185 } 186 return this.units - that.units; 187 } 188 189 192 public boolean equals(Object o) { 193 if (this == o) { 194 return true; 195 } else if (o == null) { 196 return false; 197 } else if (o instanceof Extent) { 198 Extent that = (Extent) o; 199 return this.value == that.value && this.units == that.units; 200 } else { 201 return false; 202 } 203 } 204 205 210 public int getValue() { 211 return value; 212 } 213 214 232 public int getUnits() { 233 return units; 234 } 235 236 243 public boolean isComparableTo(Extent that) { 244 return this.units == that.units || (this.isPrint() && that.isPrint()); 245 } 246 247 254 public boolean isEnglish() { 255 return units == IN || units == PC || units == PT; 256 } 257 258 265 public boolean isSI() { 266 return units == MM || units == CM; 267 } 268 269 274 public boolean isPercentage() { 275 return units == PERCENT; 276 } 277 278 285 public boolean isPrint() { 286 return units == IN || units == PC || units == PT || units == MM || units == CM; 287 } 288 289 298 public int toMm() { 299 switch (units) { 300 case MM: 301 return value; 302 case CM: 303 return value * 10; 304 case IN: 305 return (int) (value * 25.4); 306 case PT: 307 return (int) ((value / 72) * 25.4); 308 case PC: 309 return (int) ((value / 6) * 25.4); 310 } 311 throw new IllegalStateException ("Cannot convert to mm."); 312 } 313 314 322 public int toPoint() { 323 switch (units) { 324 case PT: 325 return value; 326 case PC: 327 return value * 12; 328 case IN: 329 return value * 72; 330 case MM: 331 return (int) ((value / 25.4) * 72); 332 case CM: 333 return (int) ((value / 2.54) * 72); 334 } 335 throw new IllegalStateException ("Cannot convert to pt."); 336 } 337 338 344 public String toString() { 345 StringBuffer out = new StringBuffer (); 346 out.append(value); 347 switch (units) { 348 case Extent.CM: 349 out.append("cm"); 350 break; 351 case Extent.EM: 352 out.append("em"); 353 break; 354 case Extent.EX: 355 out.append("ex"); 356 break; 357 case Extent.IN: 358 out.append("in"); 359 break; 360 case Extent.MM: 361 out.append("mm"); 362 break; 363 case Extent.PC: 364 out.append("pc"); 365 break; 366 case Extent.PERCENT: 367 out.append("%"); 368 break; 369 case Extent.PT: 370 out.append("pt"); 371 break; 372 case Extent.PX: 373 out.append("px"); 374 break; 375 } 376 return out.toString(); 377 } 378 } 379 | Popular Tags |