1. /*
  2. * @(#)Enum.java 1.12 04/06/08
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.lang;
  8. import java.io.Serializable;
  9. /**
  10. * This is the common base class of all Java language enumeration types.
  11. *
  12. * @author Josh Bloch
  13. * @author Neal Gafter
  14. * @version 1.12, 06/08/04
  15. * @since 1.5
  16. */
  17. public abstract class Enum<E extends Enum<E>>
  18. implements Comparable<E>, Serializable {
  19. /**
  20. * The name of this enum constant, as declared in the enum declaration.
  21. * Most programmers should use the {@link #toString} method rather than
  22. * accessing this field.
  23. */
  24. private final String name;
  25. /**
  26. * Returns the name of this enum constant, exactly as declared in its
  27. * enum declaration.
  28. *
  29. * <b>Most programmers should use the {@link #toString} method in
  30. * preference to this one, as the toString method may return
  31. * a more user-friendly name.</b> This method is designed primarily for
  32. * use in specialized situations where correctness depends on getting the
  33. * exact name, which will not vary from release to release.
  34. *
  35. * @return the name of this enum constant
  36. */
  37. public final String name() {
  38. return name;
  39. }
  40. /**
  41. * The ordinal of this enumeration constant (its position
  42. * in the enum declaration, where the initial constant is assigned
  43. * an ordinal of zero).
  44. *
  45. * Most programmers will have no use for this field. It is designed
  46. * for use by sophisticated enum-based data structures, such as
  47. * {@link java.util.EnumSet} and {@link java.util.EnumMap}.
  48. */
  49. private final int ordinal;
  50. /**
  51. * Returns the ordinal of this enumeration constant (its position
  52. * in its enum declaration, where the initial constant is assigned
  53. * an ordinal of zero).
  54. *
  55. * Most programmers will have no use for this method. It is
  56. * designed for use by sophisticated enum-based data structures, such
  57. * as {@link java.util.EnumSet} and {@link java.util.EnumMap}.
  58. *
  59. * @return the ordinal of this enumeration constant
  60. */
  61. public final int ordinal() {
  62. return ordinal;
  63. }
  64. /**
  65. * Sole constructor. Programmers cannot invoke this constructor.
  66. * It is for use by code emitted by the compiler in response to
  67. * enum type declarations.
  68. *
  69. * @param name - The name of this enum constant, which is the identifier
  70. * used to declare it.
  71. * @param ordinal - The ordinal of this enumeration constant (its position
  72. * in the enum declaration, where the initial constant is assigned
  73. * an ordinal of zero).
  74. */
  75. protected Enum(String name, int ordinal) {
  76. this.name = name;
  77. this.ordinal = ordinal;
  78. }
  79. /**
  80. * Returns the name of this enum constant, as contained in the
  81. * declaration. This method may be overridden, though it typically
  82. * isn't necessary or desirable. An enum type should override this
  83. * method when a more "programmer-friendly" string form exists.
  84. *
  85. * @return the name of this enum constant
  86. */
  87. public String toString() {
  88. return name;
  89. }
  90. /**
  91. * Returns true if the specified object is equal to this
  92. * enum constant.
  93. *
  94. * @param other the object to be compared for equality with this object.
  95. * @return true if the specified object is equal to this
  96. * enum constant.
  97. */
  98. public final boolean equals(Object other) {
  99. return this==other;
  100. }
  101. /**
  102. * Returns a hash code for this enum constant.
  103. *
  104. * @return a hash code for this enum constant.
  105. */
  106. public final int hashCode() {
  107. return System.identityHashCode(this);
  108. }
  109. /**
  110. * Throws CloneNotSupportedException. This guarantees that enums
  111. * are never cloned, which is necessary to preserve their "singleton"
  112. * status.
  113. *
  114. * @return (never returns)
  115. */
  116. protected final Object clone() throws CloneNotSupportedException {
  117. throw new CloneNotSupportedException();
  118. }
  119. /**
  120. * Compares this enum with the specified object for order. Returns a
  121. * negative integer, zero, or a positive integer as this object is less
  122. * than, equal to, or greater than the specified object.
  123. *
  124. * Enum constants are only comparable to other enum constants of the
  125. * same enum type. The natural order implemented by this
  126. * method is the order in which the constants are declared.
  127. */
  128. public final int compareTo(E o) {
  129. Enum other = (Enum)o;
  130. Enum self = this;
  131. if (self.getClass() != other.getClass() && // optimization
  132. self.getDeclaringClass() != other.getDeclaringClass())
  133. throw new ClassCastException();
  134. return self.ordinal - other.ordinal;
  135. }
  136. /**
  137. * Returns the Class object corresponding to this enum constant's
  138. * enum type. Two enum constants e1 and e2 are of the
  139. * same enum type if and only if
  140. * e1.getDeclaringClass() == e2.getDeclaringClass().
  141. * (The value returned by this method may differ from the one returned
  142. * by the {@link Object#getClass} method for enum constants with
  143. * constant-specific class bodies.)
  144. *
  145. * @return the Class object corresponding to this enum constant's
  146. * enum type
  147. */
  148. public final Class<E> getDeclaringClass() {
  149. Class clazz = getClass();
  150. Class zuper = clazz.getSuperclass();
  151. return (zuper == Enum.class) ? clazz : zuper;
  152. }
  153. /**
  154. * Returns the enum constant of the specified enum type with the
  155. * specified name. The name must match exactly an identifier used
  156. * to declare an enum constant in this type. (Extraneous whitespace
  157. * characters are not permitted.)
  158. *
  159. * @param enumType the <tt>Class</tt> object of the enum type from which
  160. * to return a constant
  161. * @param name the name of the constant to return
  162. * @return the enum constant of the specified enum type with the
  163. * specified name
  164. * @throws IllegalArgumentException if the specified enum type has
  165. * no constant with the specified name, or the specified
  166. * class object does not represent an enum type
  167. * @throws NullPointerException if <tt>enumType</tt> or <tt>name</tt>
  168. * is null
  169. * @since 1.5
  170. */
  171. public static <T extends Enum<T>> T valueOf(Class<T> enumType,
  172. String name) {
  173. T result = enumType.enumConstantDirectory().get(name);
  174. if (result != null)
  175. return result;
  176. if (name == null)
  177. throw new NullPointerException("Name is null");
  178. throw new IllegalArgumentException(
  179. "No enum const " + enumType +"." + name);
  180. }
  181. }