1 7 8 9 package javax.print.attribute; 10 11 import java.io.Serializable ; 12 13 66 public abstract class Size2DSyntax implements Serializable , Cloneable { 67 68 private static final long serialVersionUID = 5584439964938660530L; 69 70 74 private int x; 75 76 80 private int y; 81 82 86 public static final int INCH = 25400; 87 88 92 public static final int MM = 1000; 93 94 95 109 protected Size2DSyntax(float x, float y, int units) { 110 if (x < 0.0f) { 111 throw new IllegalArgumentException ("x < 0"); 112 } 113 if (y < 0.0f) { 114 throw new IllegalArgumentException ("y < 0"); 115 } 116 if (units < 1) { 117 throw new IllegalArgumentException ("units < 1"); 118 } 119 this.x = (int) (x * units + 0.5f); 120 this.y = (int) (y * units + 0.5f); 121 } 122 123 137 protected Size2DSyntax(int x, int y, int units) { 138 if (x < 0) { 139 throw new IllegalArgumentException ("x < 0"); 140 } 141 if (y < 0) { 142 throw new IllegalArgumentException ("y < 0"); 143 } 144 if (units < 1) { 145 throw new IllegalArgumentException ("units < 1"); 146 } 147 this.x = x * units; 148 this.y = y * units; 149 } 150 151 166 private static float convertFromMicrometers(int x, int units) { 167 if (units < 1) { 168 throw new IllegalArgumentException ("units is < 1"); 169 } 170 return ((float)x) / ((float)units); 171 } 172 173 187 public float[] getSize(int units) { 188 return new float[] {getX(units), getY(units)}; 189 } 190 191 204 public float getX(int units) { 205 return convertFromMicrometers(x, units); 206 } 207 208 221 public float getY(int units) { 222 return convertFromMicrometers(y, units); 223 } 224 225 244 public String toString(int units, String unitsName) { 245 StringBuffer result = new StringBuffer (); 246 result.append(getX (units)); 247 result.append('x'); 248 result.append(getY (units)); 249 if (unitsName != null) { 250 result.append(' '); 251 result.append(unitsName); 252 } 253 return result.toString(); 254 } 255 256 278 public boolean equals(Object object) { 279 return(object != null && 280 object instanceof Size2DSyntax && 281 this.x == ((Size2DSyntax ) object).x && 282 this.y == ((Size2DSyntax ) object).y); 283 } 284 285 288 public int hashCode() { 289 return (((x & 0x0000FFFF) ) | 290 ((y & 0x0000FFFF) << 16)); 291 } 292 293 299 public String toString() { 300 StringBuffer result = new StringBuffer (); 301 result.append(x); 302 result.append('x'); 303 result.append(y); 304 result.append(" um"); 305 return result.toString(); 306 } 307 308 314 protected int getXMicrometers(){ 315 return x; 316 } 317 318 324 protected int getYMicrometers() { 325 return y; 326 } 327 328 } 329 | Popular Tags |