|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--lib206.Enum
Class Enum is an abstract parent class for an enumerated data type. Enumeration literal values are represented as Strings. The public data member value holds the string value of an enumeration literal.
Extending classes represent an enumerated type as a protected array of strings, each string being one of the legal enumeration literals of the type. The extending constructor calls the Enum constructor with a string enumeration value provided by its caller, and the list of legal enumeration values. The Enum constructor validates the caller-provided value, and if legal assigns it to this.value. If the caller-provided enumeration value is illegal, the Enum constructor throws an IllegalEnumException.
Here is an example of an extending class that defines an enumerated type for the seven days of the week:
class DayOfTheWeek extends Enum { public DayOfTheWeek(String value) { super(value, possibles); } static protected String[] possibles = { "Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday" }; }Here are a legal and illegal construction of enumerated DayOfTheWeek values:
DayOfTheWeek goodDay = new DayOfTheWeek("Monday"); DayOfTheWeek badDay = new DayOfTheWeek("Greenday");Here is how the public value field is used to access a DayOfTheWeek value:
if (goodDay.value == "Monday") ...As in Pascal-class languages of yesteryear, an enumation literal can be treated as an integer value. There are next and previous methods that return enumeration string values based on the relative ordering in the enumeration. There is also a getPosition method that returns the relative integer value of an enumeration literal directly. There is a constructor overload that builds an enumation literal given its ordinal position in a list of possibles. If an extending class wants to make this form of construction available to users, it implements its own int-valued constructor overload. E.g., here is such a constructor for the DayOfTheWeek example:
public DayOfTheWeek(int position) { super(position, possibles); }
Field Summary | |
protected int |
position
The ordinal position of this enum literal in the list of possibles |
protected java.lang.String[] |
possibles
Reference to the list of possibles in the extending class. |
java.lang.String |
value
Enumerated literal value for this enumerated type |
Constructor Summary | |
Enum(int position,
java.lang.String[] possibles)
Construct an enumerated literal object from the given position in the given possibles. |
|
Enum(java.lang.String value,
java.lang.String[] possibles)
Construct an enumerated literal object of the given string value, if that string value is one of the legal values in the given possibles array. |
Method Summary | |
int |
getPosition()
Return the numeric position of this in the list of possibles. |
java.lang.String |
next()
Return the next enum value in the list of possibles if there is one, null otherwise. |
java.lang.String |
previous()
Return the previous enum value in the list of possibles if there is one, null otherwise. |
java.lang.String |
toString()
Convert this to a string by returning the value. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
public java.lang.String value
protected int position
protected java.lang.String[] possibles
Constructor Detail |
public Enum(java.lang.String value, java.lang.String[] possibles) throws IllegalEnumException
public Enum(int position, java.lang.String[] possibles) throws IllegalEnumException
Method Detail |
public java.lang.String next()
DayOfTheWeek day = new DayOfTheWeek("Wednesday")then day.next = "Thursday"
public java.lang.String previous()
DayOfTheWeek day = new DayOfTheWeek("Wednesday")then day.previous = "Tuesday"
public java.lang.String toString()
toString
in class java.lang.Object
public int getPosition()
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |