satin.common
Class Enum

java.lang.Object
  |
  +--satin.common.Enum
Direct Known Subclasses:
AppCode, CalcWindow.Format, CWmode, EdgeType, FilmType, HemiMode, MCmode, MCstartupOptions.Model, MCstartupOptions.VDM, MiniMonDisplay, MobType, ModifierKey, PlayerTest.Mode, PlayerTest.Status, SelGenSpec, SourceFormat, SrcRec, TCstyle, TCtype, TestStatus, TimecodeRate, TrackLightType, TrackType, VideoFormat

public abstract class Enum
extends java.lang.Object

An abstract base class for "enum" classes. This is a base class for deriving classes that behave something like C or C++ "enum" types, except that they don't have integer values.

What Java Should Have Provided

This class is intended to provide support for a language feature that is missing from Java. This feature, if it were supported, would allow you to define an enum like this (Java keywords, real or imagined, are shown in boldface):
 public enum NewEnglandState
 {
     VERMONT, MASSACHUSETTS, CONNECTICUT, NEW_HAMPSHIRE, MAINE, RHODE_ISLAND
 }; 
This would define the symbol NewEnglandState to be a new user-defined type, whose set of values are the symbols enclosed in curly braces (VERMONT, ... RHODE_ISLAND). When used outside of their definition, values would have to be explicitly scoped to their type (for example NewEnglandState.VERMONT). The only operations allowable would be assignment and a test for equality, for example:
 NewEnglandState s;  // declare variable s to be of type NewEnglandState
 // pretend s gets initialized somewhere 
 if (s == NewEnglandState.NEW_HAMPSHIRE)  // comparison
     s = NewEnglandState.VERMONT;    // assignment
 

How the Enum Class Works

Since Java does not provide us with this feature, we must simulate it using classes and objects. Each enum "type" is represented by a class derived from Enum. For each value of the enum, we statically instantiate one object of that derived class, and assign it to a constant class variable declared within the derived class. Additional run-time instantiations are prevented by making the the constructor is protected. Thus, the complete set of objects of the derived enum class that can ever exist are statically defined within the class.

This is better than using integer contants because (a) it prevents, at compile time, using invalid integer values, and (b) it provides better self-documenting code.


Example of Usage

Let's say we need a user-defined type that represents the New England states. We want to be able to declare variables and parameters in such a way that, at compile time, we can be assured that they can only contain legal values.

We would define a class to represent a state as follows:

 public class NEstate extends Enum
 {
     protected NEstate (String s) { super(s); }
 
     public static final NEstate VERMONT = new NEstate("VERMONT");
     public static final NEstate MASSACHUSETTS = new NEstate("MASSACHUSETTS");
     public static final NEstate CONNECTICUT = new NEstate("CONNECTICUT");
     public static final NEstate NEW_HAMPSHIRE = new NEstate("NEW_HAMPSHIRE");
     public static final NEstate MAINE = new NEstate("MAINE");
     public static final NEstate RHODE_ISLAND = new NEstate("RHODE_ISLAND");
 }
We could then use variables of type NEstate like this:
 NEstate s = NEstate.VERMONT;
 // ...
 if (s == NEstate.MAINE)
     System.out.println("ayuh");
Note that the following code will not compile:
NEstate s = new NEstate("Quebec");
Because the constructor is protected, objects of class NEstate can only be instantiated within the class itself. This means that there can be no additional (bogus) states created at runtime.

Author:
Ed Stauff

Field Summary
final java.lang.String name
          The name of the enum value.
 
Constructor Summary
protected Enum(java.lang.String name)
          Constructs an enum value.
 
Method Summary
java.lang.String toString()
          Returns a string representation of the enum value.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

name

public final java.lang.String name
The name of the enum value. By convention, this should be the same as the name of the constant variable that holds it. Its only legitimate use is to allow the Java runtime system to print out an object of this class with a readable name.
Constructor Detail

Enum

protected Enum(java.lang.String name)
Constructs an enum value. Values should only be constructed statically from within the derived class.

Parameters:
name
by convention, the same as the name of the constant that holds this value. This value is placed in the name field.
Method Detail

toString

public java.lang.String toString()
Returns a string representation of the enum value. This is typically the class name followed by a period followed by the value name.

Overrides:
toString in class java.lang.Object

Returns:
a string representation of the enum value.
Revision History

5/31/01 Ed Stauff - made changes to docs per initial code review.

3/26/01 Ed Stauff - improved example of usage.

3/21/01 Ed Stauff - initial version reviewed by automation team.



Generated on 5/9/2001 19:37 from Enum.java (PRIVATE SOURCES)