public abstract class TypeSafeEnum extends Number implements Serializable, Comparable
This abstract class represents the base class for all enumerates.
Enumerates defined using this class have the following advantages
over the usual "static int"
declaration:
Comparable
and can be used as
collection's keys (e.g. TreeMap).switch
statements, bit-wise sets ("int" values
power of 2) or look-up tables.Serializable
.Here is an example how this class can be used to define IDs for mouse events. There is no need to reserve predefined ranges of ID values for each event sub-class (Ref. current AWT implementation).
public class Event extends java.util.EventObject { public static class ID extends TypeSafeEnum {} } public class MouseEvent extends Event { 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 final ID KEY_PRESSED = new ID(); public static final ID KEY_RELEASED = new ID(); ... }
Here is another example where enums with predefined values are used in a
switch
statement. Name or value duplication would result
in exception being thrown at initialization time.
public class Graphic { public static class Color extends TypeSafeEnum { public Color(int value, String name) { super(value, name); } } static final int RED_VALUE = 0xFF0000; static final Color RED = new Color(RED_VALUE, "red"); static final int GREEN_VALUE = 0x00FF00; static final Color GREEN = new Color(GREEN_VALUE, "green"); static final int BLUE_VALUE = 0x0000FF; static final Color BLUE = new Color(BLUE_VALUE, "blue"); } ... Color color; switch (color.intValue()) { case RED_VALUE: case GREEN_VALUE: case BLUE_VALUE: }
Note: This class is an implementation of the typesafe enumeration Pattern
Copyright (c) SAP AGModifier | Constructor and Description |
---|---|
protected |
TypeSafeEnum()
Default constructor.
|
protected |
TypeSafeEnum(long value)
Creates an TypeSafeEnumerate of specified value.
|
protected |
TypeSafeEnum(long value,
String name)
Creates an TypeSafeEnumerate of specified value and specified name.
|
protected |
TypeSafeEnum(String name)
Creates an TypeSafeEnumerate of specified name.
|
Modifier and Type | Method and Description |
---|---|
int |
compareTo(Object o)
Compares this TypeSafeEnum with the specified object for order.
|
double |
doubleValue()
Returns this TypeSafeEnum's value as a
double . |
float |
floatValue()
Returns this TypeSafeEnum's value as a
float . |
static List |
getEnums(Class parentClass)
Return already registered enumerations of the specified Class type.
|
static List |
getEnums(Class parentClass,
boolean includeChildren)
Return already registered enumerations of the specified Class type.
|
protected static TypeSafeEnum |
getInstance(Class TypeSafeEnumClass,
long value)
Returns the TypeSafeEnum with the specified value.
|
protected static TypeSafeEnum |
getInstance(Class TypeSafeEnumClass,
String name)
Returns the TypeSafeEnum with the specified name.
|
protected static Collection |
getInstances(Class TypeSafeEnumClass)
Returns a read-only list of the specified TypeSafeEnumeration.
|
String |
getName()
Returns the name of this TypeSafeEnum.
|
int |
intValue()
Returns this TypeSafeEnum's value as a
int . |
long |
longValue()
Returns this TypeSafeEnum's value as a
long . |
String |
toString()
Returns a string representation of this TypeSafeEnum.
|
byteValue, shortValue
protected TypeSafeEnum()
0
). This constructor is used for
anonymous extendable TypeSafeEnumerations (with value automatically assigned).IllegalStateException
- if an TypeSafeEnum with same value already exists.protected TypeSafeEnum(String name)
0
).
This constructor is used for extendable named TypeSafeEnumerations
(with value automatically assigned).name
- the name for this TypeSafeEnum.IllegalStateException
- if an TypeSafeEnum with same name or same value
already exists.protected TypeSafeEnum(long value)
value
- the value for this TypeSafeEnum.IllegalStateException
- if an TypeSafeEnum with same value already exists.protected TypeSafeEnum(long value, String name)
switch
statements).value
- the value for this TypeSafeEnum.name
- the name for this TypeSafeEnum.IllegalStateException
- if an TypeSafeEnum with same name or same value
already exists.public int compareTo(Object o)
compareTo
in interface Comparable
o
- the object to be compared.ClassCastException
- if the specified object's type prevents it
from being compared to this object.protected static Collection getInstances(Class TypeSafeEnumClass)
public static class Color extends TypeSafeEnum { ... public static Collection getInstances() { return getInstances(Color.class); } }
TypeSafeEnumClass
- the TypeSafeEnum class.protected static TypeSafeEnum getInstance(Class TypeSafeEnumClass, String name)
public static class Color extends TypeSafeEnum { ... public static Color getInstance(String name) { return (Color) getInstance(Color.class, name); } }
name
- the name of the TypeSafeEnum to search for.TypeSafeEnumClass
- the class to search for.null
if not found (e.g. class not initialized).protected static TypeSafeEnum getInstance(Class TypeSafeEnumClass, long value)
public static class Color extends TypeSafeEnum { ... public static Color getInstance(int value) { return (Color) getInstance(Color.class, value); } }
value
- the value of the TypeSafeEnum to search for.TypeSafeEnumClass
- the class to search for.null
if not found (e.g. class not initialized).public final int intValue()
int
.
This may involve rounding or truncation.public final long longValue()
long
.public final float floatValue()
float
.
This may involve rounding.floatValue
in class Number
float
.public final double doubleValue()
double
.
This may involve rounding.doubleValue
in class Number
double
.public final String getName()
null
if this TypeSafeEnum is
anonymous.public String toString()
public static List getEnums(Class parentClass)
Note that in order to be considered, a TypeSafeEnum Class needs to be loaded by the related ClassLoader.
parentClass
- a TypeSafeEnum class.public static List getEnums(Class parentClass, boolean includeChildren)
Note that in order to be considered, a TypeSafeEnum Class needs to be loaded by the related ClassLoader.
parentClass
- a TypeSafeEnum class.includeChildren
- if true the sub classes of the specified
TypeSafeEnum are also returnedAccess Rights |
---|
SC | DC | Public Part | ACH |
---|---|---|---|
[sap.com] KMC-CM
|
[sap.com] tc/km/frwk
|
api
|
EP-KM-CM
|
[sap.com] KMC-COLL
|
[sap.com] tc/kmc/coll/util
|
api
|
EP-KM-COL
|
[sap.com] KMC-WPC
|
[sap.com] tc/kmc/wpc/wpcfacade
|
api
|
EP-PIN-WPC-WCM
|
Copyright 2018 SAP AG Complete Copyright Notice