001: /**
002: * Redistribution and use of this software and associated documentation
003: * ("Software"), with or without modification, are permitted provided
004: * that the following conditions are met:
005: *
006: * 1. Redistributions of source code must retain copyright
007: * statements and notices. Redistributions must also contain a
008: * copy of this document.
009: *
010: * 2. Redistributions in binary form must reproduce the
011: * above copyright notice, this list of conditions and the
012: * following disclaimer in the documentation and/or other
013: * materials provided with the distribution.
014: *
015: * 3. The name "Exolab" must not be used to endorse or promote
016: * products derived from this Software without prior written
017: * permission of Intalio, Inc. For written permission,
018: * please contact info@exolab.org.
019: *
020: * 4. Products derived from this Software may not be called "Exolab"
021: * nor may "Exolab" appear in their names without prior written
022: * permission of Intalio, Inc. Exolab is a registered
023: * trademark of Intalio, Inc.
024: *
025: * 5. Due credit should be given to the Exolab Project
026: * (http://www.exolab.org/).
027: *
028: * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS
029: * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
030: * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
031: * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
032: * INTALIO, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
033: * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
034: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
035: * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
036: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
037: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
038: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
039: * OF THE POSSIBILITY OF SUCH DAMAGE.
040: *
041: * Copyright 1999-2002 (C) Intalio, Inc. All Rights Reserved.
042: */package org.exolab.javasource;
043:
044: import java.util.Enumeration;
045:
046: /**
047: * Describes the definition of a enum type class.
048: *
049: * @author <a href="mailto:andrew DOT fawcett AT coda DOT com">Andrew Fawcett</a>
050: * @version $Revision: 6669 $ $Date: 2006-04-25 16:09:10 -0600 (Tue, 25 Apr 2006) $
051: */
052: public final class JEnum extends AbstractJClass {
053: //--------------------------------------------------------------------------
054:
055: /** The list of elements of this JEnumConstant. */
056: private JNamedMap _constants;
057:
058: //--------------------------------------------------------------------------
059:
060: /**
061: * Construct JEnum with given name.
062: *
063: * @param name The name for this JEnum.
064: */
065: protected JEnum(final String name) {
066: super (name);
067:
068: _constants = new JNamedMap();
069:
070: //-- initialize default Java doc
071: getJDocComment().setComment(
072: "Enumeration " + getLocalName() + ".");
073: }
074:
075: //--------------------------------------------------------------------------
076:
077: /**
078: * {@inheritDoc}
079: */
080: public void addImport(final String className) {
081: if (className == null || className.length() == 0) {
082: return;
083: }
084: addImportInternal(className);
085: }
086:
087: /**
088: * Adds the given JMember to this JEnum.
089: *
090: * @param jMember The JMember to add.
091: */
092: public void addMember(final JMember jMember) {
093: if (jMember instanceof JEnumConstant) {
094: addConstant((JEnumConstant) jMember);
095: } else if (jMember instanceof JField) {
096: addField((JField) jMember);
097: } else if (jMember instanceof JMethod) {
098: addMethod((JMethod) jMember);
099: } else {
100: String error = null;
101: if (jMember == null) {
102: error = "the argument 'jMember' must not be null.";
103: } else {
104: error = "Cannot add JMember '"
105: + jMember.getClass().getName()
106: + "' to JClass, unrecognized type.";
107: }
108: throw new IllegalArgumentException(error);
109: }
110: }
111:
112: /**
113: * Adds the given JEnumConstant to this JEnum.
114: *
115: * @param jConstant The constant to add.
116: */
117: public void addConstant(final JEnumConstant jConstant) {
118: if (jConstant == null) {
119: throw new IllegalArgumentException(
120: "Enum fields cannot be null");
121: }
122:
123: String name = jConstant.getName();
124: if (_constants.get(name) != null) {
125: String err = "duplicate name found: " + name;
126: throw new IllegalArgumentException(err);
127: }
128: _constants.put(name, jConstant);
129: }
130:
131: /**
132: * Returns the member with the given name, or null if no member was found
133: * with the given name.
134: *
135: * @param name The name of the member to return.
136: * @return The member with the given name, or null if no member was found
137: * with the given name.
138: */
139: public JEnumConstant getConstant(final String name) {
140: return (JEnumConstant) _constants.get(name);
141: }
142:
143: /**
144: * Returns an array of all the JEnumConstant of this JEnum.
145: *
146: * @return An array of all the JEnumConstant of this JEnum.
147: */
148: public JEnumConstant[] getConstants() {
149: int size = _constants.size();
150: JEnumConstant[] farray = new JEnumConstant[size];
151: for (int i = 0; i < size; i++) {
152: farray[i] = (JEnumConstant) _constants.get(i);
153: }
154: return farray;
155: }
156:
157: //--------------------------------------------------------------------------
158:
159: /**
160: * {@inheritDoc}
161: */
162: public void print(final JSourceWriter jsw, final boolean classOnly) {
163: if (jsw == null) {
164: throw new IllegalArgumentException(
165: "argument 'jsw' should not be null.");
166: }
167:
168: //-- print class headers (comment header, package, imports) if desired
169: if (!classOnly) {
170: printClassHeaders(jsw);
171: }
172:
173: getJDocComment().print(jsw);
174:
175: printEnumDefinitionLine(jsw); // includes the opening '{'
176:
177: jsw.writeln();
178: jsw.indent();
179:
180: printEnumConstants(jsw);
181: printMemberVariables(jsw);
182: printStaticInitializers(jsw);
183: printConstructors(jsw);
184: printMethods(jsw);
185: printInnerClasses(jsw);
186:
187: jsw.unindent();
188: jsw.writeln('}');
189: jsw.flush();
190: }
191:
192: /**
193: * Writes to the JSourceWriter the line that defines this enum. This
194: * line includes the enum name, implements entries, and
195: * any modifiers such as "private".
196: *
197: * @param jsw The JSourceWriter to be used.
198: */
199: private void printEnumDefinitionLine(final JSourceWriter jsw) {
200: StringBuffer buffer = new StringBuffer();
201: buffer.setLength(0);
202:
203: //-- print annotations
204: getAnnotatedElementHelper().printAnnotations(jsw);
205:
206: JModifiers modifiers = getModifiers();
207: if (modifiers.isPrivate()) {
208: buffer.append("private ");
209: } else if (modifiers.isPublic()) {
210: buffer.append("public ");
211: }
212:
213: buffer.append("enum ");
214: buffer.append(getLocalName());
215: buffer.append(' ');
216: if (getInterfaceCount() > 0) {
217: boolean endl = false;
218: if ((getInterfaceCount() > 1)) {
219: jsw.writeln(buffer.toString());
220: buffer.setLength(0);
221: endl = true;
222: }
223: buffer.append("implements ");
224:
225: Enumeration enumeration = getInterfaces();
226: while (enumeration.hasMoreElements()) {
227: buffer.append(enumeration.nextElement());
228: if (enumeration.hasMoreElements()) {
229: buffer.append(", ");
230: }
231: }
232: if (endl) {
233: jsw.writeln(buffer.toString());
234: buffer.setLength(0);
235: } else {
236: buffer.append(' ');
237: }
238: }
239:
240: buffer.append('{');
241: jsw.writeln(buffer.toString());
242: buffer.setLength(0);
243: }
244:
245: /**
246: * Writes to the JSourceWriter all constants belonging to this enum.
247: *
248: * @param jsw The JSourceWriter to be used.
249: */
250: private void printEnumConstants(final JSourceWriter jsw) {
251: if (_constants.size() > 0) {
252: jsw.writeln();
253: jsw.writeln(" //------------------/");
254: jsw.writeln(" //- Enum Constants -/");
255: jsw.writeln("//------------------/");
256: jsw.writeln();
257: }
258: for (int i = 0; i < _constants.size(); i++) {
259: JEnumConstant jConstant = (JEnumConstant) _constants.get(i);
260: jConstant.print(jsw);
261: if (i < _constants.size() - 1) {
262: jsw.write(",");
263: } else {
264: jsw.write(";");
265: }
266: jsw.writeln();
267: }
268: }
269:
270: //--------------------------------------------------------------------------
271: }
|