View Javadoc
1   /**
2    *    Copyright 2006-2016 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
15   */
16  package org.mybatis.generator.api.dom.java;
17  
18  import java.util.ArrayList;
19  import java.util.HashSet;
20  import java.util.Iterator;
21  import java.util.List;
22  import java.util.Set;
23  
24  import org.mybatis.generator.api.dom.OutputUtilities;
25  
26  /**
27   * This class encapsulates the idea of an inner enum - it has methods that make
28   * it easy to generate inner enum.
29   * 
30   * @author Jeff Butler
31   */
32  public class InnerEnum extends JavaElement {
33  
34      /** The fields. */
35      private List<Field> fields;
36  
37      /** The inner classes. */
38      private List<InnerClass> innerClasses;
39  
40      /** The inner enums. */
41      private List<InnerEnum> innerEnums;
42  
43      /** The type. */
44      private FullyQualifiedJavaType type;
45  
46      /** The super interface types. */
47      private Set<FullyQualifiedJavaType> superInterfaceTypes;
48  
49      /** The methods. */
50      private List<Method> methods;
51  
52      /** The enum constants. */
53      private List<String> enumConstants;
54  
55      /**
56       * Instantiates a new inner enum.
57       *
58       * @param type
59       *            the type
60       */
61      public InnerEnum(FullyQualifiedJavaType type) {
62          super();
63          this.type = type;
64          fields = new ArrayList<Field>();
65          innerClasses = new ArrayList<InnerClass>();
66          innerEnums = new ArrayList<InnerEnum>();
67          superInterfaceTypes = new HashSet<FullyQualifiedJavaType>();
68          methods = new ArrayList<Method>();
69          enumConstants = new ArrayList<String>();
70      }
71  
72      /**
73       * Gets the fields.
74       *
75       * @return Returns the fields.
76       */
77      public List<Field> getFields() {
78          return fields;
79      }
80  
81      /**
82       * Adds the field.
83       *
84       * @param field
85       *            the field
86       */
87      public void addField(Field field) {
88          fields.add(field);
89      }
90  
91      /**
92       * Gets the inner classes.
93       *
94       * @return Returns the innerClasses.
95       */
96      public List<InnerClass> getInnerClasses() {
97          return innerClasses;
98      }
99  
100     /**
101      * Adds the inner class.
102      *
103      * @param innerClass
104      *            the inner class
105      */
106     public void addInnerClass(InnerClass innerClass) {
107         innerClasses.add(innerClass);
108     }
109 
110     /**
111      * Gets the inner enums.
112      *
113      * @return the inner enums
114      */
115     public List<InnerEnum> getInnerEnums() {
116         return innerEnums;
117     }
118 
119     /**
120      * Adds the inner enum.
121      *
122      * @param innerEnum
123      *            the inner enum
124      */
125     public void addInnerEnum(InnerEnum innerEnum) {
126         innerEnums.add(innerEnum);
127     }
128 
129     /**
130      * Gets the enum constants.
131      *
132      * @return the enum constants
133      */
134     public List<String> getEnumConstants() {
135         return enumConstants;
136     }
137 
138     /**
139      * Adds the enum constant.
140      *
141      * @param enumConstant
142      *            the enum constant
143      */
144     public void addEnumConstant(String enumConstant) {
145         enumConstants.add(enumConstant);
146     }
147 
148     /**
149      * Gets the formatted content.
150      *
151      * @param indentLevel
152      *            the indent level
153      * @param compilationUnit the compilation unit
154      * @return the formatted content
155      */
156     public String getFormattedContent(int indentLevel, CompilationUnit compilationUnit) {
157         StringBuilder sb = new StringBuilder();
158 
159         addFormattedJavadoc(sb, indentLevel);
160         addFormattedAnnotations(sb, indentLevel);
161 
162         OutputUtilities.javaIndent(sb, indentLevel);
163         if (getVisibility() == JavaVisibility.PUBLIC) {
164             sb.append(getVisibility().getValue());
165         }
166 
167         sb.append("enum "); //$NON-NLS-1$
168         sb.append(getType().getShortName());
169 
170         if (superInterfaceTypes.size() > 0) {
171             sb.append(" implements "); //$NON-NLS-1$
172 
173             boolean comma = false;
174             for (FullyQualifiedJavaType fqjt : superInterfaceTypes) {
175                 if (comma) {
176                     sb.append(", "); //$NON-NLS-1$
177                 } else {
178                     comma = true;
179                 }
180 
181                 sb.append(JavaDomUtils.calculateTypeName(compilationUnit, fqjt));
182             }
183         }
184 
185         sb.append(" {"); //$NON-NLS-1$
186         indentLevel++;
187 
188         Iterator<String> strIter = enumConstants.iterator();
189         while (strIter.hasNext()) {
190             OutputUtilities.newLine(sb);
191             OutputUtilities.javaIndent(sb, indentLevel);
192             String enumConstant = strIter.next();
193             sb.append(enumConstant);
194 
195             if (strIter.hasNext()) {
196                 sb.append(',');
197             } else {
198                 sb.append(';');
199             }
200         }
201 
202         if (fields.size() > 0) {
203             OutputUtilities.newLine(sb);
204         }
205 
206         Iterator<Field> fldIter = fields.iterator();
207         while (fldIter.hasNext()) {
208             OutputUtilities.newLine(sb);
209             Field field = fldIter.next();
210             sb.append(field.getFormattedContent(indentLevel, compilationUnit));
211             if (fldIter.hasNext()) {
212                 OutputUtilities.newLine(sb);
213             }
214         }
215 
216         if (methods.size() > 0) {
217             OutputUtilities.newLine(sb);
218         }
219 
220         Iterator<Method> mtdIter = methods.iterator();
221         while (mtdIter.hasNext()) {
222             OutputUtilities.newLine(sb);
223             Method method = mtdIter.next();
224             sb.append(method.getFormattedContent(indentLevel, false, compilationUnit));
225             if (mtdIter.hasNext()) {
226                 OutputUtilities.newLine(sb);
227             }
228         }
229 
230         if (innerClasses.size() > 0) {
231             OutputUtilities.newLine(sb);
232         }
233 
234         Iterator<InnerClass> icIter = innerClasses.iterator();
235         while (icIter.hasNext()) {
236             OutputUtilities.newLine(sb);
237             InnerClass innerClass = icIter.next();
238             sb.append(innerClass.getFormattedContent(indentLevel, compilationUnit));
239             if (icIter.hasNext()) {
240                 OutputUtilities.newLine(sb);
241             }
242         }
243 
244         if (innerEnums.size() > 0) {
245             OutputUtilities.newLine(sb);
246         }
247 
248         Iterator<InnerEnum> ieIter = innerEnums.iterator();
249         while (ieIter.hasNext()) {
250             OutputUtilities.newLine(sb);
251             InnerEnum innerEnum = ieIter.next();
252             sb.append(innerEnum.getFormattedContent(indentLevel, compilationUnit));
253             if (ieIter.hasNext()) {
254                 OutputUtilities.newLine(sb);
255             }
256         }
257 
258         indentLevel--;
259         OutputUtilities.newLine(sb);
260         OutputUtilities.javaIndent(sb, indentLevel);
261         sb.append('}');
262 
263         return sb.toString();
264     }
265 
266     /**
267      * Gets the super interface types.
268      *
269      * @return Returns the superInterfaces.
270      */
271     public Set<FullyQualifiedJavaType> getSuperInterfaceTypes() {
272         return superInterfaceTypes;
273     }
274 
275     /**
276      * Adds the super interface.
277      *
278      * @param superInterface
279      *            the super interface
280      */
281     public void addSuperInterface(FullyQualifiedJavaType superInterface) {
282         superInterfaceTypes.add(superInterface);
283     }
284 
285     /**
286      * Gets the methods.
287      *
288      * @return Returns the methods.
289      */
290     public List<Method> getMethods() {
291         return methods;
292     }
293 
294     /**
295      * Adds the method.
296      *
297      * @param method
298      *            the method
299      */
300     public void addMethod(Method method) {
301         methods.add(method);
302     }
303 
304     /**
305      * Gets the type.
306      *
307      * @return Returns the type.
308      */
309     public FullyQualifiedJavaType getType() {
310         return type;
311     }
312 }