01: /*
02: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
03: * Distributed under the terms of either:
04: * - the common development and distribution license (CDDL), v1.0; or
05: * - the GNU Lesser General Public License, v2.1 or later
06: * $Id: EnumClass.java 3634 2007-01-08 21:42:24Z gbevin $
07: */
08: package com.uwyn.rife.datastructures;
09:
10: import java.util.Collection;
11: import java.util.HashMap;
12:
13: /**
14: * The purpose of this abstract base class is to allow the creation of
15: * type-safe enumerations.
16: * <p>Only the derived class is allowed to create instances and should do so
17: * as <code>public static final</code> objects.
18: * <p>Each instance of a <code>EnumClass</code> class needs an identifier to
19: * its constructor. This identifier is used to uniquely differentiate
20: * enumeration members amongst each-other.
21: *
22: * @author Geert Bevin (gbevin[remove] at uwyn dot com)
23: * @version $Revision: 3634 $
24: * @since 1.0
25: */
26: public abstract class EnumClass<IdentifierType> {
27: private static HashMap<String, HashMap<Object, EnumClass>> sTypes = new HashMap<String, HashMap<Object, EnumClass>>();
28:
29: protected IdentifierType mIdentifier = null;
30:
31: protected EnumClass(IdentifierType identifier) {
32: registerType(this .getClass(), identifier);
33: }
34:
35: protected EnumClass(Class klass, IdentifierType identifier) {
36: registerType(klass, identifier);
37: }
38:
39: protected final void registerType(Class klass,
40: IdentifierType identifier) {
41: assert klass != null;
42: assert identifier != null;
43:
44: String class_name = klass.getName();
45: HashMap<Object, EnumClass> instances = null;
46:
47: if (!sTypes.containsKey(class_name)) {
48: instances = new HashMap<Object, EnumClass>();
49: sTypes.put(class_name, instances);
50: } else {
51: instances = sTypes.get(class_name);
52: }
53: mIdentifier = identifier;
54: instances.put(mIdentifier, this );
55: }
56:
57: protected static Collection<?> getIdentifiers(
58: Class<? extends EnumClass> type) {
59: return sTypes.get(type.getName()).keySet();
60: }
61:
62: protected static Collection<? extends EnumClass> getMembers(
63: Class<? extends EnumClass> type) {
64: return sTypes.get(type.getName()).values();
65: }
66:
67: protected static <MemberType extends EnumClass> MemberType getMember(
68: Class<MemberType> type, Object identifier) {
69: return (MemberType) sTypes.get(type.getName()).get(identifier);
70: }
71:
72: public String toString() {
73: return mIdentifier.toString();
74: }
75:
76: public int hashCode() {
77: return mIdentifier.hashCode();
78: }
79:
80: public boolean equals(Object object) {
81: if (null == object) {
82: return false;
83: }
84:
85: if (object instanceof EnumClass) {
86: EnumClass other_enumclass = (EnumClass) object;
87: if (null != other_enumclass
88: && other_enumclass.mIdentifier.equals(mIdentifier)) {
89: return true;
90: } else {
91: return false;
92: }
93: }
94:
95: return object.equals(mIdentifier);
96: }
97: }
|