001 
002 /*
003  *  JScripter Emulation 1.0 - To Script Java
004  *  Copyright (C) 2008-2011  J.J.Liu<jianjunliu@126.com> <http://www.jscripter.org>
005  *  
006  *  This program is free software: you can redistribute it and/or modify
007  *  it under the terms of the GNU Affero General Public License as published by
008  *  the Free Software Foundation, either version 3 of the License, or
009  *  (at your option) any later version.
010  *  
011  *  This program is distributed in the hope that it will be useful,
012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014  *  GNU Affero General Public License for more details.
015  *  
016  *  You should have received a copy of the GNU Affero General Public License
017  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
018  */
019 
020 package org.jscripter.emu.java.lang;
021 
022 import org.jscripter.emu.java.io.Serializable;
023 
024 import js.*;
025 
026 /**
027  * <p>The <b>internal</b> common base class of all Java language enumeration types, emulating a 
028  * standard <tt>java.lang</tt> interface or class with the same simple name as this one.</p>
029  * <p>This interface or class is only used internally by JS re-compiler implementations.</p>
030  * <p>Please refer to <a href="http://java.sun.com/docs/">the Java API Standards</a> for detail description of the original class or interface.</p>
031  * 
032  * @author <a href="mailto:jianjunliu@126.com">J.J.Liu (Jianjun Liu)</a> at <a href="http://www.jscripter.org" target="_blank">http://www.jscripter.org</a>
033  * 
034  * @javascript Re-compilers must redirect the resolution of the emulated original Java class or interface to this one.
035  */
036 public abstract class Enum<E extends Enum<E>> implements Comparable<E>, Serializable
037 {
038     private final java.lang.String name;
039     final int ordinal;
040 
041     /**
042      * <p>Sole constructor.</p>
043      * <p>Programmers cannot invoke this constructor. It is for use by code emitted by the 
044      * Java compiler in response to <tt>enum</tt> type declarations.</p>
045      * @param name The name of this <tt>enum</tt> constant, which is the identifier used to declare it.
046      * @param ordinal The ordinal of this enumeration constant (its position in the <tt>enum</tt> 
047      * declaration, where the initial constant is assigned an ordinal of zero).
048      * @since 1.0
049      * @javascript Re-compilers must report error on end-users directly using this constructor.
050      */
051     protected Enum(java.lang.String name, int ordinal) {
052         this.name = name;
053         this.ordinal = ordinal;
054         ((js.ObjectLike)(java.lang.Object)getClass()).var(name, this);
055     }
056 
057     /**
058      * <p>Returns the enum constant of the specified enum type with the specified name.</p>
059      * <p>The name must match exactly an identifier used to declare an enum constant in this type. 
060      * (Extraneous whitespace characters are not permitted.).</p>
061      * @param enumType The Class object of the enum type from which to return a constant.
062      * @param name The name of the constant to return.
063      * @return The enum constant of the specified enum type with the specified name.
064      * @throws java.lang.IllegalArgumentException if the specified enum type has no constant with 
065      * the specified name, or the specified class object does not represent an enum type.
066      * @throws java.lang.NullPointerException if <tt>enumType</tt> or <tt>name</tt> is <tt>null</tt>.
067      * @since 1.0
068      * @javascript Re-compilers must report error on end-users directly using this method.
069      */
070     @SuppressWarnings("unchecked")
071     public static <T extends Enum<T>> T valueOf(Class<T> enumType, java.lang.String name) {
072         if (Js.not(enumType) || Js.not(name)) {
073             throw new java.lang.NullPointerException(name);
074         }
075         java.lang.Object o = ((js.ObjectLike)(java.lang.Object)enumType).var(name);
076         if (Js.not(o)) {
077             throw new java.lang.IllegalArgumentException(name);
078         }
079         return (T)o;
080     }
081 
082     /**
083      * <p>Throws java.lang.CloneNotSupportedException.</p>
084      * <p>This guarantees that enums are never cloned, which is necessary to preserve their 
085      * "singleton" status.</p>
086      * @return (never returns).
087      * @throws java.lang.CloneNotSupportedException if the object's class does not support the 
088      * {@link Cloneable} interface. Subclasses that override the {@link #clone()} method can also 
089      * throw this exception to indicate that an instance cannot be cloned.
090      * @since 1.0
091      * @javascript Re-compilers must report error on end-users directly using this method.
092      */
093     @Override
094     protected java.lang.Object clone() throws java.lang.CloneNotSupportedException {
095         throw new java.lang.CloneNotSupportedException();
096     }
097 
098     /**
099      * <p>Compares this enum with the specified object for order.</p>
100      * <p>It Returns a negative integer, zero, or a positive integer as this object is less than, 
101      * equal to, or greater than the specified object.</p>
102      * <p>Enum constants are only comparable to other enum constants of the same enum type.</p>
103      * <p>The natural order implemented by this method is the order in which the constants are declared.</p>
104      * @param other The object to be compared.
105      * @return A negative integer, zero, or a positive integer as this object is less than, equal 
106      * to, or greater than the specified object.
107      * @since 1.0
108      * @javascript Re-compilers must report error on end-users directly using this method.
109      * For efficiency, the invocation of this instance emulation method with single statement can be in-lined in re-compilation.
110      */
111     public final int compareTo(E other) {
112         return this.ordinal - other.ordinal;
113     }
114 
115     /**
116      * <p>Determines whether the specified object is equal to this enum constant.</p>
117      * @param other The object to be compared for equality with this object.
118      * @return <tt>true</tt> if the specified object is equal to this enum constant.
119      * @since 1.0
120      * @javascript Re-compilers must report error on end-users directly using this method.
121      * For efficiency, the invocation of this instance emulation method with single statement can be in-lined in re-compilation.
122      */
123     @Override
124     public final boolean equals(java.lang.Object other) {
125         return this == other;
126     }
127 
128     /**
129      * <p>Returns the Class object corresponding to this enum constant's enum type.</p>
130      * @return The Class object corresponding to this enum constant's enum type.
131      * @since 1.0
132      * @javascript Re-compilers must report error on end-users directly using this method.
133      */
134     @SuppressWarnings("unchecked")
135     public final java.lang.Class<E> getDeclaringClass() {
136         java.lang.Class<?> clazz = getClass();
137         java.lang.Class<?> superclass = clazz.getSuperclass();
138 
139         return (superclass == Enum.class) ? (java.lang.Class<E>)clazz : (java.lang.Class<E>)superclass;
140     }
141 
142     /**
143      * <p>Returns a hash code for this enum constant.</p>
144      * @return A hash code for this enum constant.
145      * @since 1.0
146      * @javascript Re-compilers must report error on end-users directly using this method.
147      * For efficiency, the invocation of this instance emulation method with single statement can be in-lined in re-compilation.
148      */
149     @Override
150     public final int hashCode() {
151         return java.lang.System.identityHashCode(this);
152     }
153 
154     /**
155      * <p>Returns the name of this enum constant, exactly as declared in its enum declaration.</p>
156      * @return The name of this enum constant.
157      * @since 1.0
158      * @javascript Re-compilers must report error on end-users directly using this method.
159      * For efficiency, the invocation of this instance emulation method with single statement can be in-lined in re-compilation.
160      */
161     public final java.lang.String name() {
162         return name;
163     }
164 
165     /**
166      * <p>Returns the ordinal of this enumeration constant (its position in its enum declaration, 
167      * where the initial constant is assigned an ordinal of zero).</p>
168      * @return The ordinal of this enumeration constant.
169      * @since 1.0
170      * @javascript Re-compilers must report error on end-users directly using this method.
171      * For efficiency, the invocation of this instance emulation method with single statement can be in-lined in re-compilation.
172      */
173     public final int ordinal() {
174         return ordinal;
175     }
176 
177     /**
178      * <p>Returns the name of this enum constant, as contained in the declaration.</p>
179      * @return The name of this enum constant.
180      * @since 1.0
181      * @javascript Re-compilers must report error on end-users directly using this method.
182      * For efficiency, the invocation of this instance emulation method with single statement can be in-lined in re-compilation.
183      */
184     @Override
185     public java.lang.String toString() {
186         return name;
187     }
188 }