1. /*
  2. * @(#)ResolutionSyntax.java 1.5 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 ResolutionSyntax is an abstract base class providing the common
  11. * implementation of all attributes denoting a printer resolution.
  12. * <P>
  13. * A resolution attribute's value consists of two items, the cross feed
  14. * direction resolution and the feed direction resolution. A resolution
  15. * attribute may be constructed by supplying the two values and indicating the
  16. * units in which the values are measured. Methods are provided to return a
  17. * resolution attribute's values, indicating the units in which the values are
  18. * to be returned. The two most common resolution units are dots per inch (dpi)
  19. * and dots per centimeter (dpcm), and exported constants {@link #DPI
  20. * <CODE>DPI</CODE>} and {@link #DPCM <CODE>DPCM</CODE>} are provided for
  21. * indicating those units.
  22. * <P>
  23. * Once constructed, a resolution attribute's value is immutable.
  24. * <P>
  25. * <B>Design</B>
  26. * <P>
  27. * A resolution attribute's cross feed direction resolution and feed direction
  28. * resolution values are stored internally using units of dots per 100 inches
  29. * (dphi). Storing the values in dphi rather than, say, metric units allows
  30. * precise integer arithmetic conversions between dpi and dphi and between dpcm
  31. * and dphi: 1 dpi = 100 dphi, 1 dpcm = 254 dphi. Thus, the values can be stored
  32. * into and retrieved back from a resolution attribute in either units with no
  33. * loss of precision. This would not be guaranteed if a floating point
  34. * representation were used. However, roundoff error will in general occur if a
  35. * resolution attribute's values are created in one units and retrieved in
  36. * different units; for example, 600 dpi will be rounded to 236 dpcm, whereas
  37. * the true value (to five figures) is 236.22 dpcm.
  38. * <P>
  39. * Storing the values internally in common units of dphi lets two resolution
  40. * attributes be compared without regard to the units in which they were
  41. * created; for example, 300 dpcm will compare equal to 762 dpi, as they both
  42. * are stored as 76200 dphi. In particular, a lookup service can
  43. * match resolution attributes based on equality of their serialized
  44. * representations regardless of the units in which they were created. Again,
  45. * using integers for internal storage allows precise equality comparisons to be
  46. * done, which would not be guaranteed if a floating point representation were
  47. * used.
  48. * <P>
  49. * The exported constant {@link #DPI <CODE>DPI</CODE>} is actually the
  50. * conversion factor by which to multiply a value in dpi to get the value in
  51. * dphi. Likewise, the exported constant {@link #DPCM <CODE>DPCM</CODE>} is the
  52. * conversion factor by which to multiply a value in dpcm to get the value in
  53. * dphi. A client can specify a resolution value in units other than dpi or dpcm
  54. * by supplying its own conversion factor. However, since the internal units of
  55. * dphi was chosen with supporting only the external units of dpi and dpcm in
  56. * mind, there is no guarantee that the conversion factor for the client's units
  57. * will be an exact integer. If the conversion factor isn't an exact integer,
  58. * resolution values in the client's units won't be stored precisely.
  59. * <P>
  60. *
  61. * @author David Mendenhall
  62. * @author Alan Kaminsky
  63. */
  64. public abstract class ResolutionSyntax implements Serializable, Cloneable {
  65. /**
  66. * Cross feed direction resolution in units of dots per 100 inches (dphi).
  67. * @serial
  68. */
  69. private int crossFeedResolution;
  70. /**
  71. * Feed direction resolution in units of dots per 100 inches (dphi).
  72. * @serial
  73. */
  74. private int feedResolution;
  75. /**
  76. * Value to indicate units of dots per inch (dpi). It is actually the
  77. * conversion factor by which to multiply dpi to yield dphi (100).
  78. */
  79. public static final int DPI = 100;
  80. /**
  81. * Value to indicate units of dots per centimeter (dpcm). It is actually
  82. * the conversion factor by which to multiply dpcm to yield dphi (254).
  83. */
  84. public static final int DPCM = 254;
  85. /**
  86. * Construct a new resolution attribute from the given items.
  87. *
  88. * @param crossFeedResolution
  89. * Cross feed direction resolution.
  90. * @param feedResolution
  91. * Feed direction resolution.
  92. * @param units
  93. * Unit conversion factor, e.g. {@link #DPI <CODE>DPI</CODE>} or
  94. * {@link #DPCM <CODE>DPCM</CODE>}.
  95. *
  96. * @exception IllegalArgumentException
  97. * (unchecked exception) Thrown if <CODE>crossFeedResolution</CODE> <
  98. * 1 or <CODE>feedResolution</CODE> < 1 or <CODE>units</CODE> < 1.
  99. */
  100. public ResolutionSyntax(int crossFeedResolution, int feedResolution,
  101. int units) {
  102. if (crossFeedResolution < 1) {
  103. throw new IllegalArgumentException("crossFeedResolution is < 1");
  104. }
  105. if (feedResolution < 1) {
  106. throw new IllegalArgumentException("feedResolution is < 1");
  107. }
  108. if (units < 1) {
  109. throw new IllegalArgumentException("units is < 1");
  110. }
  111. this.crossFeedResolution = crossFeedResolution * units;
  112. this.feedResolution = feedResolution * units;
  113. }
  114. /**
  115. * Convert a value from dphi to some other units. The result is rounded to
  116. * the nearest integer.
  117. *
  118. * @param dphi
  119. * Value (dphi) to convert.
  120. * @param units
  121. * Unit conversion factor, e.g. {@link #DPI <CODE>DPI</CODE>} or
  122. * {@link #DPCM <CODE>DPCM</CODE>}.
  123. *
  124. * @return The value of <CODE>dphi</CODE> converted to the desired units.
  125. *
  126. * @exception IllegalArgumentException
  127. * (unchecked exception) Thrown if <CODE>units</CODE> < 1.
  128. */
  129. private static int convertFromDphi(int dphi, int units) {
  130. if (units < 1) {
  131. throw new IllegalArgumentException(": units is < 1");
  132. }
  133. int round = units / 2;
  134. return (dphi + round) / units;
  135. }
  136. /**
  137. * Get this resolution attribute's resolution values in the given units.
  138. * The values are rounded to the nearest integer.
  139. *
  140. * @param units
  141. * Unit conversion factor, e.g. {@link #DPI <CODE>DPI</CODE>} or
  142. * {@link #DPCM <CODE>DPCM</CODE>}.
  143. *
  144. * @return A two-element array with the cross feed direction resolution
  145. * at index 0 and the feed direction resolution at index 1.
  146. *
  147. * @exception IllegalArgumentException
  148. * (unchecked exception) Thrown if <CODE>units</CODE> < 1.
  149. */
  150. public int[] getResolution(int units) {
  151. return new int[] { getCrossFeedResolution(units),
  152. getFeedResolution(units)
  153. };
  154. }
  155. /**
  156. * Returns this resolution attribute's cross feed direction resolution in
  157. * the given units. The value is rounded to the nearest integer.
  158. *
  159. * @param units
  160. * Unit conversion factor, e.g. {@link #DPI <CODE>DPI</CODE>} or
  161. * {@link #DPCM <CODE>DPCM</CODE>}.
  162. *
  163. * @return Cross feed direction resolution.
  164. *
  165. * @exception IllegalArgumentException
  166. * (unchecked exception) Thrown if <CODE>units</CODE> < 1.
  167. */
  168. public int getCrossFeedResolution(int units) {
  169. return convertFromDphi (crossFeedResolution, units);
  170. }
  171. /**
  172. * Returns this resolution attribute's feed direction resolution in the
  173. * given units. The value is rounded to the nearest integer.
  174. *
  175. * @param units
  176. * Unit conversion factor, e.g. {@link #DPI <CODE>DPI</CODE>} or {@link
  177. * #DPCM <CODE>DPCM</CODE>}.
  178. *
  179. * @return Feed direction resolution.
  180. *
  181. * @exception IllegalArgumentException
  182. * (unchecked exception) Thrown if <CODE>units</CODE> < 1.
  183. */
  184. public int getFeedResolution(int units) {
  185. return convertFromDphi (feedResolution, units);
  186. }
  187. /**
  188. * Returns a string version of this resolution attribute in the given units.
  189. * The string takes the form <CODE>"<I>C</I>x<I>F</I> <I>U</I>"</CODE>,
  190. * where <I>C</I> is the cross feed direction resolution, <I>F</I> is the
  191. * feed direction resolution, and <I>U</I> is the units name. The values are
  192. * rounded to the nearest integer.
  193. *
  194. * @param units
  195. * Unit conversion factor, e.g. {@link #DPI <CODE>DPI</CODE>} or {@link
  196. * #DPCM <CODE>DPCM</CODE>}.
  197. * @param unitsName
  198. * Units name string, e.g. <CODE>"dpi"</CODE> or <CODE>"dpcm"</CODE>. If
  199. * null, no units name is appended to the result.
  200. *
  201. * @return String version of this resolution attribute.
  202. *
  203. * @exception IllegalArgumentException
  204. * (unchecked exception) Thrown if <CODE>units</CODE> < 1.
  205. */
  206. public String toString(int units, String unitsName) {
  207. StringBuffer result = new StringBuffer();
  208. result.append(getCrossFeedResolution (units));
  209. result.append('x');
  210. result.append(getFeedResolution (units));
  211. if (unitsName != null) {
  212. result.append (' ');
  213. result.append (unitsName);
  214. }
  215. return result.toString();
  216. }
  217. /**
  218. * Determine whether this resolution attribute's value is less than or
  219. * equal to the given resolution attribute's value. This is true if all
  220. * of the following conditions are true:
  221. * <UL>
  222. * <LI>
  223. * This attribute's cross feed direction resolution is less than or equal to
  224. * the <CODE>other</CODE> attribute's cross feed direction resolution.
  225. * <LI>
  226. * This attribute's feed direction resolution is less than or equal to the
  227. * <CODE>other</CODE> attribute's feed direction resolution.
  228. * </UL>
  229. *
  230. * @param other Resolution attribute to compare with.
  231. *
  232. * @return True if this resolution attribute is less than or equal to the
  233. * <CODE>other</CODE> resolution attribute, false otherwise.
  234. *
  235. * @exception NullPointerException
  236. * (unchecked exception) Thrown if <CODE>other</CODE> is null.
  237. */
  238. public boolean lessThanOrEquals(ResolutionSyntax other) {
  239. return (this.crossFeedResolution <= other.crossFeedResolution &&
  240. this.feedResolution <= other.feedResolution);
  241. }
  242. /**
  243. * Returns whether this resolution attribute is equivalent to the passed in
  244. * object. To be equivalent, all of the following conditions must be true:
  245. * <OL TYPE=1>
  246. * <LI>
  247. * <CODE>object</CODE> is not null.
  248. * <LI>
  249. * <CODE>object</CODE> is an instance of class ResolutionSyntax.
  250. * <LI>
  251. * This attribute's cross feed direction resolution is equal to
  252. * <CODE>object</CODE>'s cross feed direction resolution.
  253. * <LI>
  254. * This attribute's feed direction resolution is equal to
  255. * <CODE>object</CODE>'s feed direction resolution.
  256. * </OL>
  257. *
  258. * @param object Object to compare to.
  259. *
  260. * @return True if <CODE>object</CODE> is equivalent to this resolution
  261. * attribute, false otherwise.
  262. */
  263. public boolean equals(Object object) {
  264. return(object != null &&
  265. object instanceof ResolutionSyntax &&
  266. this.crossFeedResolution ==
  267. ((ResolutionSyntax) object).crossFeedResolution &&
  268. this.feedResolution ==
  269. ((ResolutionSyntax) object).feedResolution);
  270. }
  271. /**
  272. * Returns a hash code value for this resolution attribute.
  273. */
  274. public int hashCode() {
  275. return(((crossFeedResolution & 0x0000FFFF)) |
  276. ((feedResolution & 0x0000FFFF) << 16));
  277. }
  278. /**
  279. * Returns a string version of this resolution attribute. The string takes
  280. * the form <CODE>"<I>C</I>x<I>F</I> dphi"</CODE>, where <I>C</I> is the
  281. * cross feed direction resolution and <I>F</I> is the feed direction
  282. * resolution. The values are reported in the internal units of dphi.
  283. */
  284. public String toString() {
  285. StringBuffer result = new StringBuffer();
  286. result.append(crossFeedResolution);
  287. result.append('x');
  288. result.append(feedResolution);
  289. result.append(" dphi");
  290. return result.toString();
  291. }
  292. /**
  293. * Returns this resolution attribute's cross feed direction resolution in
  294. * units of dphi. (For use in a subclass.)
  295. *
  296. * @return Cross feed direction resolution.
  297. */
  298. protected int getCrossFeedResolutionDphi() {
  299. return crossFeedResolution;
  300. }
  301. /**
  302. * Returns this resolution attribute's feed direction resolution in units
  303. * of dphi. (For use in a subclass.)
  304. *
  305. * @return Feed direction resolution.
  306. */
  307. protected int getFeedResolutionDphi() {
  308. return feedResolution;
  309. }
  310. }