org.sape.carbon.core.util.enum
Class BaseEnum

java.lang.Object
  |
  +--org.sape.carbon.core.util.enum.BaseEnum
All Implemented Interfaces:
Serializable
Direct Known Subclasses:
BootStrapperStateEnum, DayOfWeekEnum, LifecycleStateEnum, MailContentTypeEnum, MBeanOperationImpactEnum, MonthEnum, OperationsImpactEnum, QueueFullPolicyEnum, ResultSetConcurrencyEnum, ResultSetTypeEnum, SeverityEnum, TaskStatusEnum

public abstract class BaseEnum
extends Object
implements Serializable

This class acts as the base enumeration for all enumerated types in the system. It provides a simple and convenient mechanism to create new sets of enumeration instances and access them via ordinal or name based lookup.

This class also provides for the system wide storage of these objects, via static reference, so that all instances of a particular value of an enumerated type will in fact be the same instance. This has the advantage of allowing reference equals ("==") operations to work for instance comparison providing for very fast checks.

This base implementation will return null from most methods if unable to return the requested information. Subclasses are expected to check for these nulls and throw an appropriate subclass of EnumNotFoundException. Copyright 2002 Sapient

Since:
carbon 1.0
Version:
$Revision: 1.18 $($Author: dvoet $ / $Date: 2003/05/05 21:21:22 $)
Author:
Greg Hinkle, January 2002
See Also:
Serialized Form

Nested Class Summary
private static class BaseEnum.EnumTypeAggregator
          This inner class of the BaseEnum system catalogs all enumeration instances for one particular class of enumerated values.
 
Field Summary
private static Map enumTypeMap
          A static cache of the enum types currently loaded in the system.
protected  String name
          The string representation of this enumeration instance
protected  int ordinal
          The ordinal representation of this enumeration instance
 
Constructor Summary
protected BaseEnum(Class enumeratedType, String name)
          Constructs a new instance of the specific enumerated type automatically selecting the next ordinal value for it and placing references to it into the main catalogue for this type.
protected BaseEnum(Class enumeratedType, String name, int ordinal)
          Constructs a new instance of the specific enumerated type with the specified name and ordinal values.
 
Method Summary
 boolean equals(Object that)
          This implementation of equals does instance reference comparisons since all instances of single type actually are the same object.
static BaseEnum getByName(String name, Class enumeratedType)
          Static accessor to the specified enumerated type value.
static BaseEnum getByOrdinal(int ordinal, Class enumeratedType)
          Static accessor to the specified enumerated type value.
private static BaseEnum.EnumTypeAggregator getEnumTypeAggregator(Class enumeratedType)
          Gets the EnumTypeAggregator for the given enumeratedType
 String getName()
          Accesses the name of this enumeration instance
 int getOrdinal()
          Accesses the ordinal represenation of this enumeration instance
 int hashCode()
          This implementation of hashCode provides the system hashCode (location in memory) since all instances of single type actually are the same object.
static Iterator iterator(Class enumeratedType)
          Static accessor to an iterator for all instances of a specific enumeration type.
 String toString()
          Returns a string representation of this enumeration instance that includes its true class type, its enumeration name and its ordinal value.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

name

protected String name
The string representation of this enumeration instance


ordinal

protected int ordinal
The ordinal representation of this enumeration instance


enumTypeMap

private static Map enumTypeMap
A static cache of the enum types currently loaded in the system.

Constructor Detail

BaseEnum

protected BaseEnum(Class enumeratedType,
                   String name)

Constructs a new instance of the specific enumerated type automatically selecting the next ordinal value for it and placing references to it into the main catalogue for this type.

Note: Never mix the usage of the two constructors of this class in subclasses. This can lead to improperly numbered enumerations.

Parameters:
enumeratedType - the class type to build
name - the name or system wide string key to the enum instance

BaseEnum

protected BaseEnum(Class enumeratedType,
                   String name,
                   int ordinal)
Constructs a new instance of the specific enumerated type with the specified name and ordinal values. Use this constructor for explicit control over the ordinal values of enumeration instances.

Note: Never mix the usage of the two constructors of this class in subclasses. This can lead to improperly numbered enumerations.

Parameters:
enumeratedType - the subclass of BaseEnum to create an enumeration instance of
name - the String representation (name) of this type
ordinal - the ordinal value reperesentation of this type
Method Detail

getName

public String getName()
Accesses the name of this enumeration instance

Returns:
String the name of this enumeration

getOrdinal

public int getOrdinal()
Accesses the ordinal represenation of this enumeration instance

Returns:
long the ordinal value of this enumeration

toString

public String toString()
Returns a string representation of this enumeration instance that includes its true class type, its enumeration name and its ordinal value.

Overrides:
toString in class Object
Returns:
a String representation of this value

equals

public final boolean equals(Object that)
This implementation of equals does instance reference comparisons since all instances of single type actually are the same object. This method can not, and should not, be overriden.

Overrides:
equals in class Object
Parameters:
that - the enum object to compare against
Returns:
true if they are the same object, false otherwise

hashCode

public final int hashCode()
This implementation of hashCode provides the system hashCode (location in memory) since all instances of single type actually are the same object. This method can not, and should not, be overriden.

Overrides:
hashCode in class Object
Returns:
a system hashcode of the object

getByName

public static final BaseEnum getByName(String name,
                                       Class enumeratedType)
Static accessor to the specified enumerated type value. Will return a specific instance of that enumerated type by its name.

Parameters:
name - the name of the enumeration instance to return
enumeratedType - a subclass of BaseEnum which is the type of the instance to return
Returns:
the BaseEnum subclass that corresponds to this type and name

getByOrdinal

public static final BaseEnum getByOrdinal(int ordinal,
                                          Class enumeratedType)
Static accessor to the specified enumerated type value. Will return a specific instance of that enumerated type by its name.

Parameters:
ordinal - the ordinal value of this
enumeratedType - a subclass of BaseEnum which is the type of the instance to return
Returns:
the BaseEnum subclass that corresponds to this type and ordinal

iterator

public static final Iterator iterator(Class enumeratedType)
Static accessor to an iterator for all instances of a specific enumeration type. This iterator is ordered by the ordinal value of the enumeration instances.

Parameters:
enumeratedType - a subclass of BaseEnum which is the type of the instance to return
Returns:
Iterator an iterator over the values of this enumerated type

getEnumTypeAggregator

private static BaseEnum.EnumTypeAggregator getEnumTypeAggregator(Class enumeratedType)
Gets the EnumTypeAggregator for the given enumeratedType

Since:
carbon 1.2


Copyright 1999-2003 Sapient Corporation. All Rights Reserved.