|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object net.lucidviews.util.EnumValue<T>
T
- the EnumValue sub classpublic abstract class EnumValue<T extends EnumValue>
Base-class for defining an enumeration type.
To define an enumeration extend this class, implementing the
getEnumeration
method, and create instances for each value in
your enumeration.
In Java5, use of generics allows the exact type (sub-class) of EnumValue to be
associated with the Enumeration.
For example:
It is essential that all constructors of the class are
public class DistanceUnit extends EnumValue<DistanceUnit>
{
// create a list for the enumeration values
public static final Enumeration<DistanceUnit> ENUMERATION = new Enumeration<DistanceUnit>();
// provide the EnumValue base class with access to the enumeration
public Enumeration<DistanceUnit> getEnumeration()
{
return ENUMERATION;
}
// define the enumeration values
public static final DistanceUnit METRE = new DistanceUnit();
public static final DistanceUnit YARD = new DistanceUnit();
public static final DistanceUnit KILOMETRE = new DistanceUnit();
public static final DistanceUnit MILE = new DistanceUnit();
// make the constructor private to protect this enum
private DistanceUnit()
{
}
}
protected
or private
so that no other instances can be created, which
would extend your enumeration with values that you did not define. This
includes the default constructor, which must be declared
protected
or private
if you define no other
constructor.
However, it is recommended that you assign a user-friendly text string to
each of your enumeration values. To do this you will need to define a
constructor in your enum class like this:
...
// define the enumeration values
public static final DistanceUnit METRE = new DistanceUnit( "metre" );
public static final DistanceUnit YARD = new DistanceUnit( "yard" );
public static final DistanceUnit KILOMETRE = new DistanceUnit( "kilometre" );
public static final DistanceUnit MILE = new DistanceUnit( "mile" );
// make the constructor private to protect this enum
private DistanceUnit( String unitName )
{
super( unitName );
}
...
The enumeration type is now ready to use. Using the enumeration type makes
the source code more readable and ensures the compiler performs some type
checking on parameter values.
For example, the method:
public void setDistanceUnit( int distanceUnit )
would become
public void setDistanceUnit( DistanceUnit newValue )
The primary advantage is that the newValue
must be one of the
constants defined by the DistanceUnit
class - in the first
interface, the distanceUnit
could be any int
value
and the method would need to perform some range checks first and throw an
exception. With enumerations the checks are done at compile time and no
exception needs to be caught.
Enumeration values can be taken from the exported constants in the
EnumValue
sub class or derived from their index or user-friendly
name.
For example, if a string value "mile
" is read from a
configuration file, the enumeration value can be looked-up:
String distanceUnitName = ...;
DistanceUnit distanceUnit = DistanceUnit.ENUMERATION.lookupValue( distanceUnitName );
IMPORTANT
Use the ==
operator, not the
equals
method to evaluate or compare EnumValue
s.
The equals
method performs an internal representation
comparision and is provided so that string and integer values will match
the text-value and index of an enumeration value.
Each enumeration value is a new, unique constant and should be compared
directly. For example:
String distanceUnitName = ...;
DistanceUnit distanceUnit = DistanceUnit.ENUMERATION.lookupValue( distanceUnitName );
if (distanceUnit == DistanceUnit.MILE)
{
...
}
Sub Enumerations
In Java5 the EnumValue
class supports sub enumerations
- sub lists within an enumeration.
To support sub lists you should define your enum class slightly differently to the
definition described above.
Like this:
Sub lists can then be defined like so:
public class Department<T extends Department> extends EnumValue<T>
{
// create a list for the enumeration values
public static final Enumeration<Department> ENUMERATION = new Enumeration<Department>();
// provide the EnumValue base class with access to the enumeration
public Enumeration<T> getEnumeration()
{
// The following cast works only when T = Department.
// Sub-classes MUST override this method
// and return an Enumeration of values of that sub type.
return (Enumeration<T>)ENUMERATION;
}
// define the enumeration values
public static final Department<Department> SALES = new Department<Department>( "sales" );
public static final Department<Department> ACCOUNTS = new Department<Department>( "accounts" );
public static final Department<Department> OFFICE_SERVICES = new Department<Department>( "office services" );
// protect the constructor to prevent new departments being created
protected Department( String name )
{
super( name );
}
}
As the definition of the class suggests (
public class TechnicalDepartment<T extends TechnicalDepartment> extends Department<T>
{
// create a list for the enumeration values
public static final Enumeration<TechnicalDepartment> ENUMERATION = new Enumeration<TechnicalDepartment>( Department.ENUMERATION );
// provide the EnumValue base class with access to the enumeration
public Enumeration<T> getEnumeration()
{
// The following cast works only when T = TechnicalDepartment.
// Sub-classes MUST override this method
// and return an Enumeration of values of that sub type.
return (Enumeration<T>)ENUMERATION;
}
// define the enumeration values
public static final TechnicalDepartment<TechnicalDepartment> DEVELOPMENT = new TechnicalDepartment<TechnicalDepartment>( "development" );
public static final TechnicalDepartment<TechnicalDepartment> SUPPORT = new TechnicalDepartment<TechnicalDepartment>( "support" );
public static final TechnicalDepartment<TechnicalDepartment> TESTING = new TechnicalDepartment<TechnicalDepartment>( "testing" );
// protect the constructor to prevent new technical departments being created
protected TechnicalDepartment( String name )
{
super( name );
}
}
TechnicalDepartment extends Department
) a technical department
is a sub type of a department.
The TechnicalDepartment
s DEVELOPMENT, SUPPORT and TESTING not only make up the sub-list of
technical departments but are also part of the Department
enumeration.
i.e.
TechnicalDepartment.ENUMERATION.valueOf( "development" )
returns TechnicalDepartment.DEVELOPMENT
Department.ENUMERATION.valueOf( "development" )
returns TechnicalDepartment.DEVELOPMENT
Department.ENUMERATION.valueOf( "sales" )
returns Department.SALES
TechnicalDepartment.ENUMERATION.valueOf( "sales" )
throws a NoSuchElementException
Enumeration
,
Serialized FormField Summary | |
---|---|
protected int |
_index
The index of this value within the enumeration. |
protected boolean |
_isCaseSensitive
A flag indicating if the text value is case sensitive. |
protected String |
_textValue
The user-friendly name associated with this enumeration value. |
protected static String |
ENUMERATION_FIELD_NAME
The name of the field that sub classes use to hold the Enumeration. |
protected static int |
UNDEFINED_INDEX
A value assigned to the index variable before the index of the enum value is known. |
Constructor Summary | |
---|---|
protected |
EnumValue()
Create an enumeration value with no user-friendly name. |
protected |
EnumValue(String textValue)
Create an enumeration value that can be identified by the given text. |
protected |
EnumValue(String textValue,
boolean isCaseSensitive)
Create an enumeration value that can be identified by the given text. |
Method Summary | |
---|---|
int |
compareTo(T other)
Compares this value with another enum value for order. |
boolean |
equals(Object obj)
|
abstract Enumeration<T> |
getEnumeration()
Obtain the enumeration this value belongs to. |
int |
getIndex()
Get the index of this enum value within the enumeration list. |
String |
getTextValue()
Get the text representation of this enum value. |
int |
hashCode()
|
String |
name()
A synonym for getTextValue , defined to make switching from
enum s to EnumValue s easier. |
int |
ordinal()
A synonym for getIndex , defined to make switching from
enum s to EnumValue s easier. |
String |
toString()
|
static EnumValue<?> |
valueOf(Class<? extends EnumValue> enumType,
String name)
Returns the EnumValue of the specified enumeration type
with the specified name. |
Methods inherited from class java.lang.Object |
---|
clone, finalize, getClass, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
protected static final int UNDEFINED_INDEX
protected static final String ENUMERATION_FIELD_NAME
protected String _textValue
protected boolean _isCaseSensitive
protected int _index
Constructor Detail |
---|
protected EnumValue(String textValue, boolean isCaseSensitive)
textValue
- the String
that is associated with this
enum valueisCaseSensitive
- indicates if a String
should be
associated with this enum value only if the case
matches that in the text valueprotected EnumValue(String textValue)
textValue
- the String
that is associated with this
enum valueprotected EnumValue()
Method Detail |
---|
public abstract Enumeration<T> getEnumeration()
public int getIndex()
public int ordinal()
getIndex
, defined to make switching from
enum
s to EnumValue
s easier.
getIndex()
public String getTextValue()
public String name()
getTextValue
, defined to make switching from
enum
s to EnumValue
s easier.
getTextValue()
public int compareTo(T other)
other
- the other enum value
public static EnumValue<?> valueOf(Class<? extends EnumValue> enumType, String name) throws IllegalArgumentException, NullPointerException
EnumValue
of the specified enumeration type
with the specified name. The name must match with the text value of
the enum value. (Extraneous whitespace characters are not permitted.)
enumType
- the Class
object of the
EnumValue
from which to return a constantname
- the text value of the enum value to return
EnumValue
from the specified enumeration type
with the specified name
IllegalArgumentException
- if the specified enumeration class has
no constant with the specified name, or
the specified class does not represent
an enum type
NullPointerException
- if enumType
or
name
is null
public String toString()
toString
in class Object
public int hashCode()
hashCode
in class Object
public boolean equals(Object obj)
equals
in class Object
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |