com.lc.util
Class SafeEnumeration

java.lang.Object
  |
  +--com.lc.util.SafeEnumeration
All Implemented Interfaces:
java.lang.Comparable
Direct Known Subclasses:
AlgorithmState, TabuListTrigger

public abstract class SafeEnumeration
extends java.lang.Object
implements java.lang.Comparable

Base class for strongly-typed enumerations, which know the name that was used to declare them in the Java source code.

The concrete class defines at once

Methods defined in SafeEnumeration allow to access to the whole set of defined elements (like a Pascal enumeration).

A concrete class derived from SafeEnumeration should be implemented as follows :

public final class <MyClass> extends SafeEnumeration {
  private <MyClass>() { }
  public static final <MyClass> VALUE_A = new <MyClass>() ;
  public static final <MyClass> VALUE_B = new <MyClass>() ;
  public static final <MyClass> VALUE_C = new <MyClass>() ;
}

 

Or, as well :

public final class <MyClass> extends SafeEnumeration {
  private <MyClass>( String name ) { super( name ) ; }
  public static final <MyClass> VALUE_A = new <MyClass>( "value a" ) ;
  // ...
}
 

This kind of enumeration may be iterated as follows :

<MyClass>[] elements = ( <MyClass>[] )
    SafeEnumeration.getElements( <MyClass>.class ) ;
for( int i = 0 ; i < elements.length ; i++ ) {
  <MyClass> = elements[ i ] ) ;
  // ...
}
 
For convenience, the concrete class could define wrappers for type-safety, depending of the needs.

Known issue : one should avoid to define a SafeEnumeration as a nested class, since it has been observed that SafeEnumerationPropertyEditor does not handle them (at least with JBuilder 4.0).

Version:
$Revision: 1.1.1.1 $ $Date: 2002/02/19 22:12:03 $
Author:
Laurent Caillette

Constructor Summary
SafeEnumeration()
          Constructor.
SafeEnumeration(java.lang.String userDefinedName)
          Constructor.
 
Method Summary
 int compareTo(java.lang.Object obj)
          Compares the declaring names.
static SafeEnumeration getByDeclaringName(java.lang.Class aClass, java.lang.String name)
          Returns the item of a concrete class, of the given name.
 java.lang.String getDeclaringName()
          Returns the name of the item, as defined in the Java source code.
static SafeEnumeration[] getElements(java.lang.Class aClass)
          Returns an array of the concrete type of the enumeration.
static SafeEnumeration getFirstElement(java.lang.Class aClass)
          Returns the 1st item declared in the source code.
 java.lang.String getName()
          Returns the name of the item.
 java.lang.String toString()
          Idem getDeclaringName().
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

SafeEnumeration

public SafeEnumeration()
Constructor. The name will be the same as the declaringName.

SafeEnumeration

public SafeEnumeration(java.lang.String userDefinedName)
Constructor. The name property may be of a different value than the one defined in the source code.
Method Detail

getName

public final java.lang.String getName()
Returns the name of the item.
Returns:
The name of the item.

getDeclaringName

public final java.lang.String getDeclaringName()
Returns the name of the item, as defined in the Java source code.
Returns:
The name of the item, as defined in the Java source code.

toString

public final java.lang.String toString()
Idem getDeclaringName().
Overrides:
toString in class java.lang.Object

getElements

public static final SafeEnumeration[] getElements(java.lang.Class aClass)
Returns an array of the concrete type of the enumeration.
Returns:
An array of the concrete type of the enumeration.

getFirstElement

public static final SafeEnumeration getFirstElement(java.lang.Class aClass)
Returns the 1st item declared in the source code.
Returns:
The 1st item declared in the source code.

getByDeclaringName

public static final SafeEnumeration getByDeclaringName(java.lang.Class aClass,
                                                       java.lang.String name)
Returns the item of a concrete class, of the given name.
Parameters:
aClass - The concrete class of the enumeration.
name - The name used in the Java source code.
Returns:
The item, or null if not found.

compareTo

public final int compareTo(java.lang.Object obj)
Compares the declaring names.
Specified by:
compareTo in interface java.lang.Comparable