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 static org.mybatis.generator.api.dom.OutputUtilities.calculateImports;
19  import static org.mybatis.generator.api.dom.OutputUtilities.newLine;
20  import static org.mybatis.generator.internal.util.messages.Messages.getString;
21  
22  import java.util.ArrayList;
23  import java.util.Collections;
24  import java.util.List;
25  import java.util.Set;
26  import java.util.TreeSet;
27  
28  /**
29   * The Class TopLevelEnumeration.
30   *
31   * @author Jeff Butler
32   */
33  public class TopLevelEnumeration extends InnerEnum implements CompilationUnit {
34      
35      /** The imported types. */
36      private Set<FullyQualifiedJavaType> importedTypes;
37  
38      /** The static imports. */
39      private Set<String> staticImports;
40  
41      /** The file comment lines. */
42      private List<String> fileCommentLines;
43  
44      /**
45       * Instantiates a new top level enumeration.
46       *
47       * @param type
48       *            the type
49       */
50      public TopLevelEnumeration(FullyQualifiedJavaType type) {
51          super(type);
52          importedTypes = new TreeSet<FullyQualifiedJavaType>();
53          fileCommentLines = new ArrayList<String>();
54          staticImports = new TreeSet<String>();
55      }
56  
57      /* (non-Javadoc)
58       * @see org.mybatis.generator.api.dom.java.CompilationUnit#getFormattedContent()
59       */
60      public String getFormattedContent() {
61          StringBuilder sb = new StringBuilder();
62  
63          for (String fileCommentLine : fileCommentLines) {
64              sb.append(fileCommentLine);
65              newLine(sb);
66          }
67  
68          if (getType().getPackageName() != null
69                  && getType().getPackageName().length() > 0) {
70              sb.append("package "); //$NON-NLS-1$
71              sb.append(getType().getPackageName());
72              sb.append(';');
73              newLine(sb);
74              newLine(sb);
75          }
76  
77          for (String staticImport : staticImports) {
78              sb.append("import static "); //$NON-NLS-1$
79              sb.append(staticImport);
80              sb.append(';');
81              newLine(sb);
82          }
83          
84          if (staticImports.size() > 0) {
85              newLine(sb);
86          }
87          
88          Set<String> importStrings = calculateImports(importedTypes);
89          for (String importString : importStrings) {
90              sb.append(importString);
91              newLine(sb);
92          }
93  
94          if (importStrings.size() > 0) {
95              newLine(sb);
96          }
97  
98          sb.append(super.getFormattedContent(0, this));
99  
100         return sb.toString();
101     }
102 
103     /* (non-Javadoc)
104      * @see org.mybatis.generator.api.dom.java.CompilationUnit#getImportedTypes()
105      */
106     public Set<FullyQualifiedJavaType> getImportedTypes() {
107         return Collections.unmodifiableSet(importedTypes);
108     }
109 
110     /* (non-Javadoc)
111      * @see org.mybatis.generator.api.dom.java.CompilationUnit#getSuperClass()
112      */
113     public FullyQualifiedJavaType getSuperClass() {
114         throw new UnsupportedOperationException(getString("RuntimeError.11")); //$NON-NLS-1$
115     }
116 
117     /* (non-Javadoc)
118      * @see org.mybatis.generator.api.dom.java.CompilationUnit#isJavaInterface()
119      */
120     public boolean isJavaInterface() {
121         return false;
122     }
123 
124     /* (non-Javadoc)
125      * @see org.mybatis.generator.api.dom.java.CompilationUnit#isJavaEnumeration()
126      */
127     public boolean isJavaEnumeration() {
128         return true;
129     }
130 
131     /* (non-Javadoc)
132      * @see org.mybatis.generator.api.dom.java.CompilationUnit#addImportedType(org.mybatis.generator.api.dom.java.FullyQualifiedJavaType)
133      */
134     public void addImportedType(FullyQualifiedJavaType importedType) {
135         if (importedType.isExplicitlyImported()
136                 && !importedType.getPackageName().equals(
137                         getType().getPackageName())) {
138             importedTypes.add(importedType);
139         }
140     }
141 
142     /* (non-Javadoc)
143      * @see org.mybatis.generator.api.dom.java.CompilationUnit#addFileCommentLine(java.lang.String)
144      */
145     public void addFileCommentLine(String commentLine) {
146         fileCommentLines.add(commentLine);
147     }
148 
149     /* (non-Javadoc)
150      * @see org.mybatis.generator.api.dom.java.CompilationUnit#getFileCommentLines()
151      */
152     public List<String> getFileCommentLines() {
153         return fileCommentLines;
154     }
155 
156     /* (non-Javadoc)
157      * @see org.mybatis.generator.api.dom.java.CompilationUnit#addImportedTypes(java.util.Set)
158      */
159     public void addImportedTypes(Set<FullyQualifiedJavaType> importedTypes) {
160         this.importedTypes.addAll(importedTypes);
161     }
162 
163     /* (non-Javadoc)
164      * @see org.mybatis.generator.api.dom.java.CompilationUnit#getStaticImports()
165      */
166     public Set<String> getStaticImports() {
167         return staticImports;
168     }
169 
170     /* (non-Javadoc)
171      * @see org.mybatis.generator.api.dom.java.CompilationUnit#addStaticImport(java.lang.String)
172      */
173     public void addStaticImport(String staticImport) {
174         staticImports.add(staticImport);
175     }
176 
177     /* (non-Javadoc)
178      * @see org.mybatis.generator.api.dom.java.CompilationUnit#addStaticImports(java.util.Set)
179      */
180     public void addStaticImports(Set<String> staticImports) {
181         this.staticImports.addAll(staticImports);
182     }
183 }