The Carbon Java Framework  

The Carbon Utilities Subsystem

Enumerations Usage

Printer Friendly Version

Author: Greg Hinkle (ghinkle at sapient.com)
Version: $Revision: 1.3 $($Author: dvoet $ / $Date: 2003/04/29 21:21:52 $)
Created: May 2002

Introduction

The BaseEnum base class acts as a utility base- class for robust enumerated types. This document describes how to use instances of that class and how to create new enumeration types.

Purpose

Enumeration types are designed to be strongly type instances of an object type with a finite number of possible values. In Java, they are designed to provide a simple mechanism for tracking those different values and provides compile-time checking.

Creating new Enumerations

New enumeration types should be created for any technical or business type that contains a small number of possible values. Then any time this data is used in the system it should always be handled as the Enumerated type. It should also be noted that the boundaries of the system should be clearly demarcated and that is the point at which translation to and from the Enumerated type object should happen. This would include things such as drop-down in the presentation tier and for example the data version in a database at the persistence tier.

Account type example

Now we will create an example enumeration type that is designed to track the type of a banking account around an online bank site. In this case there are three main types of accounts: Single, Joint and Money Market. These account types impact presentation and business logic and need to be used at all tiers of the application.

To create this AccountEnum class we can use the template provided with the Framework and simply alter the class name as below. We also add to the top of the file the static instantiations of the configuration types we want to support.

AccountEnum.java example enumerated type

public class AccountEnum extends BaseEnum implements Serializable {

   /** Single Account Type */
   public static final SINGLE_ACCOUNT = new AccountEnum("Single");

   /** Joint Account Type */
   public static final JOINT_ACCOUNT = new AccountEnum("Joint");

   /** Single Account Type */
   public static final MONEY_MARKET_ACCOUNT = new AccountEnum("Money Market");


    /**
     * Constructs a enumeration of type AccountEnum
     *
     * @param name the String representation of this enumeration
     */
    private AccountEnum(String name) {
        super(AccountEnum.class, name);
    }


    /**
     * Looks up a AccountEnum by its string representation
     *
     * @param name the String name of a enumeration instance
     * @return a specific AccountEnum
     * @throws AccountEnumNotFoundException when there is no
     *   enumeration instance for the given name
     */
    public static final AccountEnum getByName(String name)
        throws AccountEnumNotFoundException {
        AccountEnum enum =  (AccountEnum)
            BaseEnum.getByName(name, AccountEnum.class);
        if (enum != null) {
            return enum;
        } else {
            throw new AccountEnumNotFoundException(name);
        }
    }

    /**
     * Looks up a AccountEnum by its ordinal representation
     *
     * @param ordinal the integer ordinal value of a specific enumeration instance
     * @return an specific AccountEnum
     * @throws AccountEnum NotFoundException when there is no enumeration instance
     *   for the given ordinal
     */
    public static AccountEnum  getByOrdinal(long ordinal)
        throws AccountEnumNotFoundException {

        AccountEnum enum =  (AccountEnum)
        BaseEnum.getByOrdinal(ordinal, AccountEnum.class);
        if (enum != null) {
            return enum;
        } else {
            throw new AccountEnumNotFoundException(
                (new Long(ordinal)).toString());
        }
    }

    /**
     * Retrieves and iterator by ascending ordinal order for all enumerations
     * of this type.
     *
     * @return an iterator over all AccountEnum values
     */
    public static Iterator iterator() {
        return BaseEnum.iterator(AccountEnum.class);
    }

    /**
     * Overrides part of serialization to return a reference to an
     * enumeration instance that is managed by the Enumeration Subsystem. These
     */
    final Object readResolve() throws ObjectStreamException {
        return super.getByOrdinal(this.ordinal, AccountEnum.class);
    }

    /**
     * This class is a typesafe retriever excepection for an enumeration
     */
    public static class AccountEnumNotFoundException extends EnumNotFoundException {
        public AccountEnumNotFoundException(String name) {
           super(name);
        }
    }
}

Example business logic

This class will now let us create and manipulate the enumerated type of accounts throughout the system. An example below shows us utilizing the account type attribute of an account object to do type specific business actions.

import com.mycompany.myapp.util.AccountEnum;

...

public void doSomething(Account account) {

    if (account.getAccountType() == AccountEnum.SINGLE_ACCOUNT) {
        ...
    } else if (account.getAccountType() == AccountEnum.JOINT_ACCOUNT) {
        ...
    } else if (account.getAccountType() == AccountEnum.MONEY_MARKET_ACCOUNT) {
        ...
    }
}

Transformation

The following example shows how to save and restore enumerated types from a database column holding their string values.

Persistence storage example

PreparedStatement stmt =
    Connection.prepareStatement(
        "insert into accounts (name, type) values (?, ?)");

stmt.setString(1, account.getName());

stmt.setString(2, accoung.getAccountType().getName());

...

Persistence retrieval example

PreparedStatement stmt =
    Connection.prepareStatement(
        "select name, type from accounts where id = ?");
...
account.setName(rset.getString(1));
account.setAccountType(AccountEnum.getByName(rset.getString(2)));
...

Copyright © 2001-2003, Sapient Corporation