001: package com.technoetic.xplanner.domain;
002:
003: import java.io.Serializable;
004: import java.io.InvalidObjectException;
005:
006: /**
007: * Created by IntelliJ IDEA.
008: * User: sg897500
009: * Date: Aug 9, 2005
010: * Time: 2:49:51 PM
011: * To change this template use File | Settings | File Templates.
012: */
013: public abstract class CharacterEnum implements Serializable {
014:
015: protected final char code;
016: protected final String name;
017:
018: public abstract String getAbbreviationKey();
019:
020: public CharacterEnum(char code, String name) {
021: this .code = code;
022: this .name = name;
023: }
024:
025: public abstract String getNameKey();
026:
027: public String getName() {
028: return name;
029: }
030:
031: public char getCode() {
032: return code;
033: }
034:
035: public boolean equals(Object obj) {
036: if (this == obj)
037: return true;
038: if (obj == null || !(obj instanceof CharacterEnum))
039: return false;
040: return this .getName().equals(((CharacterEnum) obj).getName());
041: }
042:
043: public int hashCode() {
044: return code;
045: }
046:
047: public String toString() {
048: return getName();
049: }
050:
051: //See http://www.javaworld.com/javaworld/javatips/jw-javatip122.html for the reason of this method
052: private Object readResolve() throws InvalidObjectException {
053: CharacterEnum[] enums = listEnums();
054: for (int i = 0; i < enums.length; i++) {
055: if (enums[i].getName().equals(getName()))
056: return enums[i];
057: }
058: throw new InvalidObjectException("Wrong enum value (" + code
059: + "," + name + ")");
060: }
061:
062: public abstract CharacterEnum[] listEnums();
063:
064: protected static CharacterEnum valueOf(String name,
065: CharacterEnum[] enums) {
066: if (name == null)
067: return null;
068: for (int i = 0; i < enums.length; i++) {
069: CharacterEnum enumeration = enums[i];
070: if (enumeration.getName().equals(name))
071: return enumeration;
072: }
073:
074: throw new RuntimeException("Unknown enum name " + name);
075: }
076:
077: protected static CharacterEnum fromName(String name,
078: CharacterEnum[] enums) {
079: return valueOf(name, enums);
080: }
081:
082: protected static CharacterEnum fromNameKey(String key,
083: CharacterEnum[] enums) {
084: if (key == null)
085: return null;
086: for (int i = 0; i < enums.length; i++) {
087: CharacterEnum enumeration = enums[i];
088: if (enumeration.getNameKey().equals(key))
089: return enumeration;
090: }
091: throw new RuntimeException("Unknown enum key " + key);
092: }
093:
094: protected static CharacterEnum fromCode(char code,
095: CharacterEnum[] enums) {
096: for (int i = 0; i < enums.length; i++) {
097: CharacterEnum enumeration = enums[i];
098: if (enumeration.code == code)
099: return enumeration;
100: }
101: throw new RuntimeException("Unknown enum code " + code);
102: }
103: }
|