1
2
3
4
5 package com.jguild.jrpm.io.constant;
6
7 import java.util.HashMap;
8 import java.util.TreeMap;
9
10
11 /***
12 * Abstract Enum class.
13 *
14 * @version $Id: EnumDelegate.java,v 1.2 2003/10/20 16:32:12 mkuss Exp $
15 */
16 class EnumDelegate implements EnumIf {
17 private static final HashMap map = new HashMap();
18 public static final int _UNKNOWN = -1;
19 private String name;
20 private long id;
21
22 /***
23 * Creates a new EnumDelegate object.
24 *
25 * @param refClass Referencing class for this enum (the class that implements the enum)
26 * @param id A id for this enum
27 * @param name A name for this enum
28 * @param realEnum The actual reference of the enum object (the real enum object that implements EnumIf)
29 */
30 public EnumDelegate(Class refClass, long id, String name, EnumIf realEnum) {
31 this.id = id;
32 this.name = name;
33
34 MapEntry mapEntry;
35
36 if (map.containsKey(refClass)) {
37 mapEntry = (MapEntry) map.get(refClass);
38 } else {
39 mapEntry = new MapEntry();
40 map.put(refClass, mapEntry);
41 }
42
43 mapEntry.idMap.put(new Long(id), realEnum);
44 mapEntry.nameMap.put(name.toLowerCase(), realEnum);
45 }
46
47 /***
48 * Get a enum by id
49 *
50 * @param refClass referencing class
51 * @param id The id of the enum
52 * @return The enum object
53 */
54 public static EnumIf getEnumById(Class refClass, long id) {
55 EnumIf ret = null;
56 MapEntry mapEntry = null;
57
58 mapEntry = (MapEntry) map.get(refClass);
59
60 if (mapEntry != null) {
61 ret = (EnumIf) mapEntry.idMap.get(new Long(id));
62
63 if (ret == null) {
64 ret = (EnumIf) mapEntry.idMap.get(new Long(_UNKNOWN));
65 }
66 }
67
68 return ret;
69 }
70
71 /***
72 * Get a enum by name
73 *
74 * @param refClass referencing class
75 * @param name The name of the enum
76 * @return The enum object
77 */
78 public static EnumIf getEnumByName(Class refClass, String name) {
79 EnumIf ret = null;
80 MapEntry mapEntry = null;
81
82 mapEntry = (MapEntry) map.get(refClass);
83
84 if (mapEntry != null) {
85 ret = (EnumIf) mapEntry.nameMap.get(name.toLowerCase());
86
87 if (ret == null) {
88 ret = (EnumIf) mapEntry.idMap.get(new Long(_UNKNOWN));
89 }
90 }
91
92 return ret;
93 }
94
95 /***
96 * Get all defined enums of this class
97 *
98 * @param refClass referencing class
99 * @return An array of all defined enum objects
100 */
101 public static String[] getEnumNames(Class refClass) {
102 String[] ret = new String[0];
103 MapEntry mapEntry = null;
104
105 mapEntry = (MapEntry) map.get(refClass);
106
107 if (mapEntry != null) {
108 ret = (String[]) mapEntry.nameMap.keySet().toArray(ret);
109 }
110
111 return ret;
112 }
113
114
115
116
117 public long getId() {
118 return id;
119 }
120
121
122
123
124 public String getName() {
125 return name;
126 }
127
128 /***
129 * Check if this enum class contains a enum of a specified id
130 *
131 * @param refClass referencing class
132 * @param id The id of the enum
133 * @return TRUE if the enum is defined in this class
134 */
135 public static boolean containsEnumId(Class refClass, Long id) {
136 boolean ret = false;
137 MapEntry mapEntry = null;
138
139 mapEntry = (MapEntry) map.get(refClass);
140
141 if (mapEntry != null) {
142 ret = mapEntry.idMap.containsKey(id);
143 }
144
145 return ret;
146 }
147
148
149
150
151 public String toString() {
152 return id + " : " + name;
153 }
154
155 /***
156 * Inner class to map ids and names of enums
157 *
158 * @author kuss
159 */
160 private class MapEntry {
161 TreeMap idMap = new TreeMap();
162 TreeMap nameMap = new TreeMap();
163 }
164 }