Size2DSyntax.java 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /* Size2DSyntax.java --
  2. Copyright (C) 2003, 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>Size2DSyntax</code> is the abstract base class of all attribute
  35. * classes which provide a two dimensional size as value (e.g. the size of
  36. * a media like Letter or A4).
  37. * <p>
  38. * A <code>Size2DSyntax</code> instance consists of two integer values
  39. * describing the size in the x and y dimension. The units of
  40. * the given values is determined by two defined constants:
  41. * <ul>
  42. * <li>INCH - defines an inch</li>
  43. * <li>MM - defines a millimeter</li>
  44. * </ul>
  45. * </p>
  46. * <p>
  47. * A size 2D attribute is constructed by two values for the size of the x and
  48. * y dimension and the actual units of the given values as defined by the
  49. * constants.
  50. * </p>
  51. * <p>
  52. * There are different methods provided to return the size values for the
  53. * dimensions in either of the two predefined units or with a given client
  54. * supplied units conversion factor.
  55. * </p>
  56. * <p>
  57. * <b>Internal storage:</b><br>
  58. * The size of the x,y dimensions are stored internally in micrometers. The
  59. * values of the provided constants for inch (value 25400) and millimeters
  60. * (value 1000) are used as conversion factors to the internal storage units.
  61. * To get the internal micrometers values a multiplication of a given
  62. * size value with its units constant value is done. Retrieving the size value
  63. * for specific units is done by dividing the internal stored value by the
  64. * units constant value. Clients are therefore able to provide their own
  65. * size units by supplying other conversion factors.
  66. * Subclasses of <code>Size2DSyntax</code> have access to the internal
  67. * size values through the protected methods
  68. * {@link #getXMicrometers()} and {@link #getYMicrometers()}.
  69. * </p>
  70. *
  71. * @author Michael Koch (konqueror@gmx.de)
  72. */
  73. public abstract class Size2DSyntax implements Cloneable, Serializable
  74. {
  75. /**
  76. * Constant for the units of inches.
  77. * The actual value is the conversion factor to micrometers.
  78. */
  79. public static final int INCH = 25400;
  80. /**
  81. * Constant for the units of millimeters.
  82. * The actual value is the conversion factor to micrometers.
  83. */
  84. public static final int MM = 1000;
  85. /** x size in micrometers. */
  86. private int x;
  87. /** y size in micrometers. */
  88. private int y;
  89. /**
  90. * Creates a <code>Size2DSyntax</code> object with the given arguments.
  91. *
  92. * @param x the size in x direction
  93. * @param y the size in y direction
  94. * @param units the units to use for the sizes
  95. *
  96. * @exception IllegalArgumentException if x or y &lt; 0 or units &lt; 1
  97. */
  98. protected Size2DSyntax(float x, float y, int units)
  99. {
  100. if (x < 0.0f || y < 0.0f)
  101. throw new IllegalArgumentException("x and/or y may not be less than 0");
  102. if (units < 1)
  103. throw new IllegalArgumentException("units may not be less then 1");
  104. this.x = (int) (x * units + 0.5f);
  105. this.y = (int) (y * units + 0.5f);
  106. }
  107. /**
  108. * Creates a <code>Size2DSyntax</code> object with the given arguments.
  109. *
  110. * @param x the size in x direction
  111. * @param y the size in y direction
  112. * @param units the units to use for the sizes
  113. *
  114. * @exception IllegalArgumentException if x or y &lt; 0 or units &lt; 1
  115. */
  116. protected Size2DSyntax(int x, int y, int units)
  117. {
  118. if (x < 0 || y < 0)
  119. throw new IllegalArgumentException("x and/or y may not be less then 0");
  120. if (units < 1)
  121. throw new IllegalArgumentException("units may not be less then 1");
  122. this.x = x * units;
  123. this.y = y * units;
  124. }
  125. /**
  126. * Tests if the given object is equal to this object.
  127. *
  128. * @param obj the object to test
  129. *
  130. * @return <code>true</code> if both objects are equal, <code>false</code> otherwise.
  131. */
  132. public boolean equals(Object obj)
  133. {
  134. if (! (obj instanceof Size2DSyntax))
  135. return false;
  136. Size2DSyntax tmp = (Size2DSyntax) obj;
  137. return (x == tmp.getXMicrometers()
  138. && y == tmp.getYMicrometers());
  139. }
  140. /**
  141. * Returns the size described in this object as a two field array.
  142. * Index 0 contains the size in x direction, index 1 the size in
  143. * y direction.
  144. *
  145. * @param units the units to use
  146. *
  147. * @return The array with the size dimensions.
  148. *
  149. * @exception IllegalArgumentException if units &lt; 1
  150. */
  151. public float[] getSize(int units)
  152. {
  153. float[] size = new float[2];
  154. size[0] = getX(units);
  155. size[1] = getY(units);
  156. return size;
  157. }
  158. /**
  159. * Returns the size in x direction.
  160. *
  161. * @param units the units to use
  162. *
  163. * @return The size in x direction.
  164. *
  165. * @exception IllegalArgumentException if units &lt; 1
  166. */
  167. public float getX(int units)
  168. {
  169. if (units < 1)
  170. throw new IllegalArgumentException("units may not be less then 1");
  171. return ((float) x) / ((float) units);
  172. }
  173. /**
  174. * Returns the size in x direction in mircometers.
  175. * To be used by sublcasses that need access to the internal storage value.
  176. *
  177. * @return The size in x direction in micrometers.
  178. */
  179. protected int getXMicrometers()
  180. {
  181. return x;
  182. }
  183. /**
  184. * Return the size in y direction.
  185. *
  186. * @param units the units to use
  187. *
  188. * @return The size in y direction.
  189. *
  190. * @exception IllegalArgumentException if units &lt; 1
  191. */
  192. public float getY(int units)
  193. {
  194. if (units < 1)
  195. throw new IllegalArgumentException("units may not be less then 1");
  196. return ((float) y) / ((float) units);
  197. }
  198. /**
  199. * Returns the size in y direction in mircometers.
  200. * To be used by sublcasses that need access to the internal storage value.
  201. *
  202. * @return The size in y direction in micrometers.
  203. */
  204. protected int getYMicrometers()
  205. {
  206. return y;
  207. }
  208. /**
  209. * Returns the hashcode for this object.
  210. *
  211. * @return The hashcode.
  212. */
  213. public int hashCode()
  214. {
  215. return x + y;
  216. }
  217. /**
  218. * Returns the string representation for this object.
  219. * <p>
  220. * The returned string is in the form "XxY um" with X standing
  221. * for size in x and Y for the size in y direction. The used
  222. * micrometers units is indicated by the appended "um" notation.
  223. * </p>
  224. *
  225. * @return The string representation in micrometers.
  226. */
  227. public String toString()
  228. {
  229. return getXMicrometers() + "x" + getYMicrometers() + " um";
  230. }
  231. /**
  232. * Returns the string representation for this object.
  233. * <p>
  234. * The returned string is in the form "XxY U" with X standing
  235. * for size in x and Y for the size in y direction. U denotes
  236. * the units name if one is supplied. The values are given as
  237. * floating point values.
  238. * </p>
  239. *
  240. * @param units the units to use
  241. * @param unitsName the name of the units. If <code>null</code>
  242. * it is ommitted from the string representation.
  243. *
  244. * @return The string representation.
  245. */
  246. public String toString(int units, String unitsName)
  247. {
  248. if (unitsName == null)
  249. return getX(units) + "x" + getY(units);
  250. return getX(units) + "x" + getY(units) + " " + unitsName;
  251. }
  252. }