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