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.StringUtility.stringHasValue;
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 TopLevelClass.
30   *
31   * @author Jeff Butler
32   */
33  public class TopLevelClass extends InnerClass 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 class.
46       *
47       * @param type
48       *            the type
49       */
50      public TopLevelClass(FullyQualifiedJavaType type) {
51          super(type);
52          importedTypes = new TreeSet<FullyQualifiedJavaType>();
53          fileCommentLines = new ArrayList<String>();
54          staticImports = new TreeSet<String>();
55      }
56  
57      /**
58       * Instantiates a new top level class.
59       *
60       * @param typeName
61       *            the type name
62       */
63      public TopLevelClass(String typeName) {
64          this(new FullyQualifiedJavaType(typeName));
65      }
66  
67      /**
68       * Gets the imported types.
69       *
70       * @return Returns the importedTypes.
71       */
72      public Set<FullyQualifiedJavaType> getImportedTypes() {
73          return Collections.unmodifiableSet(importedTypes);
74      }
75  
76      /**
77       * Adds the imported type.
78       *
79       * @param importedType
80       *            the imported type
81       */
82      public void addImportedType(String importedType) {
83          addImportedType(new FullyQualifiedJavaType(importedType));
84      }
85      
86      /* (non-Javadoc)
87       * @see org.mybatis.generator.api.dom.java.CompilationUnit#addImportedType(org.mybatis.generator.api.dom.java.FullyQualifiedJavaType)
88       */
89      public void addImportedType(FullyQualifiedJavaType importedType) {
90          if (importedType != null
91                  && importedType.isExplicitlyImported()
92                  && !importedType.getPackageName().equals(
93                          getType().getPackageName())
94                  && !importedType.getShortName().equals(getType().getShortName())) {
95              importedTypes.add(importedType);
96          }
97      }
98  
99      /* (non-Javadoc)
100      * @see org.mybatis.generator.api.dom.java.CompilationUnit#getFormattedContent()
101      */
102     public String getFormattedContent() {
103         StringBuilder sb = new StringBuilder();
104 
105         for (String fileCommentLine : fileCommentLines) {
106             sb.append(fileCommentLine);
107             newLine(sb);
108         }
109 
110         if (stringHasValue(getType().getPackageName())) {
111             sb.append("package "); //$NON-NLS-1$
112             sb.append(getType().getPackageName());
113             sb.append(';');
114             newLine(sb);
115             newLine(sb);
116         }
117 
118         for (String staticImport : staticImports) {
119             sb.append("import static "); //$NON-NLS-1$
120             sb.append(staticImport);
121             sb.append(';');
122             newLine(sb);
123         }
124         
125         if (staticImports.size() > 0) {
126             newLine(sb);
127         }
128         
129         Set<String> importStrings = calculateImports(importedTypes);
130         for (String importString : importStrings) {
131             sb.append(importString);
132             newLine(sb);
133         }
134 
135         if (importStrings.size() > 0) {
136             newLine(sb);
137         }
138 
139         sb.append(super.getFormattedContent(0, this));
140 
141         return sb.toString();
142     }
143 
144     /* (non-Javadoc)
145      * @see org.mybatis.generator.api.dom.java.CompilationUnit#isJavaInterface()
146      */
147     public boolean isJavaInterface() {
148         return false;
149     }
150 
151     /* (non-Javadoc)
152      * @see org.mybatis.generator.api.dom.java.CompilationUnit#isJavaEnumeration()
153      */
154     public boolean isJavaEnumeration() {
155         return false;
156     }
157 
158     /* (non-Javadoc)
159      * @see org.mybatis.generator.api.dom.java.CompilationUnit#addFileCommentLine(java.lang.String)
160      */
161     public void addFileCommentLine(String commentLine) {
162         fileCommentLines.add(commentLine);
163     }
164 
165     /* (non-Javadoc)
166      * @see org.mybatis.generator.api.dom.java.CompilationUnit#getFileCommentLines()
167      */
168     public List<String> getFileCommentLines() {
169         return fileCommentLines;
170     }
171 
172     /* (non-Javadoc)
173      * @see org.mybatis.generator.api.dom.java.CompilationUnit#addImportedTypes(java.util.Set)
174      */
175     public void addImportedTypes(Set<FullyQualifiedJavaType> importedTypes) {
176         this.importedTypes.addAll(importedTypes);
177     }
178 
179     /* (non-Javadoc)
180      * @see org.mybatis.generator.api.dom.java.CompilationUnit#getStaticImports()
181      */
182     public Set<String> getStaticImports() {
183         return staticImports;
184     }
185 
186     /* (non-Javadoc)
187      * @see org.mybatis.generator.api.dom.java.CompilationUnit#addStaticImport(java.lang.String)
188      */
189     public void addStaticImport(String staticImport) {
190         staticImports.add(staticImport);
191     }
192 
193     /* (non-Javadoc)
194      * @see org.mybatis.generator.api.dom.java.CompilationUnit#addStaticImports(java.util.Set)
195      */
196     public void addStaticImports(Set<String> staticImports) {
197         this.staticImports.addAll(staticImports);
198     }
199 }