lib206
Class Enum

java.lang.Object
  |
  +--lib206.Enum
All Implemented Interfaces:
java.io.Serializable

public abstract class Enum
extends java.lang.Object
implements java.io.Serializable

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);
     }                                                                 

See Also:
Serialized Form

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

value

public java.lang.String value
Enumerated literal value for this enumerated type


position

protected int position
The ordinal position of this enum literal in the list of possibles


possibles

protected java.lang.String[] possibles
Reference to the list of possibles in the extending class. This reference is used in next and previous methods.

Constructor Detail

Enum

public Enum(java.lang.String value,
            java.lang.String[] possibles)
     throws IllegalEnumException
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. If the given string value is not among the possibles, throw an IllegalEnumException.


Enum

public Enum(int position,
            java.lang.String[] possibles)
     throws IllegalEnumException
Construct an enumerated literal object from the given position in the given possibles. If position < 0 or position > possibles.length, throw an IllegalEnumException.

Method Detail

next

public java.lang.String next()
Return the next enum value in the list of possibles if there is one, null otherwise. E.g., if
     DayOfTheWeek day = new DayOfTheWeek("Wednesday")
                                                                   
then day.next = "Thursday"


previous

public java.lang.String previous()
Return the previous enum value in the list of possibles if there is one, null otherwise. E.g., if
     DayOfTheWeek day = new DayOfTheWeek("Wednesday")
                                                                   
then day.previous = "Tuesday"


toString

public java.lang.String toString()
Convert this to a string by returning the value.

Overrides:
toString in class java.lang.Object

getPosition

public int getPosition()
Return the numeric position of this in the list of possibles.