1 /* 2 * Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 27 package javax.print.attribute; 28 29 import java.io.Serializable; 30 31 /** 32 * Class Size2DSyntax is an abstract base class providing the common 33 * implementation of all attributes denoting a size in two dimensions. 34 * <P> 35 * A two-dimensional size attribute's value consists of two items, the X 36 * dimension and the Y dimension. A two-dimensional size attribute may be 37 * constructed by supplying the two values and indicating the units in which the 38 * values are measured. Methods are provided to return a two-dimensional size 39 * attribute's values, indicating the units in which the values are to be 40 * returned. The two most common size units are inches (in) and millimeters 41 * (mm), and exported constants {@link #INCH INCH} and {@link #MM 42 * MM} are provided for indicating those units. 43 * <P> 44 * Once constructed, a two-dimensional size attribute's value is immutable. 45 * <P> 46 * <B>Design</B> 47 * <P> 48 * A two-dimensional size attribute's X and Y dimension values are stored 49 * internally as integers in units of micrometers (µm), where 1 micrometer 50 * = 10<SUP>-6</SUP> meter = 1/1000 millimeter = 1/25400 inch. This permits 51 * dimensions to be represented exactly to a precision of 1/1000 mm (= 1 52 * µm) or 1/100 inch (= 254 µm). If fractional inches are expressed in 53 * negative powers of two, this permits dimensions to be represented exactly to 54 * a precision of 1/8 inch (= 3175 µm) but not 1/16 inch (because 1/16 inch 55 * does not equal an integral number of µm). 56 * <P> 57 * Storing the dimensions internally in common units of µm lets two size 58 * attributes be compared without regard to the units in which they were 59 * created; for example, 8.5 in will compare equal to 215.9 mm, as they both are 60 * stored as 215900 µm. For example, a lookup service can 61 * match resolution attributes based on equality of their serialized 62 * representations regardless of the units in which they were created. Using 63 * integers for internal storage allows precise equality comparisons to be done, 64 * which would not be guaranteed if an internal floating point representation 65 * were used. Note that if you're looking for U.S. letter sized media in metric 66 * units, you have to search for a media size of 215.9 x 279.4 mm; rounding off 67 * to an integral 216 x 279 mm will not match. 68 * <P> 69 * The exported constant {@link #INCH INCH} is actually the 70 * conversion factor by which to multiply a value in inches to get the value in 71 * µm. Likewise, the exported constant {@link #MM MM} is the 72 * conversion factor by which to multiply a value in mm to get the value in 73 * µm. A client can specify a resolution value in units other than inches 74 * or mm by supplying its own conversion factor. However, since the internal 75 * units of µm was chosen with supporting only the external units of inch 76 * and mm in mind, there is no guarantee that the conversion factor for the 77 * client's units will be an exact integer. If the conversion factor isn't an 78 * exact integer, resolution values in the client's units won't be stored 79 * precisely. 80 * 81 * @author Alan Kaminsky 82 */ 83 public abstract class Size2DSyntax implements Serializable, Cloneable { 84 85 private static final long serialVersionUID = 5584439964938660530L; 86 87 /** 88 * X dimension in units of micrometers (µm). 89 * @serial 90 */ 91 private int x; 92 93 /** 94 * Y dimension in units of micrometers (µm). 95 * @serial 96 */ 97 private int y; 98 99 /** 100 * Value to indicate units of inches (in). It is actually the conversion 101 * factor by which to multiply inches to yield µm (25400). 102 */ 103 public static final int INCH = 25400; 104 105 /** 106 * Value to indicate units of millimeters (mm). It is actually the 107 * conversion factor by which to multiply mm to yield µm (1000). 108 */ 109 public static final int MM = 1000; 110 111 112 /** 113 * Construct a new two-dimensional size attribute from the given 114 * floating-point values. 115 * 116 * @param x X dimension. 117 * @param y Y dimension. 118 * @param units 119 * Unit conversion factor, e.g. {@link #INCH INCH} or 120 * {@link #MM MM}. 121 * 122 * @exception IllegalArgumentException 123 * (Unchecked exception) Thrown if {@code x < 0} or {@code y < 0} or 124 * {@code units < 1}. 125 */ 126 protected Size2DSyntax(float x, float y, int units) { 127 if (x < 0.0f) { 128 throw new IllegalArgumentException("x < 0"); 129 } 130 if (y < 0.0f) { 131 throw new IllegalArgumentException("y < 0"); 132 } 133 if (units < 1) { 134 throw new IllegalArgumentException("units < 1"); 135 } 136 this.x = (int) (x * units + 0.5f); 137 this.y = (int) (y * units + 0.5f); 138 } 139 140 /** 141 * Construct a new two-dimensional size attribute from the given integer 142 * values. 143 * 144 * @param x X dimension. 145 * @param y Y dimension. 146 * @param units 147 * Unit conversion factor, e.g. {@link #INCH INCH} or 148 * {@link #MM MM}. 149 * 150 * @exception IllegalArgumentException 151 * (Unchecked exception) Thrown if {@code x < 0} or {@code y < 0} 152 * or {@code units < 1}. 153 */ 154 protected Size2DSyntax(int x, int y, int units) { 155 if (x < 0) { 156 throw new IllegalArgumentException("x < 0"); 157 } 158 if (y < 0) { 159 throw new IllegalArgumentException("y < 0"); 160 } 161 if (units < 1) { 162 throw new IllegalArgumentException("units < 1"); 163 } 164 this.x = x * units; 165 this.y = y * units; 166 } 167 168 /** 169 * Convert a value from micrometers to some other units. The result is 170 * returned as a floating-point number. 171 * 172 * @param x 173 * Value (micrometers) to convert. 174 * @param units 175 * Unit conversion factor, e.g. {@link #INCH <CODE>INCH</CODE>} or 176 * {@link #MM <CODE>MM</CODE>}. 177 * 178 * @return The value of <CODE>x</CODE> converted to the desired units. 179 * 180 * @exception IllegalArgumentException 181 * (unchecked exception) Thrown if <CODE>units</CODE> < 1. 182 */ 183 private static float convertFromMicrometers(int x, int units) { 184 if (units < 1) { 185 throw new IllegalArgumentException("units is < 1"); 186 } 187 return ((float)x) / ((float)units); 188 } 189 190 /** 191 * Get this two-dimensional size attribute's dimensions in the given units 192 * as floating-point values. 193 * 194 * @param units 195 * Unit conversion factor, e.g. {@link #INCH INCH} or {@link #MM MM}. 196 * 197 * @return A two-element array with the X dimension at index 0 and the Y 198 * dimension at index 1. 199 * 200 * @exception IllegalArgumentException 201 * (unchecked exception) Thrown if {@code units < 1}. 202 */ 203 public float[] getSize(int units) { 204 return new float[] {getX(units), getY(units)}; 205 } 206 207 /** 208 * Returns this two-dimensional size attribute's X dimension in the given 209 * units as a floating-point value. 210 * 211 * @param units 212 * Unit conversion factor, e.g. {@link #INCH INCH} or {@link #MM MM}. 213 * 214 * @return X dimension. 215 * 216 * @exception IllegalArgumentException 217 * (unchecked exception) Thrown if {@code units < 1}. 218 */ 219 public float getX(int units) { 220 return convertFromMicrometers(x, units); 221 } 222 223 /** 224 * Returns this two-dimensional size attribute's Y dimension in the given 225 * units as a floating-point value. 226 * 227 * @param units 228 * Unit conversion factor, e.g. {@link #INCH INCH} or {@link #MM MM}. 229 * 230 * @return Y dimension. 231 * 232 * @exception IllegalArgumentException 233 * (unchecked exception) Thrown if {@code units < 1}. 234 */ 235 public float getY(int units) { 236 return convertFromMicrometers(y, units); 237 } 238 239 /** 240 * Returns a string version of this two-dimensional size attribute in the 241 * given units. The string takes the form <CODE>"<I>X</I>x<I>Y</I> 242 * <I>U</I>"</CODE>, where <I>X</I> is the X dimension, <I>Y</I> is the Y 243 * dimension, and <I>U</I> is the units name. The values are displayed in 244 * floating point. 245 * 246 * @param units 247 * Unit conversion factor, e.g. {@link #INCH INCH} or {@link #MM MM}. 248 * 249 * @param unitsName 250 * Units name string, e.g. {@code in} or {@code mm}. If 251 * null, no units name is appended to the result. 252 * 253 * @return String version of this two-dimensional size attribute. 254 * 255 * @exception IllegalArgumentException 256 * (unchecked exception) Thrown if {@code units < 1}. 257 */ 258 public String toString(int units, String unitsName) { 259 StringBuffer result = new StringBuffer(); 260 result.append(getX (units)); 261 result.append('x'); 262 result.append(getY (units)); 263 if (unitsName != null) { 264 result.append(' '); 265 result.append(unitsName); 266 } 267 return result.toString(); 268 } 269 270 /** 271 * Returns whether this two-dimensional size attribute is equivalent to the 272 * passed in object. To be equivalent, all of the following conditions must 273 * be true: 274 * <OL TYPE=1> 275 * <LI> 276 * <CODE>object</CODE> is not null. 277 * <LI> 278 * <CODE>object</CODE> is an instance of class Size2DSyntax. 279 * <LI> 280 * This attribute's X dimension is equal to <CODE>object</CODE>'s X 281 * dimension. 282 * <LI> 283 * This attribute's Y dimension is equal to <CODE>object</CODE>'s Y 284 * dimension. 285 * </OL> 286 * 287 * @param object Object to compare to. 288 * 289 * @return True if <CODE>object</CODE> is equivalent to this 290 * two-dimensional size attribute, false otherwise. 291 */ 292 public boolean equals(Object object) { 293 return(object != null && 294 object instanceof Size2DSyntax && 295 this.x == ((Size2DSyntax) object).x && 296 this.y == ((Size2DSyntax) object).y); 297 } 298 299 /** 300 * Returns a hash code value for this two-dimensional size attribute. 301 */ 302 public int hashCode() { 303 return (((x & 0x0000FFFF) ) | 304 ((y & 0x0000FFFF) << 16)); 305 } 306 307 /** 308 * Returns a string version of this two-dimensional size attribute. The 309 * string takes the form <CODE>"<I>X</I>x<I>Y</I> um"</CODE>, where 310 * <I>X</I> is the X dimension and <I>Y</I> is the Y dimension. 311 * The values are reported in the internal units of micrometers. 312 */ 313 public String toString() { 314 StringBuffer result = new StringBuffer(); 315 result.append(x); 316 result.append('x'); 317 result.append(y); 318 result.append(" um"); 319 return result.toString(); 320 } 321 322 /** 323 * Returns this two-dimensional size attribute's X dimension in units of 324 * micrometers (µm). (For use in a subclass.) 325 * 326 * @return X dimension (µm). 327 */ 328 protected int getXMicrometers(){ 329 return x; 330 } 331 332 /** 333 * Returns this two-dimensional size attribute's Y dimension in units of 334 * micrometers (µm). (For use in a subclass.) 335 * 336 * @return Y dimension (µm). 337 */ 338 protected int getYMicrometers() { 339 return y; 340 } 341 342 }