src/share/classes/java/lang/Enum.java

Print this page




 156      * Throws CloneNotSupportedException.  This guarantees that enums
 157      * are never cloned, which is necessary to preserve their "singleton"
 158      * status.
 159      *
 160      * @return (never returns)
 161      */
 162     protected final Object clone() throws CloneNotSupportedException {
 163         throw new CloneNotSupportedException();
 164     }
 165 
 166     /**
 167      * Compares this enum with the specified object for order.  Returns a
 168      * negative integer, zero, or a positive integer as this object is less
 169      * than, equal to, or greater than the specified object.
 170      *
 171      * Enum constants are only comparable to other enum constants of the
 172      * same enum type.  The natural order implemented by this
 173      * method is the order in which the constants are declared.
 174      */
 175     public final int compareTo(E o) {
 176         Enum other = (Enum)o;
 177         Enum self = this;
 178         if (self.getClass() != other.getClass() && // optimization
 179             self.getDeclaringClass() != other.getDeclaringClass())
 180             throw new ClassCastException();
 181         return self.ordinal - other.ordinal;
 182     }
 183 
 184     /**
 185      * Returns the Class object corresponding to this enum constant's
 186      * enum type.  Two enum constants e1 and  e2 are of the
 187      * same enum type if and only if
 188      *   e1.getDeclaringClass() == e2.getDeclaringClass().
 189      * (The value returned by this method may differ from the one returned
 190      * by the {@link Object#getClass} method for enum constants with
 191      * constant-specific class bodies.)
 192      *
 193      * @return the Class object corresponding to this enum constant's
 194      *     enum type
 195      */

 196     public final Class<E> getDeclaringClass() {
 197         Class clazz = getClass();
 198         Class zuper = clazz.getSuperclass();
 199         return (zuper == Enum.class) ? clazz : zuper;
 200     }
 201 
 202     /**
 203      * Returns the enum constant of the specified enum type with the
 204      * specified name.  The name must match exactly an identifier used
 205      * to declare an enum constant in this type.  (Extraneous whitespace
 206      * characters are not permitted.)
 207      *
 208      * <p>Note that for a particular enum type {@code T}, the
 209      * implicitly declared {@code public static T valueOf(String)}
 210      * method on that enum may be used instead of this method to map
 211      * from a name to the corresponding enum constant.  All the
 212      * constants of an enum type can be obtained by calling the
 213      * implicit {@code public static T[] values()} method of that
 214      * type.
 215      *
 216      * @param <T> The enum type whose constant is to be returned
 217      * @param enumType the {@code Class} object of the enum type from which
 218      *      to return a constant
 219      * @param name the name of the constant to return




 156      * Throws CloneNotSupportedException.  This guarantees that enums
 157      * are never cloned, which is necessary to preserve their "singleton"
 158      * status.
 159      *
 160      * @return (never returns)
 161      */
 162     protected final Object clone() throws CloneNotSupportedException {
 163         throw new CloneNotSupportedException();
 164     }
 165 
 166     /**
 167      * Compares this enum with the specified object for order.  Returns a
 168      * negative integer, zero, or a positive integer as this object is less
 169      * than, equal to, or greater than the specified object.
 170      *
 171      * Enum constants are only comparable to other enum constants of the
 172      * same enum type.  The natural order implemented by this
 173      * method is the order in which the constants are declared.
 174      */
 175     public final int compareTo(E o) {
 176         Enum<?> other = (Enum<?>)o;
 177         Enum<E> self = this;
 178         if (self.getClass() != other.getClass() && // optimization
 179             self.getDeclaringClass() != other.getDeclaringClass())
 180             throw new ClassCastException();
 181         return self.ordinal - other.ordinal;
 182     }
 183 
 184     /**
 185      * Returns the Class object corresponding to this enum constant's
 186      * enum type.  Two enum constants e1 and  e2 are of the
 187      * same enum type if and only if
 188      *   e1.getDeclaringClass() == e2.getDeclaringClass().
 189      * (The value returned by this method may differ from the one returned
 190      * by the {@link Object#getClass} method for enum constants with
 191      * constant-specific class bodies.)
 192      *
 193      * @return the Class object corresponding to this enum constant's
 194      *     enum type
 195      */
 196     @SuppressWarnings("unchecked")
 197     public final Class<E> getDeclaringClass() {
 198         Class<?> clazz = getClass();
 199         Class<?> zuper = clazz.getSuperclass();
 200         return (zuper == Enum.class) ? (Class<E>)clazz : (Class<E>)zuper;
 201     }
 202 
 203     /**
 204      * Returns the enum constant of the specified enum type with the
 205      * specified name.  The name must match exactly an identifier used
 206      * to declare an enum constant in this type.  (Extraneous whitespace
 207      * characters are not permitted.)
 208      *
 209      * <p>Note that for a particular enum type {@code T}, the
 210      * implicitly declared {@code public static T valueOf(String)}
 211      * method on that enum may be used instead of this method to map
 212      * from a name to the corresponding enum constant.  All the
 213      * constants of an enum type can be obtained by calling the
 214      * implicit {@code public static T[] values()} method of that
 215      * type.
 216      *
 217      * @param <T> The enum type whose constant is to be returned
 218      * @param enumType the {@code Class} object of the enum type from which
 219      *      to return a constant
 220      * @param name the name of the constant to return