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.
| Modifier and Type | Field and Description |
|---|---|
| private final String | m_name
The string representation of the Enum. |
| Access | Constructor and Description |
|---|---|
| protected | |
| protected |
| Modifier and Type | Method and Description |
|---|---|
| public boolean | Returns: the equality statusthe other object o)Overrides java. |
| public final String | Returns: the nameString of this Enum itemRetrieve the name of this Enum item, set in the constructor. |
| public int | |
| public String | Returns: String in the formtype[name], eg.:
Color[Red].Overrides java. |
| m_name | back to summary |
|---|---|
| private final String m_name The string representation of the Enum. | |
| Enum | back to summary |
|---|---|
| protected Enum(final String name) Constructor to add a new named item. Note access changed from
| |
| Enum | back to summary |
|---|---|
| protected Enum(final String name, final Map<K, V> map) Constructor to add a new named item. Note access changed from | |
| equals | back to summary |
|---|---|
| public boolean equals(Object o) Overrides java. 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.
| |
| getName | back to summary |
|---|---|
| public final String getName() Retrieve the name of this Enum item, set in the constructor.
| |
| hashCode | back to summary |
|---|---|
| public int hashCode() Overrides java. Doc from java. Returns a hash code value for the object. This method is
supported for the benefit of hash tables such as those provided by
The general contract of
| |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Human readable description of this Enum item. For use when debugging.
| |