org.apache.avalon.framework

Class Enum

Known Direct Subclasses:
ValuedEnum

public abstract class Enum
extends java.lang.Object

Basic enum class for type-safe enums. Should be used as an abstract base. For example:
 import org.apache.avalon.framework.Enum;

 public final class Color extends Enum {
   public static final Color RED = new Color( "Red" );
   public static final Color GREEN = new Color( "Green" );
   public static final Color BLUE = new Color( "Blue" );

   private Color( final String color )
   {
     super( color );
   }
 }
 
If further operations, such as iterating over all items, are required, the Enum(String,Map) constructor can be used to populate a Map, from which further functionality can be derived:
 public final class Color extends Enum {
   static final Map map = new HashMap();

   public static final Color RED = new Color( "Red", map );
   public static final Color GREEN = new Color( "Green", map );
   public static final Color BLUE = new Color( "Blue", map );

   private Color( final String color, final Map map )
   {
     super( color, map );
   }

   public static Iterator iterator()
   {
     return map.values().iterator();
   }
 }
 

NOTE: between 4.0 and 4.1, the constructors' access has been changed from public to protected. This is to prevent users of the Enum breaking type-safety by defining new Enum items. All Enum items should be defined in the Enum class, as shown above.

Version:
CVS $Revision: 1.23 $ $Date: 2003/02/11 15:58:37 $

Author:
Avalon Development Team

Constructor Summary

Enum(String name)
Constructor to add a new named item.
Enum(String name, Map map)
Constructor to add a new named item.

Method Summary

boolean
equals(Object other)
Tests for equality.
String
getName()
Retrieve the name of this Enum item, set in the constructor.
int
hashCode()
Returns a hash code value for the object.
String
toString()
Human readable description of this Enum item.

Constructor Details

Enum

protected Enum(String name)
Constructor to add a new named item.

Note: access changed from public to protected after 4.0. See class description.

Parameters:
name - Name of the item.


Enum

protected Enum(String name,
               Map map)
Constructor to add a new named item.

Note: access changed from public to protected after 4.0. See class description.

Parameters:
name - Name of the item.
map - A Map, to which will be added a pointer to the newly constructed object.

Method Details

equals

public final boolean equals(Object other)
Tests for equality. Two Enum:s are considered equal if they have the same class names and the same names. Identity is tested for first, so this method runs fast. The method is also declared final - I (LSutic) did this to allow the JIT to inline it easily.

Parameters:
other - the other object

Returns:
the equality status


getName

public final String getName()
Retrieve the name of this Enum item, set in the constructor.

Returns:
the name String of this Enum item


hashCode

public int hashCode()
Returns a hash code value for the object.

Returns:
a hash code value for this object


toString

public String toString()
Human readable description of this Enum item. For use when debugging.

Returns:
String in the form type[name], eg.: Color[Red].


"Copyright B) 2001 Apache Jakarta Project. All Rights Reserved."