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

public abstract Class ValuedEnum

extends Enum
Class Inheritance
Imports
java.util.Map

Basic enum class for type-safe enums with values. Valued enum items can be compared and ordered with the provided methods. Should be used as an abstract base. For example:
import org.apache.avalon.framework.ValuedEnum;

public final class JavaVersion
  extends ValuedEnum
{
  //standard enums for version of JVM
  public static final JavaVersion  JAVA1_0  = new JavaVersion( "Java 1.0", 100 );
  public static final JavaVersion  JAVA1_1  = new JavaVersion( "Java 1.1", 110 );
  public static final JavaVersion  JAVA1_2  = new JavaVersion( "Java 1.2", 120 );
  public static final JavaVersion  JAVA1_3  = new JavaVersion( "Java 1.3", 130 );

  private JavaVersion( final String name, final int value )
  {
    super( name, value );
  }
}
The above class could then be used as follows:
import org.apache.avalon.framework.context.Context;
import org.apache.avalon.framework.context.Contextualizable;
import org.apache.avalon.framework.context.ContextException;

public class MyComponent implements Contextualizable
{
  JavaVersion requiredVer = JavaVersion.JAVA1_2;

  public void contextualize(Context context)
      throws ContextException
  {
    JavaVersion ver = (JavaVersion)context.get("java.version");
    if ( ver.isLessThan( requiredVer ) )
    {
      throw new RuntimeException( requiredVer.getName()+" or higher required" );
    }
  }
}
As with Enum, the ValuedEnum(String, int, Map) constructor can be used to populate a Map, from which further functionality can be derived.

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: ValuedEnum.java 506231 2007-02-12 02:36:54Z crossley $

Field Summary

Modifier and TypeField and Description
private final int
m_value

The value contained in enum.

Constructor Summary

AccessConstructor and Description
protected
ValuedEnum(final String
the name of enum item.
name
,
final int
the value of enum item.
value
)

Constructor for enum item.

protected
ValuedEnum(final String
the name of enum item.
name
,
final int
the value of enum item.
value
,
final Map<K, V>
the Map to add enum item to.
map
)

Constructor for enum item so that it gets added to Map at creation.

Method Summary

Modifier and TypeMethod and Description
public boolean

Returns:

the equality status
equals
(Object
the other object
o
)

Overrides org.apache.avalon.framework.Enum.equals.

Tests for equality.
public final int

Returns:

the enum item's value.
getValue
()

Get value of enum item.

public int
hashCode()

Overrides org.apache.avalon.framework.Enum.hashCode.

Returns a hash code value for the object.
public final boolean

Returns:

true if equal
isEqualTo
(final ValuedEnum
the other enum
other
)

Test if enum item is equal in value to other enum.

public final boolean

Returns:

true if greater than
isGreaterThan
(final ValuedEnum
the other enum
other
)

Test if enum item is greater than in value to other enum.

public final boolean

Returns:

true if greater than or equal
isGreaterThanOrEqual
(final ValuedEnum
the other enum
other
)

Test if enum item is greater than or equal in value to other enum.

public final boolean

Returns:

true if less than
isLessThan
(final ValuedEnum
the other enum
other
)

Test if enum item is less than in value to other enum.

public final boolean

Returns:

true if less than or equal
isLessThanOrEqual
(final ValuedEnum
the other enum
other
)

Test if enum item is less than or equal in value to other enum.

public String

Returns:

String in the form type[name=value], eg.: JavaVersion[Java 1.0=100].
toString
()

Overrides org.apache.avalon.framework.Enum.toString.

Override toString method to produce human readable description.
Inherited from org.apache.avalon.framework.Enum:
getName

Field Detail

m_valueback to summary
private final int m_value

The value contained in enum.

Constructor Detail

ValuedEnumback to summary
protected ValuedEnum(final String name, final int value)

Constructor for enum item.

Note

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

Parameters
name:String

the name of enum item.

value:int

the value of enum item.

ValuedEnumback to summary
protected ValuedEnum(final String name, final int value, final Map<K, V> map)

Constructor for enum item so that it gets added to Map at creation. Adding to a map is useful for implementing find...() style methods.

Note

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

Parameters
name:String

the name of enum item.

value:int

the value of enum item.

map:Map<K, V>

the Map to add enum item to.

Method Detail

equalsback to summary
public boolean equals(Object o)

Overrides org.apache.avalon.framework.Enum.equals.

Tests for equality. Two Enum:s are considered equal if they are of the same class, have the same name, and same value.

Parameters
o:Object

the other object

Returns:boolean

the equality status

getValueback to summary
public final int getValue()

Get value of enum item.

Returns:int

the enum item's value.

hashCodeback to summary
public int hashCode()

Overrides org.apache.avalon.framework.Enum.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.

isEqualToback to summary
public final boolean isEqualTo(final ValuedEnum other)

Test if enum item is equal in value to other enum.

Parameters
other:ValuedEnum

the other enum

Returns:boolean

true if equal

isGreaterThanback to summary
public final boolean isGreaterThan(final ValuedEnum other)

Test if enum item is greater than in value to other enum.

Parameters
other:ValuedEnum

the other enum

Returns:boolean

true if greater than

isGreaterThanOrEqualback to summary
public final boolean isGreaterThanOrEqual(final ValuedEnum other)

Test if enum item is greater than or equal in value to other enum.

Parameters
other:ValuedEnum

the other enum

Returns:boolean

true if greater than or equal

isLessThanback to summary
public final boolean isLessThan(final ValuedEnum other)

Test if enum item is less than in value to other enum.

Parameters
other:ValuedEnum

the other enum

Returns:boolean

true if less than

isLessThanOrEqualback to summary
public final boolean isLessThanOrEqual(final ValuedEnum other)

Test if enum item is less than or equal in value to other enum.

Parameters
other:ValuedEnum

the other enum

Returns:boolean

true if less than or equal

toStringback to summary
public String toString()

Overrides org.apache.avalon.framework.Enum.toString.

Override toString method to produce human readable description.

Returns:String

String in the form type[name=value], eg.: JavaVersion[Java 1.0=100].