1 /**
2 * Redistribution and use of this software and associated documentation
3 * ("Software"), with or without modification, are permitted provided
4 * that the following conditions are met:
5 *
6 * 1. Redistributions of source code must retain copyright
7 * statements and notices. Redistributions must also contain a
8 * copy of this document.
9 *
10 * 2. Redistributions in binary form must reproduce the
11 * above copyright notice, this list of conditions and the
12 * following disclaimer in the documentation and/or other
13 * materials provided with the distribution.
14 *
15 * 3. The name "Exolab" must not be used to endorse or promote
16 * products derived from this Software without prior written
17 * permission of Intalio, Inc. For written permission,
18 * please contact info@exolab.org.
19 *
20 * 4. Products derived from this Software may not be called "Exolab"
21 * nor may "Exolab" appear in their names without prior written
22 * permission of Intalio, Inc. Exolab is a registered
23 * trademark of Intalio, Inc.
24 *
25 * 5. Due credit should be given to the Exolab Project
26 * (http://www.exolab.org/).
27 *
28 * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32 * INTALIO, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39 * OF THE POSSIBILITY OF SUCH DAMAGE.
40 *
41 * Copyright 1999-2002 (C) Intalio, Inc. All Rights Reserved.
42 */
43 package org.exolab.javasource;
44
45 import java.util.Enumeration;
46 import java.util.LinkedHashMap;
47 import java.util.Map;
48
49 /**
50 * Describes the definition of a enum type class.
51 *
52 * @author <a href="mailto:andrew DOT fawcett AT coda DOT com">Andrew Fawcett</a>
53 * @version $Revision$ $Date: 2006-04-25 16:09:10 -0600 (Tue, 25 Apr 2006) $
54 */
55 public final class JEnum extends JClass {
56
57 /** The list of elements of this JEnumConstant. */
58 private Map<String, JEnumConstant> _enumConstants = new LinkedHashMap<String, JEnumConstant>();
59
60 /**
61 * Construct JEnum with given name.
62 *
63 * @param name The name for this JEnum.
64 */
65 public JEnum(final String name) {
66 super(name);
67
68 //-- initialize default Java doc
69 getJDocComment().setComment("Enumeration " + getLocalName() + ".");
70 }
71
72 /**
73 * Override to only allow private constructors.
74 * @param constructor The constructor that should be added.
75 */
76 public void addConstructor(final JConstructor constructor) {
77 if (constructor.getModifiers().isPrivate()) {
78 super.addConstructor(constructor);
79 }
80 }
81
82 //--------------------------------------------------------------------------
83
84 /**
85 * Adds the given {@link JMember} to this {@link JEnum}.
86 *
87 * @param jMember The {@link JMember} to add.
88 */
89 public void addMember(final JMember jMember) {
90 if (jMember instanceof JEnumConstant) {
91 addEnumConstant((JEnumConstant) jMember);
92 } else {
93 super.addMember(jMember);
94 }
95 }
96
97 /**
98 * Adds the given {@link JEnumConstant} to this {@link JEnum}.
99 *
100 * @param jEnumConstant The constant to add.
101 */
102 public void addEnumConstant(final JEnumConstant jEnumConstant) {
103 if (jEnumConstant == null) {
104 throw new IllegalArgumentException("Enum fields cannot be null");
105 }
106
107 String name = jEnumConstant.getName();
108 if (_enumConstants.get(name) != null) {
109 String err = "duplicate name found: " + name;
110 throw new IllegalArgumentException(err);
111 }
112 _enumConstants.put(name, jEnumConstant);
113 }
114
115 /**
116 * Returns the member with the given name, or null if no member was found
117 * with the given name.
118 *
119 * @param name The name of the member to return.
120 * @return The member with the given name, or null if no member was found
121 * with the given name.
122 */
123 public JEnumConstant getEnumConstant(final String name) {
124 return _enumConstants.get(name);
125 }
126
127 /**
128 * Returns an array of all the JEnumConstant of this JEnum.
129 *
130 * @return An array of all the JEnumConstant of this JEnum.
131 */
132 public JEnumConstant[] getEnumConstants() {
133 return _enumConstants.values().toArray(new JEnumConstant[_enumConstants.size()]);
134 }
135
136 /**
137 * Returns the number of enum constants.
138 *
139 * @return The number of enum constants.
140 */
141 public int getEnumConstantCount() {
142 return this._enumConstants.size();
143 }
144
145 /**
146 * {@inheritDoc}
147 */
148 public void print(final JSourceWriter jsw, final boolean classOnly) {
149 if (jsw == null) {
150
151 throw new IllegalArgumentException("argument 'jsw' should not be null.");
152 }
153
154 //-- print class headers (comment header, package, imports) if desired
155 if (!classOnly) {
156 printClassHeaders(jsw);
157 }
158
159 getJDocComment().print(jsw);
160
161 printEnumDefinitionLine(jsw); // includes the opening '{'
162
163 jsw.writeln();
164 jsw.indent();
165
166 printEnumConstants(jsw);
167 printMemberVariables(jsw);
168 printStaticInitializers(jsw);
169 printConstructors(jsw);
170 printMethods(jsw);
171 printInnerClasses(jsw);
172
173 jsw.unindent();
174 jsw.writeln('}');
175 jsw.flush();
176 }
177
178 /**
179 * Writes to the JSourceWriter the line that defines this enum. This
180 * line includes the enum name, implements entries, and
181 * any modifiers such as "private".
182 *
183 * @param jsw The JSourceWriter to be used.
184 */
185 private void printEnumDefinitionLine(final JSourceWriter jsw) {
186 StringBuilder buffer = new StringBuilder();
187
188 //-- print annotations
189 getAnnotatedElementHelper().printAnnotations(jsw);
190
191 JModifiers modifiers = getModifiers();
192 if (modifiers.isPrivate()) {
193 buffer.append("private ");
194 } else if (modifiers.isPublic()) {
195 buffer.append("public ");
196 }
197
198 buffer.append("enum ");
199 buffer.append(getLocalName());
200 buffer.append(' ');
201 if (getInterfaceCount() > 0) {
202 boolean endl = false;
203 if ((getInterfaceCount() > 1)) {
204 jsw.writeln(buffer.toString());
205 buffer.setLength(0);
206 endl = true;
207 }
208 buffer.append("implements ");
209
210 Enumeration<String> enumeration = getInterfaces();
211 while (enumeration.hasMoreElements()) {
212 buffer.append(enumeration.nextElement());
213 if (enumeration.hasMoreElements()) { buffer.append(", "); }
214 }
215 if (endl) {
216 jsw.writeln(buffer.toString());
217 buffer.setLength(0);
218 } else {
219 buffer.append(' ');
220 }
221 }
222
223 buffer.append('{');
224 jsw.writeln(buffer.toString());
225 buffer.setLength(0);
226 }
227
228 /**
229 * Writes to the JSourceWriter all constants belonging to this enum.
230 *
231 * @param jsw The JSourceWriter to be used.
232 */
233 private void printEnumConstants(final JSourceWriter jsw) {
234 if (!_enumConstants.isEmpty()) {
235 jsw.writeln();
236 jsw.writeln(" //------------------/");
237 jsw.writeln(" //- Enum Constants -/");
238 jsw.writeln("//------------------/");
239 jsw.writeln();
240 }
241
242 int i = 0;
243 for (JEnumConstant jConstant : _enumConstants.values()) {
244 i++;
245 jConstant.print(jsw);
246 if (i < _enumConstants.size()) {
247 jsw.write(",");
248 } else {
249 jsw.write(";");
250 }
251 jsw.writeln();
252 }
253 }
254
255 }