1. /*
  2. * @(#)Size2DSyntax.java 1.5 04/01/07
  3. *
  4. * Copyright 2004 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. private static final long serialVersionUID = 5584439964938660530L;
  64. /**
  65. * X dimension in units of micrometers (µm).
  66. * @serial
  67. */
  68. private int x;
  69. /**
  70. * Y dimension in units of micrometers (µm).
  71. * @serial
  72. */
  73. private int y;
  74. /**
  75. * Value to indicate units of inches (in). It is actually the conversion
  76. * factor by which to multiply inches to yield µm (25400).
  77. */
  78. public static final int INCH = 25400;
  79. /**
  80. * Value to indicate units of millimeters (mm). It is actually the
  81. * conversion factor by which to multiply mm to yield µm (1000).
  82. */
  83. public static final int MM = 1000;
  84. /**
  85. * Construct a new two-dimensional size attribute from the given
  86. * floating-point values.
  87. *
  88. * @param x X dimension.
  89. * @param y Y dimension.
  90. * @param units
  91. * Unit conversion factor, e.g. {@link #INCH <CODE>INCH</CODE>} or
  92. * {@link #MM <CODE>MM</CODE>}.
  93. *
  94. * @exception IllegalArgumentException
  95. * (Unchecked exception) Thrown if <CODE>x</CODE> < 0 or <CODE>y</CODE>
  96. * < 0 or <CODE>units</CODE> < 1.
  97. */
  98. protected Size2DSyntax(float x, float y, int units) {
  99. if (x < 0.0f) {
  100. throw new IllegalArgumentException("x < 0");
  101. }
  102. if (y < 0.0f) {
  103. throw new IllegalArgumentException("y < 0");
  104. }
  105. if (units < 1) {
  106. throw new IllegalArgumentException("units < 1");
  107. }
  108. this.x = (int) (x * units + 0.5f);
  109. this.y = (int) (y * units + 0.5f);
  110. }
  111. /**
  112. * Construct a new two-dimensional size attribute from the given integer
  113. * values.
  114. *
  115. * @param x X dimension.
  116. * @param y Y dimension.
  117. * @param units
  118. * Unit conversion factor, e.g. {@link #INCH <CODE>INCH</CODE>} or
  119. * {@link #MM <CODE>MM</CODE>}.
  120. *
  121. * @exception IllegalArgumentException
  122. * (Unchecked exception) Thrown if <CODE>x</CODE> < 0 or <CODE>y</CODE>
  123. * < 0 or <CODE>units</CODE> < 1.
  124. */
  125. protected Size2DSyntax(int x, int y, int units) {
  126. if (x < 0) {
  127. throw new IllegalArgumentException("x < 0");
  128. }
  129. if (y < 0) {
  130. throw new IllegalArgumentException("y < 0");
  131. }
  132. if (units < 1) {
  133. throw new IllegalArgumentException("units < 1");
  134. }
  135. this.x = x * units;
  136. this.y = y * units;
  137. }
  138. /**
  139. * Convert a value from micrometers to some other units. The result is
  140. * returned as a floating-point number.
  141. *
  142. * @param x
  143. * Value (micrometers) to convert.
  144. * @param units
  145. * Unit conversion factor, e.g. {@link #INCH <CODE>INCH</CODE>} or
  146. * {@link #MM <CODE>MM</CODE>}.
  147. *
  148. * @return The value of <CODE>x</CODE> converted to the desired units.
  149. *
  150. * @exception IllegalArgumentException
  151. * (unchecked exception) Thrown if <CODE>units</CODE> < 1.
  152. */
  153. private static float convertFromMicrometers(int x, int units) {
  154. if (units < 1) {
  155. throw new IllegalArgumentException("units is < 1");
  156. }
  157. return ((float)x) / ((float)units);
  158. }
  159. /**
  160. * Get this two-dimensional size attribute's dimensions in the given units
  161. * as floating-point values.
  162. *
  163. * @param units
  164. * Unit conversion factor, e.g. {@link #INCH <CODE>INCH</CODE>} or
  165. * {@link #MM <CODE>MM</CODE>}.
  166. *
  167. * @return A two-element array with the X dimension at index 0 and the Y
  168. * dimension at index 1.
  169. *
  170. * @exception IllegalArgumentException
  171. * (unchecked exception) Thrown if <CODE>units</CODE> < 1.
  172. */
  173. public float[] getSize(int units) {
  174. return new float[] {getX(units), getY(units)};
  175. }
  176. /**
  177. * Returns this two-dimensional size attribute's X dimension in the given
  178. * units as a floating-point value.
  179. *
  180. * @param units
  181. * Unit conversion factor, e.g. {@link #INCH <CODE>INCH</CODE>} or
  182. * {@link #MM <CODE>MM</CODE>}.
  183. *
  184. * @return X dimension.
  185. *
  186. * @exception IllegalArgumentException
  187. * (unchecked exception) Thrown if <CODE>units</CODE> < 1.
  188. */
  189. public float getX(int units) {
  190. return convertFromMicrometers(x, units);
  191. }
  192. /**
  193. * Returns this two-dimensional size attribute's Y dimension in the given
  194. * units as a floating-point value.
  195. *
  196. * @param units
  197. * Unit conversion factor, e.g. {@link #INCH <CODE>INCH</CODE>} or
  198. * {@link #MM <CODE>MM</CODE>}.
  199. *
  200. * @return Y dimension.
  201. *
  202. * @exception IllegalArgumentException
  203. * (unchecked exception) Thrown if <CODE>units</CODE> < 1.
  204. */
  205. public float getY(int units) {
  206. return convertFromMicrometers(y, units);
  207. }
  208. /**
  209. * Returns a string version of this two-dimensional size attribute in the
  210. * given units. The string takes the form <CODE>"<I>X</I>x<I>Y</I>
  211. * <I>U</I>"</CODE>, where <I>X</I> is the X dimension, <I>Y</I> is the Y
  212. * dimension, and <I>U</I> is the units name. The values are displayed in
  213. * floating point.
  214. *
  215. * @param units
  216. * Unit conversion factor, e.g. {@link #INCH <CODE>INCH</CODE>} or
  217. * {@link #MM <CODE>MM</CODE>}.
  218. * @param unitsName
  219. * Units name string, e.g. <CODE>"in"</CODE> or <CODE>"mm"</CODE>. If
  220. * null, no units name is appended to the result.
  221. *
  222. * @return String version of this two-dimensional size attribute.
  223. *
  224. * @exception IllegalArgumentException
  225. * (unchecked exception) Thrown if <CODE>units</CODE> < 1.
  226. */
  227. public String toString(int units, String unitsName) {
  228. StringBuffer result = new StringBuffer();
  229. result.append(getX (units));
  230. result.append('x');
  231. result.append(getY (units));
  232. if (unitsName != null) {
  233. result.append(' ');
  234. result.append(unitsName);
  235. }
  236. return result.toString();
  237. }
  238. /**
  239. * Returns whether this two-dimensional size attribute is equivalent to the
  240. * passed in object. To be equivalent, all of the following conditions must
  241. * be true:
  242. * <OL TYPE=1>
  243. * <LI>
  244. * <CODE>object</CODE> is not null.
  245. * <LI>
  246. * <CODE>object</CODE> is an instance of class Size2DSyntax.
  247. * <LI>
  248. * This attribute's X dimension is equal to <CODE>object</CODE>'s X
  249. * dimension.
  250. * <LI>
  251. * This attribute's Y dimension is equal to <CODE>object</CODE>'s Y
  252. * dimension.
  253. * </OL>
  254. *
  255. * @param object Object to compare to.
  256. *
  257. * @return True if <CODE>object</CODE> is equivalent to this
  258. * two-dimensional size attribute, false otherwise.
  259. */
  260. public boolean equals(Object object) {
  261. return(object != null &&
  262. object instanceof Size2DSyntax &&
  263. this.x == ((Size2DSyntax) object).x &&
  264. this.y == ((Size2DSyntax) object).y);
  265. }
  266. /**
  267. * Returns a hash code value for this two-dimensional size attribute.
  268. */
  269. public int hashCode() {
  270. return (((x & 0x0000FFFF) ) |
  271. ((y & 0x0000FFFF) << 16));
  272. }
  273. /**
  274. * Returns a string version of this two-dimensional size attribute. The
  275. * string takes the form <CODE>"<I>X</I>x<I>Y</I> um"</CODE>, where
  276. * <I>X</I> is the X dimension and <I>Y</I> is the Y dimension.
  277. * The values are reported in the internal units of micrometers.
  278. */
  279. public String toString() {
  280. StringBuffer result = new StringBuffer();
  281. result.append(x);
  282. result.append('x');
  283. result.append(y);
  284. result.append(" um");
  285. return result.toString();
  286. }
  287. /**
  288. * Returns this two-dimensional size attribute's X dimension in units of
  289. * micrometers (µm). (For use in a subclass.)
  290. *
  291. * @return X dimension (µm).
  292. */
  293. protected int getXMicrometers(){
  294. return x;
  295. }
  296. /**
  297. * Returns this two-dimensional size attribute's Y dimension in units of
  298. * micrometers (µm). (For use in a subclass.)
  299. *
  300. * @return Y dimension (µm).
  301. */
  302. protected int getYMicrometers() {
  303. return y;
  304. }
  305. }