Create a class that approximates an equivalent Pascal enumeration or C enum. A specific enumeration will be represented by a class with specific instances corresponding to each element of the enumerations and public static final fields to access the instances.
  • Existing in Java Tiger 1.5

  • Elements are ordered and comparable
  • Enumeration elements are serializable
  • Enumeration element Name lookup is supported
  • Methods sequencing is included



Source Code

/**
 * Type Safe Enumeration: Colors elements: Red, Blue, Green, Yellow, White
 */
public final class Colors implements Comparable, Serializable {
	/**
	 * The map of enumeration elements to names.
	 */
	private static HashMap nameLookup = new HashMap(5);

	/**
	 * The array of enumeration elements.
	 */
	private static Colors[] ordinalLookup = new Colors[5];

	/**
	 * The "Red" enumeration element
	 */
	public static final Colors Red = new Colors("Red", 0);

	/**
	 * The "Blue" enumeration element
	 */
	public static final Colors Blue = new Colors("Blue", 1);

	/**
	 * The "Green" enumeration element
	 */
	public static final Colors Green = new Colors("Green", 2);

	/**
	 * The "Yellow" enumeration element
	 */
	public static final Colors Yellow = new Colors("Yellow", 3);

	/**
	 * The "White" enumeration element
	 */
	public static final Colors White = new Colors("White", 4);

	/**
	 * The string representation of the enumeration.
	 */
	private final String printName;

	/**
	 * The ordinal value of the enumeration used for comparison purposes.
	 */
	private final int ordinal;

	/**
	 * Create an enumeration element. Prevent instances of this class from being
	 * created externally.
	 * 
	 * @param name
	 *            the name of the enumeration element
	 * @param position
	 *            the ordinal position of the enumeration element
	 */
	private Colors(String name, int position) {
		this.ordinal = position;
		this.printName = name;
		ordinalLookup[position] = this;
		nameLookup.put(name, this);
	}

	/**
	 * Compare two enumeration elements
	 * 
	 * @param arg
	 *            the object to compare this to
	 * @return the difference between the ordinal values
	 * @see java.lang.ComparablecompareTo(Object)
	 */
	public int compareTo(Object arg) {
		return this.ordinal - ((Colors) arg).ordinal;
	}

	/**
	 * Return the string representation of the enumeration.
	 * 
	 * @return the name of the enumeration element
	 */
	public String toString() {
		return printName;
	}

	/**
	 * Resolve the enumeration element.
	 * 
	 * @return the resolved enumeration element
	 * @throws ObjectStreamException
	 *             if the enumeration element could not be resolved.
	 */
	private Object readResolve() throws ObjectStreamException {
		return ordinalLookup[ordinal];
	}

	/**
	 * Return the first enumeration element
	 * 
	 * @return the first enumeration element
	 */
	public static Colors first() {
		return ordinalLookup[0];
	}

	/**
	 * Return the last enumeration element
	 * 
	 * @return the last enumeration element
	 */
	public static Colors last() {
		return ordinalLookup[ordinalLookup.length - 1];
	}

	/**
	 * Return the enumeration element preceding this element
	 * 
	 * @return the enumeration element preceding this element
	 */
	public Colors predecessor() {
		return (this == first()) ? null : ordinalLookup[ordinal - 1];
	}

	/**
	 * Return the enumeration element following this element
	 * 
	 * @return the enumeration element following this element
	 */
	public Colors successor() {
		return (this == last()) ? null : ordinalLookup[ordinal + 1];
	}

	/**
	 * Return the enumeration element with the given name
	 * 
	 * @param name
	 *            the name of the enumeration element to find
	 * @return the named enumeration element
	 */
	public static Colors valueOf(String name) {
		return (Colors) nameLookup.get(name);
	}

}

You might like also

No Thumbnail was found
You do not create your objects but describe (using cofiguration file) how they should be created and wired together in code. A container (for ex: in case of Spring framework, the IOC container) is then responsible for hooking it all up. In a typical IOC scenario, the container creates all the objects, wires them together by setting the necessary properties, and determines when methods will be invoked. The three implementation pattern types for IOC are: Type 1 Services need to …
5019 Days ago
Singleton
Ensure a class only has one instance, and provide a global point of access to it. Statically Initialized The singleton class is implemented by defining a static field that is statically initialized (that is, the field is initialized when the class is initialized). This has the advantage that invocations of the method used to access the singleton do not incur the overhead of checking whether or not the instance has been created. Dynamically Initialized The singleton class is implemented by …
5476 Days ago
Dynamic polymorphic abstract factory
This package contains a dynamic polymorphic factory... New class can be add dynamically to the factory... even during runtime (dynamic)Factory methods are in a separate class as virtual functions. (polymorphism)Different types of factories can be subclassed from the basic factory.. (abstract)Useful iin case of licence problem, since Concrete classes are created at runtime, and only need to reside in classpath (If they are not present the code still compile). Below, the example show multiple authorization and autorisation scheme, that can …
5476 Days ago
Prototype
Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.Source Code/** * Declares interface for cloning itself. * @role __Prototype */ public interface Prototype /** * Implements an operation for cloning itself. */ public class ConcretePrototype1 implements Prototype public Prototype createCopy() } /** * Implements an operation for cloning itself. */ public class ConcretePrototype1 implements Prototype public Prototype createCopy() } …
5483 Days ago
Abstract factory
Provide an interface for creating families of related or dependent objects without specifying their concrete classes.Source Code/** * Abstract factory declares an interface for operations that create abstract * product objects. * * @role __Factory */ public interface AbstractFactory /** * Abstract factory declares an interface for operations that create abstract * product objects. * * @role __Factory */ public interface AbstractFactory /** * Abstract product - an interface for a type of Product object. * * @role __Product * …
5483 Days ago
Builder
Separate the construction of a complex object from its representation so that the same construction process can create different representations.Source Code /** * The interface Product defines interface to create parts of the * Product. */ public interface Builder /** * The ConcreteBuilder is the product of a concrete builder. * */ public class ConcreteBuilder implements Product /** * The ConcreteBuilderBuilder creates and assembles parts of * the Product. * */ public class ConcreteBuilderBuilder implements Builder /** * Construct the …
5483 Days ago