View Javadoc
1   /**
2    * Redistribution and use of this software and associated documentation ("Software"), with or
3    * without modification, are permitted provided that the following conditions are met:
4    *
5    * 1. Redistributions of source code must retain copyright statements and notices. Redistributions
6    * must also contain a copy of this document.
7    *
8    * 2. Redistributions in binary form must reproduce the above copyright notice, this list of
9    * conditions and the following disclaimer in the documentation and/or other materials provided with
10   * the distribution.
11   *
12   * 3. The name "Exolab" must not be used to endorse or promote products derived from this Software
13   * without prior written permission of Intalio, Inc. For written permission, please contact
14   * info@exolab.org.
15   *
16   * 4. Products derived from this Software may not be called "Exolab" nor may "Exolab" appear in
17   * their names without prior written permission of Intalio, Inc. Exolab is a registered trademark of
18   * Intalio, Inc.
19   *
20   * 5. Due credit should be given to the Exolab Project (http://www.exolab.org/).
21   *
22   * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR
23   * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
24   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTALIO, INC. OR ITS
25   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28   * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
29   * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30   *
31   * Copyright 1999-2002 (C) Intalio, Inc. All Rights Reserved.
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   * Describes the definition of a enum type class.
41   *
42   * @author <a href="mailto:andrew DOT fawcett AT coda DOT com">Andrew Fawcett</a>
43   * @version $Revision$ $Date: 2006-04-25 16:09:10 -0600 (Tue, 25 Apr 2006) $
44   */
45  public final class JEnum extends JClass {
46  
47    /** The list of elements of this JEnumConstant. */
48    private Map<String, JEnumConstant> _enumConstants = new LinkedHashMap<String, JEnumConstant>();
49  
50    /**
51     * Construct JEnum with given name.
52     * 
53     * @param name The name for this JEnum.
54     */
55    public JEnum(final String name) {
56      super(name);
57  
58      // -- initialize default Java doc
59      getJDocComment().setComment("Enumeration " + getLocalName() + ".");
60    }
61  
62    /**
63     * Override to only allow private constructors.
64     * 
65     * @param constructor The constructor that should be added.
66     */
67    public void addConstructor(final JConstructor constructor) {
68      if (constructor.getModifiers().isPrivate()) {
69        super.addConstructor(constructor);
70      }
71    }
72  
73    // --------------------------------------------------------------------------
74  
75    /**
76     * Adds the given {@link JMember} to this {@link JEnum}.
77     *
78     * @param jMember The {@link JMember} to add.
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     * Adds the given {@link JEnumConstant} to this {@link JEnum}.
90     *
91     * @param jEnumConstant The constant to add.
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    * Returns the member with the given name, or null if no member was found with the given name.
108    *
109    * @param name The name of the member to return.
110    * @return The member with the given name, or null if no member was found with the given name.
111    */
112   public JEnumConstant getEnumConstant(final String name) {
113     return _enumConstants.get(name);
114   }
115 
116   /**
117    * Returns an array of all the JEnumConstant of this JEnum.
118    *
119    * @return An array of all the JEnumConstant of this JEnum.
120    */
121   public JEnumConstant[] getEnumConstants() {
122     return _enumConstants.values().toArray(new JEnumConstant[_enumConstants.size()]);
123   }
124 
125   /**
126    * Returns the number of enum constants.
127    * 
128    * @return The number of enum constants.
129    */
130   public int getEnumConstantCount() {
131     return this._enumConstants.size();
132   }
133 
134   /**
135    * {@inheritDoc}
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     // -- print class headers (comment header, package, imports) if desired
144     if (!classOnly) {
145       printClassHeaders(jsw);
146     }
147 
148     getJDocComment().print(jsw);
149 
150     printEnumDefinitionLine(jsw); // includes the opening '{'
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    * Writes to the JSourceWriter the line that defines this enum. This line includes the enum name,
169    * implements entries, and any modifiers such as "private".
170    * 
171    * @param jsw The JSourceWriter to be used.
172    */
173   private void printEnumDefinitionLine(final JSourceWriter jsw) {
174     StringBuilder buffer = new StringBuilder();
175 
176     // -- print annotations
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    * Writes to the JSourceWriter all constants belonging to this enum.
217    * 
218    * @param jsw The JSourceWriter to be used.
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 }