Enum.java 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /* Enum.java - Base class for all enums
  2. Copyright (C) 2004, 2005 Free Software Foundation
  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 java.lang;
  32. import java.io.Serializable;
  33. import java.lang.reflect.Field;
  34. /**
  35. * This class represents a Java enumeration. All enumerations are
  36. * subclasses of this class.
  37. *
  38. * @author Tom Tromey (tromey@redhat.com)
  39. * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
  40. * @since 1.5
  41. */
  42. public abstract class Enum<T extends Enum<T>>
  43. implements Comparable<T>, Serializable
  44. {
  45. /**
  46. * For compatability with Sun's JDK
  47. */
  48. private static final long serialVersionUID = -4300926546619394005L;
  49. /**
  50. * The name of this enum constant.
  51. */
  52. final String name;
  53. /**
  54. * The number of this enum constant. Each constant is given a number
  55. * which matches the order in which it was declared, starting with zero.
  56. */
  57. final int ordinal;
  58. /**
  59. * This constructor is used by the compiler to create enumeration constants.
  60. *
  61. * @param name the name of the enumeration constant.
  62. * @param ordinal the number of the enumeration constant, based on the
  63. * declaration order of the constants and starting from zero.
  64. */
  65. protected Enum(String name, int ordinal)
  66. {
  67. this.name = name;
  68. this.ordinal = ordinal;
  69. }
  70. /**
  71. * Returns an Enum for a enum class given a description string of
  72. * the enum constant.
  73. *
  74. * @exception NullPointerException when etype or s are null.
  75. * @exception IllegalArgumentException when there is no value s in
  76. * the enum etype.
  77. */
  78. public static <S extends Enum<S>> S valueOf(Class<S> etype, String s)
  79. {
  80. if (etype == null || s == null)
  81. throw new NullPointerException();
  82. try
  83. {
  84. // XXX We should not use getDeclaredField, because it does
  85. // an unnecessary security check.
  86. Field f = etype.getDeclaredField(s);
  87. if (! f.isEnumConstant())
  88. throw new IllegalArgumentException(s);
  89. Class.setAccessible(f);
  90. @SuppressWarnings("unchecked")
  91. S val = (S) f.get(null);
  92. return val;
  93. }
  94. catch (NoSuchFieldException exception)
  95. {
  96. throw new IllegalArgumentException(s);
  97. }
  98. catch (IllegalAccessException exception)
  99. {
  100. throw new Error("Unable to access Enum class");
  101. }
  102. }
  103. /**
  104. * Returns true if this enumeration is equivalent to the supplied object,
  105. * <code>o</code>. Only one instance of an enumeration constant exists,
  106. * so the comparison is simply done using <code>==</code>.
  107. *
  108. * @param o the object to compare to this.
  109. * @return true if <code>this == o</code>.
  110. */
  111. public final boolean equals(Object o)
  112. {
  113. // Enum constants are singular, so we need only compare `=='.
  114. return this == o;
  115. }
  116. /**
  117. * Returns the hash code of this constant. This is simply the ordinal.
  118. *
  119. * @return the hash code of this enumeration constant.
  120. */
  121. public final int hashCode()
  122. {
  123. return ordinal;
  124. }
  125. /**
  126. * Returns a textual representation of this enumeration constant.
  127. * By default, this is simply the declared name of the constant, but
  128. * specific enumeration types may provide an implementation more suited
  129. * to the data being stored.
  130. *
  131. * @return a textual representation of this constant.
  132. */
  133. public String toString()
  134. {
  135. return name;
  136. }
  137. /**
  138. * Returns an integer which represents the relative ordering of this
  139. * enumeration constant. Enumeration constants are ordered by their
  140. * ordinals, which represents their declaration order. So, comparing
  141. * two identical constants yields zero, while one declared prior to
  142. * this returns a positive integer and one declared after yields a
  143. * negative integer.
  144. *
  145. * @param e the enumeration constant to compare.
  146. * @return a negative integer if <code>e.ordinal < this.ordinal</code>,
  147. * zero if <code>e.ordinal == this.ordinal</code> and a positive
  148. * integer if <code>e.ordinal > this.ordinal</code>.
  149. * @throws ClassCastException if <code>e</code> is not an enumeration
  150. * constant of the same class.
  151. */
  152. public final int compareTo(T e)
  153. {
  154. if (getDeclaringClass() != e.getDeclaringClass())
  155. throw new ClassCastException();
  156. return ordinal - e.ordinal;
  157. }
  158. /**
  159. * Cloning of enumeration constants is prevented, to maintain their
  160. * singleton status.
  161. *
  162. * @return the cloned object.
  163. * @throws CloneNotSupportedException as enumeration constants can't be
  164. * cloned.
  165. */
  166. protected final Object clone() throws CloneNotSupportedException
  167. {
  168. throw new CloneNotSupportedException("can't clone an enum constant");
  169. }
  170. /**
  171. * Returns the name of this enumeration constant.
  172. *
  173. * @return the name of the constant.
  174. */
  175. public final String name()
  176. {
  177. return name;
  178. }
  179. /**
  180. * Returns the number of this enumeration constant, which represents
  181. * the order in which it was originally declared, starting from zero.
  182. *
  183. * @return the number of this constant.
  184. */
  185. public final int ordinal()
  186. {
  187. return ordinal;
  188. }
  189. /**
  190. * Returns the type of this enumeration constant. This is the class
  191. * corresponding to the declaration of the enumeration.
  192. *
  193. * @return the type of this enumeration constant.
  194. */
  195. public final Class<T> getDeclaringClass()
  196. {
  197. Class k = getClass();
  198. // We might be in an anonymous subclass of the enum class, so go
  199. // up one more level.
  200. if (k.getSuperclass() != Enum.class)
  201. k = k.getSuperclass();
  202. return k;
  203. }
  204. /**
  205. * Enumerations can not have finalization methods.
  206. *
  207. * @since 1.6
  208. */
  209. protected final void finalize()
  210. {
  211. }
  212. }