1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 package org.exolab.javasource;
34
35 import java.util.Enumeration;
36 import java.util.LinkedHashMap;
37 import java.util.Map;
38
39
40
41
42
43
44
45 public final class JEnum extends JClass {
46
47
48 private Map<String, JEnumConstant> _enumConstants = new LinkedHashMap<String, JEnumConstant>();
49
50
51
52
53
54
55 public JEnum(final String name) {
56 super(name);
57
58
59 getJDocComment().setComment("Enumeration " + getLocalName() + ".");
60 }
61
62
63
64
65
66
67 public void addConstructor(final JConstructor constructor) {
68 if (constructor.getModifiers().isPrivate()) {
69 super.addConstructor(constructor);
70 }
71 }
72
73
74
75
76
77
78
79
80 public void addMember(final JMember jMember) {
81 if (jMember instanceof JEnumConstant) {
82 addEnumConstant((JEnumConstant) jMember);
83 } else {
84 super.addMember(jMember);
85 }
86 }
87
88
89
90
91
92
93 public void addEnumConstant(final JEnumConstant jEnumConstant) {
94 if (jEnumConstant == null) {
95 throw new IllegalArgumentException("Enum fields cannot be null");
96 }
97
98 String name = jEnumConstant.getName();
99 if (_enumConstants.get(name) != null) {
100 String err = "duplicate name found: " + name;
101 throw new IllegalArgumentException(err);
102 }
103 _enumConstants.put(name, jEnumConstant);
104 }
105
106
107
108
109
110
111
112 public JEnumConstant getEnumConstant(final String name) {
113 return _enumConstants.get(name);
114 }
115
116
117
118
119
120
121 public JEnumConstant[] getEnumConstants() {
122 return _enumConstants.values().toArray(new JEnumConstant[_enumConstants.size()]);
123 }
124
125
126
127
128
129
130 public int getEnumConstantCount() {
131 return this._enumConstants.size();
132 }
133
134
135
136
137 public void print(final JSourceWriter jsw, final boolean classOnly) {
138 if (jsw == null) {
139
140 throw new IllegalArgumentException("argument 'jsw' should not be null.");
141 }
142
143
144 if (!classOnly) {
145 printClassHeaders(jsw);
146 }
147
148 getJDocComment().print(jsw);
149
150 printEnumDefinitionLine(jsw);
151
152 jsw.writeln();
153 jsw.indent();
154
155 printEnumConstants(jsw);
156 printMemberVariables(jsw);
157 printStaticInitializers(jsw);
158 printConstructors(jsw);
159 printMethods(jsw);
160 printInnerClasses(jsw);
161
162 jsw.unindent();
163 jsw.writeln('}');
164 jsw.flush();
165 }
166
167
168
169
170
171
172
173 private void printEnumDefinitionLine(final JSourceWriter jsw) {
174 StringBuilder buffer = new StringBuilder();
175
176
177 getAnnotatedElementHelper().printAnnotations(jsw);
178
179 JModifiers modifiers = getModifiers();
180 if (modifiers.isPrivate()) {
181 buffer.append("private ");
182 } else if (modifiers.isPublic()) {
183 buffer.append("public ");
184 }
185
186 buffer.append("enum ").append(getLocalName()).append(' ');
187 if (getInterfaceCount() > 0) {
188 boolean endl = false;
189 if (getInterfaceCount() > 1) {
190 jsw.writeln(buffer.toString());
191 buffer.setLength(0);
192 endl = true;
193 }
194 buffer.append("implements ");
195
196 Enumeration<String> enumeration = getInterfaces();
197 while (enumeration.hasMoreElements()) {
198 buffer.append(enumeration.nextElement());
199 if (enumeration.hasMoreElements()) {
200 buffer.append(", ");
201 }
202 }
203 if (endl) {
204 jsw.writeln(buffer.toString());
205 buffer.setLength(0);
206 } else {
207 buffer.append(' ');
208 }
209 }
210
211 buffer.append('{');
212 jsw.writeln(buffer.toString());
213 }
214
215
216
217
218
219
220 private void printEnumConstants(final JSourceWriter jsw) {
221 if (!_enumConstants.isEmpty()) {
222 jsw.writeln();
223 jsw.writeln(" //------------------/");
224 jsw.writeln(" //- Enum Constants -/");
225 jsw.writeln("//------------------/");
226 jsw.writeln();
227 }
228
229 int i = 0;
230 for (JEnumConstant jConstant : _enumConstants.values()) {
231 i++;
232 jConstant.print(jsw);
233 if (i < _enumConstants.size()) {
234 jsw.write(",");
235 } else {
236 jsw.write(";");
237 }
238 jsw.writeln();
239 }
240 }
241
242 }