|
JADE v6.1 | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjava.lang.Number
com.dautelle.util.Enum
This class represents the base class for all enumerates; its semantic
is somewhat similar to the new JDK Tiger enum
base type,
with some additional capabilities. In particular:
public class Event { public static abstract class BaseId extends Enum { public static final Collection VALUES = getInstances(BaseId.class); } } public class MouseEvent extends Event { public static class Id extends BaseId { public static final Collection VALUES = getInstances(Id.class); public static final Id MOUSE_CLICKED = new Id(); public static final Id MOUSE_PRESSED = new Id(); public static final Id MOUSE_RELEASED = new Id(); } } public class KeyEvent extends Event { public static class Id extends BaseId { public static final Collection VALUES = getInstances(Id.class); public static final Id KEY_PRESSED = new Id(); public static final Id KEY_RELEASED = new Id(); } }
Enum
's instances can be mapped to custom values (up to
long
) for use in switch
statements,
bit-wise sets (values power of 2) or look-up tables.
Duplications (name or value) would result in exception thrown
during initialization:
public static final class Color extends Enum { public static final Collection VALUES = getInstances(Color.class); private Color(int value, String name) { // Not extendable (private) super(value, name); } public static final int RED_VALUE = 0xFF0000; public static final Color RED = new Color(RED_VALUE, "red"); public static final int GREEN_VALUE = 0x00FF00; public static final Color GREEN = new Color(GREEN_VALUE, "green"); public static final int BLUE_VALUE = 0x0000FF; public static final Color BLUE = new Color(BLUE_VALUE, "blue"); } ... Color color; switch (color.intValue()) { case Color.RED_VALUE: case Color.GREEN_VALUE: case Color.BLUE_VALUE: }
Enum
are
not implicitly final
. For example:public static class WeekDay extends Enum { public static final Collection VALUES = getInstances(WeekDay.class); public static final WeekDay SATURDAY = new WeekDay(); public static final WeekDay SUNDAY = new WeekDay(); } public static class WorkDay extends WeekDay { public static final Collection VALUES = getInstances(WorkDay.class); public static final WorkDay MONDAY = new WorkDay(); public static final WorkDay TUESDAY = new WorkDay(); public static final WorkDay WEDNESDAY = new WorkDay(); public static final WorkDay THURSDAY = new WorkDay(); public static final WorkDay FRIDAY = new WorkDay(); } ... for (Iterator i = WorkDay.VALUES.iterator(); i.hasNext(); ) { // Iterates through the five workdays. } for (Iterator i = WeekDay.VALUES.iterator(); i.hasNext(); ) { // Iterates through the seven weekdays. }
Limitations: Unlike the JDK1.5 enum
base type,
instances of this class cannot be used directly in a switch
statement. Although, for mapped's values (see Color
example
above) a switch
on the enum's value is always possible.
This approach is also faster as it does not require
a "multiway if-statement" or "array indirection"
(Ref.
JDK 1.5 Enum: IV. Implementation Notes).
This class is public domain (not copyrighted).
Constructor Summary | |
protected |
Enum()
Default constructor. |
protected |
Enum(long value)
Creates an enumerate of specified value. |
protected |
Enum(long value,
java.lang.String name)
Creates an enumerate of specified value and specified name. |
protected |
Enum(java.lang.String name)
Creates an enumerate of specified name. |
Method Summary | |
protected java.lang.Object |
clone()
Throws CloneNotSupportedException. |
int |
compareTo(java.lang.Object o)
Compares this enum with the specified object for order. |
double |
doubleValue()
Returns this enum's value as a double . |
float |
floatValue()
Returns this enum's value as a float . |
protected static java.util.Collection |
getInstances(java.lang.Class enumClass)
Returns a read-only list of the specified enumeration. |
java.lang.String |
getName()
Returns the unique name for this enum. |
int |
intValue()
Returns this enum's value as a int . |
long |
longValue()
Returns this enum's value as a long . |
protected java.lang.Object |
readResolve()
Overrides readResolve() to support
Serialization . |
java.lang.String |
toString()
Returns a string representation of this enum. |
static Enum |
valueOf(java.lang.CharSequence name,
java.lang.Class enumClass)
Returns the enum with the specified name. |
static Enum |
valueOf(long value,
java.lang.Class enumClass)
Returns the enum with the specified value. |
Methods inherited from class java.lang.Number |
byteValue, shortValue |
Methods inherited from class java.lang.Object |
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Constructor Detail |
protected Enum()
0
). This constructor is typically used for
anonymous extendable enumerations (with value automatically assigned).
java.lang.IllegalStateException
- if an enum with same value already exists.protected Enum(java.lang.String name)
0
).
This constructor is typically used for extendable named enumerations
(with value automatically assigned).
name
- the name for this enum.
java.lang.IllegalStateException
- if an enum with same name or same value
already exists.protected Enum(long value)
value
- the value for this enum.
java.lang.IllegalStateException
- if an enum with same value already exists.protected Enum(long value, java.lang.String name)
switch
statements (e.g. state machine) with values
being defined by public final static int/long
constants.
value
- the value for this enum.name
- the name for this enum.
java.lang.IllegalStateException
- if an enum with same name or same value
already exists.Method Detail |
public int compareTo(java.lang.Object o)
compareTo
in interface java.lang.Comparable
o
- the object to be compared.
java.lang.ClassCastException
- if the specified object's type prevents it
from being compared to this object.protected static java.util.Collection getInstances(java.lang.Class enumClass)
Enum
regardless of their actual values. This method is
typically used to export the constant VALUES
as inpublic static Color extends Enum { public static final Collection VALUES = getInstances(Color.class); ... }
enumClass
- the enum class.
public static Enum valueOf(long value, java.lang.Class enumClass)
value
- the value of the enum to search for.enumClass
- the class of the enum to return.
null
if not found.public static Enum valueOf(java.lang.CharSequence name, java.lang.Class enumClass)
name
- the name of the enum to search for.enumClass
- the class of the enum to return.
name.equals(enum.getName())
or null
if not found.public final int intValue()
int
.
This may involve rounding or truncation.
int
.public final long longValue()
long
.
long
.public final float floatValue()
float
.
This may involve rounding.
float
.public final double doubleValue()
double
.
This may involve rounding.
double
.public final java.lang.String getName()
Color#0
for the first declared color in the Color
class).
public java.lang.String toString()
getName()
protected java.lang.Object readResolve() throws java.io.ObjectStreamException
readResolve()
to support
Serialization
. Enum are uniquely identified by their
name (independantly of their values to avoid class loading dependencies).
java.io.ObjectStreamException
- if the enum is not found.getName()
protected final java.lang.Object clone() throws java.lang.CloneNotSupportedException
java.lang.CloneNotSupportedException
|
JADE v6.1 | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |