Top Description Fields Constructors Methods
org.apache.avalon.framework

public abstract Class Enum

extends Object
Class Inheritance
Known Direct Subclasses
org.apache.avalon.framework.ValuedEnum
Imports
java.util.Map

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.

Author
Avalon Development Team
Version
$Id: Enum.java 506231 2007-02-12 02:36:54Z crossley $

Field Summary

Modifier and TypeField and Description
private final String
m_name

The string representation of the Enum.

Constructor Summary

AccessConstructor and Description
protected
Enum(final String
Name of the item.
name
)

Constructor to add a new named item.

protected
Enum(final String
Name of the item.
name
,
final Map<K, V>
A Map, to which will be added a pointer to the newly constructed object.
map
)

Constructor to add a new named item.

Method Summary

Modifier and TypeMethod and Description
public boolean

Returns:

the equality status
equals
(Object
the other object
o
)

Overrides java.lang.Object.equals.

Tests for equality.
public final String

Returns:

the name String of this Enum item
getName
()

Retrieve the name of this Enum item, set in the constructor.

public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for the object.
public String

Returns:

String in the form type[name], eg.: Color[Red].
toString
()

Overrides java.lang.Object.toString.

Human readable description of this Enum item.
Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

m_nameback to summary
private final String m_name

The string representation of the Enum.

Constructor Detail

Enumback to summary
protected Enum(final String name)

Constructor to add a new named item.

Note

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

Parameters
name:String

Name of the item.

Enumback to summary
protected Enum(final String name, final Map<K, V> map)

Constructor to add a new named item.

Note

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

Parameters
name:String

Name of the item.

map:Map<K, V>

A Map, to which will be added a pointer to the newly constructed object.

Method Detail

equalsback to summary
public boolean equals(Object o)

Overrides java.lang.Object.equals.

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.

Parameters
o:Object

the other object

Returns:boolean

the equality status

getNameback to summary
public final String getName()

Retrieve the name of this Enum item, set in the constructor.

Returns:String

the name String of this Enum item

hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for the object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Returns:int

a hash code value for this object.

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Human readable description of this Enum item. For use when debugging.

Returns:String

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