EnumSyntax.java 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /* EnumSyntax.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.InvalidObjectException;
  33. import java.io.ObjectStreamException;
  34. import java.io.Serializable;
  35. /**
  36. * <code>EnumSyntax</code> is the abstract base class of all enumeration
  37. * classes in the Java Print Service API.
  38. * <p>
  39. * Every enumeration class which extends from EnumSyntax provides several
  40. * enumeration objects as singletons of its class.
  41. * </p>
  42. * <p>
  43. * Notes for implementing subclasses:
  44. * <ul>
  45. * <li>
  46. * The values of all enumeration singelton instances have to be in a
  47. * sequence which may start at any value. See: {@link #getOffset()}
  48. * </li>
  49. * <li>
  50. * Subclasses have to override {@link #getEnumValueTable()} and should
  51. * override {@link #getStringTable()} for correct serialization.
  52. * </li>
  53. * </ul>
  54. * </p>
  55. * Example:
  56. * <pre>
  57. * public class PrinterState extends EnumSyntax
  58. * {
  59. * public static final PrinterState IDLE = new PrinterState(1);
  60. * public static final PrinterState PROCESSING = new PrinterState(2);
  61. * public static final PrinterState STOPPED = new PrinterState(3);
  62. *
  63. * protected PrinterState(int value)
  64. * {
  65. * super(value);
  66. * }
  67. *
  68. * // Overridden because values start not at zero !
  69. * protected int getOffset()
  70. * {
  71. * return 1;
  72. * }
  73. *
  74. * private static final String[] stringTable = { "idle", "processing",
  75. * "stopped" };
  76. *
  77. * protected String[] getStringTable()
  78. * {
  79. * return stringTable;
  80. * }
  81. *
  82. * private static final PrinterState[] enumValueTable = { IDLE,
  83. * PROCESSING, STOPPED};
  84. *
  85. * protected EnumSyntax[] getEnumValueTable()
  86. * {
  87. * return enumValueTable;
  88. * }
  89. * }
  90. * </pre>
  91. *
  92. * @author Michael Koch (konqueror@gmx.de)
  93. * @author Wolfgang Baer (WBaer@gmx.de)
  94. */
  95. public abstract class EnumSyntax implements Cloneable, Serializable
  96. {
  97. private static final long serialVersionUID = -2739521845085831642L;
  98. private int value;
  99. /**
  100. * Creates a <code>EnumSyntax</code> object.
  101. *
  102. * @param value the value to set.
  103. */
  104. protected EnumSyntax(int value)
  105. {
  106. this.value = value;
  107. }
  108. /**
  109. * Returns the value of this enumeration object.
  110. *
  111. * @return The value.
  112. */
  113. public int getValue()
  114. {
  115. return value;
  116. }
  117. /**
  118. * Clones this object.
  119. *
  120. * @return A clone of this object.
  121. */
  122. public Object clone()
  123. {
  124. try
  125. {
  126. return super.clone();
  127. }
  128. catch (CloneNotSupportedException e)
  129. {
  130. // Cannot happen as we implement java.lang.Cloneable.
  131. return null;
  132. }
  133. }
  134. /**
  135. * Returns the hashcode for this object.
  136. * The hashcode is the value of this enumeration object.
  137. *
  138. * @return The hashcode.
  139. */
  140. public int hashCode()
  141. {
  142. return value;
  143. }
  144. /**
  145. * Returns the string representation for this object.
  146. * The string value from <code>getStringTable()</code> method is returned
  147. * if subclasses override this method. Otherwise the value of this object
  148. * as a string is returned.
  149. *
  150. * @return The string representation.
  151. */
  152. public String toString()
  153. {
  154. int index = value - getOffset();
  155. String[] table = getStringTable();
  156. if (table != null
  157. && index >= 0
  158. && index < table.length)
  159. return table[index];
  160. return "" + value;
  161. }
  162. /**
  163. * Returns a table with the enumeration values represented as strings
  164. * for this object.
  165. *
  166. * The default implementation just returns null. Subclasses should
  167. * override this method.
  168. *
  169. * @return The enumeration values as strings.
  170. */
  171. protected String[] getStringTable()
  172. {
  173. return null;
  174. }
  175. /**
  176. * Needed for singelton semantics during deserialisation.
  177. *
  178. * Subclasses must not override this class. Subclasses have to override
  179. * <code>getEnumValueTable()</code> and should override
  180. * <code>getStringTable()</code> for correct serialization.
  181. *
  182. * @return The Object at index <code>value - getOffset()</code>
  183. * in getEnumValueTable.
  184. * @throws ObjectStreamException if getEnumValueTable() returns null.
  185. */
  186. protected Object readResolve() throws ObjectStreamException
  187. {
  188. EnumSyntax[] table = getEnumValueTable();
  189. if (table == null)
  190. throw new InvalidObjectException("Null enumeration value table "
  191. + "for class "
  192. + this.getClass().toString());
  193. return table[value - getOffset()];
  194. }
  195. /**
  196. * Returns a table with the enumeration values for this object.
  197. *
  198. * The default implementation just returns null. Subclasses have to
  199. * to override this method for serialization.
  200. *
  201. * @return The enumeration values.
  202. */
  203. protected EnumSyntax[] getEnumValueTable()
  204. {
  205. return null;
  206. }
  207. /**
  208. * Returns the lowest used value by the enumerations of this class.
  209. *
  210. * The default implementation returns 0. This is enough if enumerations
  211. * start with a zero value. Otherwise subclasses need to override this
  212. * method for serialization and return the lowest value they use.
  213. * .
  214. * @return The lowest used value used.
  215. */
  216. protected int getOffset()
  217. {
  218. return 0;
  219. }
  220. }