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