ResolutionSyntax.java 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /* ResolutionSyntax.java --
  2. Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package javax.print.attribute;
  32. import java.io.Serializable;
  33. /**
  34. * <code>ResolutionSyntax</code> is the abstract base class of all attribute
  35. * classes which provide a resolution as value (e.g. printer resolution).
  36. * <p>
  37. * A <code>ResolutionSyntax</code> instance consists of two integer values
  38. * describing the resolution in feed and cross feed direction. The units of
  39. * the given values is determined by two defined constants:
  40. * <ul>
  41. * <li>DPCM - dots per centimeter</li>
  42. * <li>DPI - dots per inch</li>
  43. * </ul>
  44. * </p>
  45. * <p>
  46. * A resolutions attribute is constructed by two values for the resolution and
  47. * one of the two constants defining the actual units of the given values.
  48. * </p>
  49. * <p>
  50. * There are different methods provided to return the resolution values in
  51. * either of the both units and to compare if a resolution is less than or
  52. * equal to a given other resolution attribute.
  53. * </p>
  54. * <p>
  55. * <b>Internal storage:</b><br>
  56. * The resolutions are stored internally as dots per 100 inches (dphi). The
  57. * values of the provided constants for dots per inch (value 100) and dots
  58. * per centimeter (value 254) are used as conversion factors to the internal
  59. * storage units. To get the internal dphi values a multiplication of a given
  60. * resolution value with its units constant value is needed. Retrieving the
  61. * resolution for specific units is done by dividing the internal stored
  62. * value through the units constant value. Clients are therefore able to
  63. * provide their own resolution units by supplying other conversion factors.
  64. * Subclasses of <code>ResolutionSyntax</code> have access to the internal
  65. * resolution values through the protected methods
  66. * {@link #getCrossFeedResolutionDphi()} and {@link #getFeedResolutionDphi()}.
  67. * </p>
  68. *
  69. * @author Michael Koch (konqueror@gmx.de)
  70. */
  71. public abstract class ResolutionSyntax
  72. implements Cloneable, Serializable
  73. {
  74. private static final long serialVersionUID = 2706743076526672017L;
  75. /**
  76. * Constant for units of dots per centimeter.
  77. */
  78. public static final int DPCM = 254;
  79. /**
  80. * Constant for units of dots per inch
  81. */
  82. public static final int DPI = 100;
  83. private int crossFeedResolution;
  84. private int feedResolution;
  85. /**
  86. * Creates a <code>ResolutionSyntax</code> object with the given arguments.
  87. *
  88. * @param crossFeedResolution the cross feed resolution
  89. * @param feedResolution the feed resolution
  90. * @param units the unit to use (e.g. {@link #DPCM} or {@link #DPI})
  91. *
  92. * @exception IllegalArgumentException if preconditions fail
  93. */
  94. public ResolutionSyntax(int crossFeedResolution, int feedResolution,
  95. int units)
  96. {
  97. if (crossFeedResolution < 1
  98. || feedResolution < 1
  99. || units < 1)
  100. throw new IllegalArgumentException("no argument may be less than 1");
  101. this.crossFeedResolution = crossFeedResolution * units;
  102. this.feedResolution = feedResolution * units;
  103. }
  104. /**
  105. * Tests if the given object is equal to this object.
  106. *
  107. * @param obj the object to test
  108. *
  109. * @return <code>true</code> if both objects are equal,
  110. * <code>false</code> otherwise.
  111. */
  112. public boolean equals(Object obj)
  113. {
  114. if(! (obj instanceof ResolutionSyntax))
  115. return false;
  116. ResolutionSyntax tmp = (ResolutionSyntax) obj;
  117. return (crossFeedResolution == tmp.getCrossFeedResolutionDphi()
  118. && feedResolution == tmp.getFeedResolutionDphi());
  119. }
  120. /**
  121. * Returns the cross feed resolution for the given units.
  122. *
  123. * @param units the unit to use (e.g. {@link #DPCM} or {@link #DPI})
  124. * @return The resolution for the given units.
  125. *
  126. * @exception IllegalArgumentException if units &lt; 1
  127. */
  128. public int getCrossFeedResolution(int units)
  129. {
  130. if (units < 1)
  131. throw new IllegalArgumentException("units may not be less then 1");
  132. return crossFeedResolution / units;
  133. }
  134. /**
  135. * Returns the raw cross feed resolution in dots per 100 inches.
  136. *
  137. * @return The raw resolution.
  138. */
  139. protected int getCrossFeedResolutionDphi()
  140. {
  141. return crossFeedResolution;
  142. }
  143. /**
  144. * Returns the feed resolution for the given units.
  145. *
  146. * @param units the unit to use (e.g. {@link #DPCM} or {@link #DPI})
  147. * @return The resolution for the given units.
  148. *
  149. * @exception IllegalArgumentException if units &lt; 1
  150. */
  151. public int getFeedResolution(int units)
  152. {
  153. if (units < 1)
  154. throw new IllegalArgumentException("units may not be less then 1");
  155. return feedResolution / units;
  156. }
  157. /**
  158. * Returns the raw feed resolution in dots per 100 inches.
  159. *
  160. * @return The raw resolution.
  161. */
  162. protected int getFeedResolutionDphi()
  163. {
  164. return feedResolution;
  165. }
  166. /**
  167. * Returns the resolution as two field array. Index 0 is the cross feed
  168. * resolution, index 1 the feed resolution.
  169. *
  170. * @param units the units to use
  171. *
  172. * @return The array with the resolutions.
  173. */
  174. public int[] getResolution(int units)
  175. {
  176. int[] resolution = new int[2];
  177. resolution[0] = getCrossFeedResolution(units);
  178. resolution[1] = getFeedResolution(units);
  179. return resolution;
  180. }
  181. /**
  182. * Returns the hashcode for this object.
  183. *
  184. * @return The hashcode.
  185. */
  186. public int hashCode()
  187. {
  188. return crossFeedResolution + feedResolution;
  189. }
  190. /**
  191. * Checks if the given resolution attribute is a lower or equal
  192. * to this resolution object.
  193. *
  194. * @param other the resolution to check against
  195. *
  196. * @return <code>true</code> if other resolution attribute describes
  197. * a lower or equal resolution, <code>false</code> otherwise.
  198. */
  199. public boolean lessThanOrEquals(ResolutionSyntax other)
  200. {
  201. if (other == null)
  202. throw new NullPointerException("other may not be null");
  203. return (crossFeedResolution <= other.getCrossFeedResolutionDphi()
  204. && feedResolution <= other.getFeedResolutionDphi());
  205. }
  206. /**
  207. * Returns the string representation for this object.
  208. * <p>
  209. * The returned string is in the form "CxF dphi" with C standing
  210. * for the cross feed and F for the feed direction resolution.
  211. * Units used are dots per 100 inches (dphi).
  212. * </p>
  213. * @return The string representation.
  214. */
  215. public String toString()
  216. {
  217. return toString(1, "dphi");
  218. }
  219. /**
  220. * Returns the string representation for this object.
  221. * <p>
  222. * The returned string is in the form "CxF U" with C standing
  223. * for the cross feed and F for the feed direction resolution.
  224. * U denotes the units name if one is supplied.
  225. * </p>
  226. *
  227. * @param units the units to use
  228. * @param unitsName the name of the units. If <code>null</code>
  229. * it is ommitted from the string representation.
  230. *
  231. * @return The string representation.
  232. */
  233. public String toString(int units, String unitsName)
  234. {
  235. if (unitsName == null)
  236. return getCrossFeedResolution(units) + "x" + getFeedResolution(units);
  237. return ("" + getCrossFeedResolution(units)
  238. + "x" + getFeedResolution(units)
  239. + " " + unitsName);
  240. }
  241. }