1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
30
31
32
33 public class TopLevelClass extends InnerClass implements CompilationUnit {
34
35
36 private Set<FullyQualifiedJavaType> importedTypes;
37
38
39 private Set<String> staticImports;
40
41
42 private List<String> fileCommentLines;
43
44
45
46
47
48
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
59
60
61
62
63 public TopLevelClass(String typeName) {
64 this(new FullyQualifiedJavaType(typeName));
65 }
66
67
68
69
70
71
72 public Set<FullyQualifiedJavaType> getImportedTypes() {
73 return Collections.unmodifiableSet(importedTypes);
74 }
75
76
77
78
79
80
81
82 public void addImportedType(String importedType) {
83 addImportedType(new FullyQualifiedJavaType(importedType));
84 }
85
86
87
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
100
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 ");
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 ");
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
145
146
147 public boolean isJavaInterface() {
148 return false;
149 }
150
151
152
153
154 public boolean isJavaEnumeration() {
155 return false;
156 }
157
158
159
160
161 public void addFileCommentLine(String commentLine) {
162 fileCommentLines.add(commentLine);
163 }
164
165
166
167
168 public List<String> getFileCommentLines() {
169 return fileCommentLines;
170 }
171
172
173
174
175 public void addImportedTypes(Set<FullyQualifiedJavaType> importedTypes) {
176 this.importedTypes.addAll(importedTypes);
177 }
178
179
180
181
182 public Set<String> getStaticImports() {
183 return staticImports;
184 }
185
186
187
188
189 public void addStaticImport(String staticImport) {
190 staticImports.add(staticImport);
191 }
192
193
194
195
196 public void addStaticImports(Set<String> staticImports) {
197 this.staticImports.addAll(staticImports);
198 }
199 }