ch.elca.el4j.util.codingsupport
Class AbstractDefaultEnum

java.lang.Object
  extended by ch.elca.el4j.util.codingsupport.AbstractDefaultEnum
All Implemented Interfaces:
Serializable
Direct Known Subclasses:
AbstractComparableEnum

public abstract class AbstractDefaultEnum
extends Object
implements Serializable

The parent class for enums using the typesafe enum pattern (these enums are not comparable). Use the AbstractComparableEnum class as a parent if you need comparable enums. The pattern ensures that enums are correctly serialized and that there are some common accessor methods.

Concepts

How to Use

Usage example

The following code shows how to write an enumeration with the three possible values
{ASSERT, REQUIRE, ENSURE}:
 package ch.elca.el4j.codingsupport;

 import java.io.ObjectStreamException;
 // only needed due to a IBM JDK 1.2.2 bug

 /**
  * Represents the type of an assertion.
  * It uses the typesafe enum pattern for 
* enum AssertType { ASSERT, REQUIRE, ENSURE }; *

* * The code for class is potentially generated. * * / public class AssertType extends AbstractDefaultEnum { // define a private constructor with the two arguments // and delegate construction // to the parent class's constructor: private AssertType(String name, int code) { super(name, code); } // now define the elements: public static final AssertType ASSERT = new AssertType("ASSERT",1); public static final AssertType REQUIRE = new AssertType("REQUIRE",2); public static final AssertType ENSURE = new AssertType("ENSURE",3); } // Customize the get(String) method public static AssertType get(String name) { return (AssertType) AbstractDefaultEnum.get(AssertType.class, name); } }

Deployment Environment

The duplication of readResolve method in each enumeration is not needed because JDK 1.2.2 is no longer supported by EL4J. See also AbstractComparableEnum for a variant of this class that allow comparisons (i.e. it implements Comparable).
The pattern for the implementation of this class was inspired by the book "Effective Java" by Joshua Bloch.

Author:
Raphael Boog (RBO)
See Also:
Typesafe Enum Pattern Pitfalls, Serialized Form
File-location:
AbstractDefaultEnum
Last check-in date:
2009-08-04 13:59:45 +0200 (Di, 04. Aug 2009) by swismer for revision 3873

Field Summary
protected  int m_code
          The integer code for this enum element.
protected  String m_name
          The name of this enum element.
protected static Hashtable<String,Object> s_singletons
          A hastable containing all instances of this class.
 
Constructor Summary
protected AbstractDefaultEnum(String name, int code)
          Constructor that initializes the name and and adds this instance into the signletons list along with its key.
 
Method Summary
 boolean equals(Object that)
          Return true if and only if that object is equivalent to the object this operation is invoked upon.
protected static Object get(Class<?> myClass, String name)
          Return the named enum (names are case sensitive!).
 int hashCode()
          Retruns the hash code of this instance.
 Object readResolve()
          Performs an unique return of this instance after deserialzation.
 int toInt()
           
 String toString()
          Return the enum's name.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

s_singletons

protected static final Hashtable<String,Object> s_singletons
A hastable containing all instances of this class.


m_name

protected String m_name
The name of this enum element.


m_code

protected int m_code
The integer code for this enum element.

Constructor Detail

AbstractDefaultEnum

protected AbstractDefaultEnum(String name,
                              int code)
Constructor that initializes the name and and adds this instance into the signletons list along with its key. The key is comprised of the class name and the name of this enum

Parameters:
name - Is the name of this instance.
code - Is the code.
Method Detail

get

protected static Object get(Class<?> myClass,
                            String name)
Return the named enum (names are case sensitive!).

Parameters:
myClass - Is the type to get.
name - Is the name of the instantiated class.
Returns:
the named enum or null in case it is not found.

toString

public String toString()
Return the enum's name.

Overrides:
toString in class Object
Returns:
the enum's name.

toInt

public int toInt()
Returns:
Return the enum's code.

equals

public final boolean equals(Object that)
Return true if and only if that object is equivalent to the object this operation is invoked upon. Remark: due to the fact that there is only one Object for each element of an enumeration, the == operator has the same semantics (but more efficient).

Overrides:
equals in class Object

hashCode

public final int hashCode()
Retruns the hash code of this instance.

Overrides:
hashCode in class Object

readResolve

public Object readResolve()
                   throws ObjectStreamException
Performs an unique return of this instance after deserialzation.
This method has no effect with the JDK 1.2.2 of IBM. If you use the JDK version, this readResolve method must be duplicated in each enum subclass. EL4J does not support it any longer.

Returns:
readResolve
Throws:
ObjectStreamException - On reading error.


Copyright © 2005-2011 ELCA. All Rights Reserved.