dfki.util
Class EnumID
java.lang.Object
|
+--dfki.util.EnumID
- public abstract class EnumID
- extends java.lang.Object
Base class for creating typesave enumerated constants.
Enumeration ids have an unique id (integer value) and a
name. The name, for example, is useful for generation of meanigful
messages.
Advantages:
- Type checking at compile time.
- Fast equality checks with
==
and !=
.
- Support for meaningful message generation and reading
enumerated constants from strings.
Define your own class for each set of enumeration ids. This class
should have a private constructor. It has to contain all
possible ids as public and final class members.
public class Param extends EnumID
{
public final static int _A = 0;
public final static int _B = 1;
public final static Param A = new Param( _A, "A" );
public final static Param B = new Param( _B, "B" );
...
public final static EnumID[] ALL_PARAMS = new Param[] { A, B };
private Param( int id, String name ) { super( id, name ); }
}
Use the newly definied Param
class as follows:
//method calls
Result result = o.someMethod( Param.A );
//reading from a string:
String inputStr = readStringFromSomewhere();
Param p = Param.parseEnumID( inputStr, Param.ALL_PARAMS );
//enumeration checks
if (p == Param.B ) { do_something(); }
...
switch( p.value() )
{
switch _A: do_something();
switch _B: do_something();
}
//printing messages
System.out.println( "The parameter has the value: " + p );
Constructor Summary |
EnumID(int val,
java.lang.String name)
|
Methods inherited from class java.lang.Object |
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait |
EnumID
public EnumID(int val,
java.lang.String name)
- Parameters:
val
- code of the enumeration id.name
- - name of enumeration id.
value
public int value()
- Returns:
- enumeration id code.
getName
public java.lang.String getName()
- Returns:
- ID name of accesstype
toString
public java.lang.String toString()
- Overrides:
toString
in class java.lang.Object
- Returns:
- String repressentation of accesstype
parseEnumID
public static EnumID parseEnumID(java.lang.String name,
EnumID[] eList)
- Create an enumeration id from a string source.
- Parameters:
name
- The string with the expected id name.eList
- List of candidate enumeration ids.
findEnumID
public static EnumID findEnumID(int value,
EnumID[] eList)