Package org.apache.avalon.framework
Class Enum
- java.lang.Object
-
- org.apache.avalon.framework.Enum
-
- Direct Known 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, theEnum(String, Map)
constructor can be used to populate aMap
, 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
toprotected
. 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:
- $Id: Enum.java 45862 2004-09-10 22:16:39 -0500 (Fri, 10 Sep 2004) niclas $
-
-
Field Summary
Fields Modifier and Type Field Description private java.lang.String
m_name
The string representation of the Enum.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
equals(java.lang.Object o)
Tests for equality.java.lang.String
getName()
Retrieve the name of this Enum item, set in the constructor.int
hashCode()
java.lang.String
toString()
Human readable description of this Enum item.
-
-
-
Constructor Detail
-
Enum
protected Enum(java.lang.String name)
Constructor to add a new named item.Note: access changed from
public
toprotected
after 4.0. See class description.- Parameters:
name
- Name of the item.
-
Enum
protected Enum(java.lang.String name, java.util.Map map)
Constructor to add a new named item.Note: access changed from
public
toprotected
after 4.0. See class description.- Parameters:
name
- Name of the item.map
- AMap
, to which will be added a pointer to the newly constructed object.
-
-
Method Detail
-
equals
public boolean equals(java.lang.Object o)
Tests for equality. Two Enum:s are considered equal if they are of the same class and have the same names. The method is also declared final - I (LSutic) did this to allow the JIT to inline it easily.- Overrides:
equals
in classjava.lang.Object
- Parameters:
o
- the other object- Returns:
- the equality status
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
getName
public final java.lang.String getName()
Retrieve the name of this Enum item, set in the constructor.- Returns:
- the name
String
of this Enum item
-
toString
public java.lang.String toString()
Human readable description of this Enum item. For use when debugging.- Overrides:
toString
in classjava.lang.Object
- Returns:
- String in the form
type[name]
, eg.:Color[Red]
.
-
-