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);
}
}
}