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.javaIndent;
20 import static org.mybatis.generator.api.dom.OutputUtilities.newLine;
21 import static org.mybatis.generator.internal.util.StringUtility.stringHasValue;
22
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.Iterator;
26 import java.util.LinkedHashSet;
27 import java.util.List;
28 import java.util.Set;
29 import java.util.TreeSet;
30
31
32
33
34
35
36 public class Interface extends JavaElement implements CompilationUnit {
37
38
39 private Set<FullyQualifiedJavaType> importedTypes;
40
41
42 private Set<String> staticImports;
43
44
45 private FullyQualifiedJavaType type;
46
47
48 private Set<FullyQualifiedJavaType> superInterfaceTypes;
49
50
51 private List<Method> methods;
52
53
54 private List<String> fileCommentLines;
55
56
57
58
59
60
61
62 public Interface(FullyQualifiedJavaType type) {
63 super();
64 this.type = type;
65 superInterfaceTypes = new LinkedHashSet<FullyQualifiedJavaType>();
66 methods = new ArrayList<Method>();
67 importedTypes = new TreeSet<FullyQualifiedJavaType>();
68 fileCommentLines = new ArrayList<String>();
69 staticImports = new TreeSet<String>();
70 }
71
72
73
74
75
76
77
78 public Interface(String type) {
79 this(new FullyQualifiedJavaType(type));
80 }
81
82
83
84
85 public Set<FullyQualifiedJavaType> getImportedTypes() {
86 return Collections.unmodifiableSet(importedTypes);
87 }
88
89
90
91
92 public void addImportedType(FullyQualifiedJavaType importedType) {
93 if (importedType.isExplicitlyImported()
94 && !importedType.getPackageName().equals(type.getPackageName())) {
95 importedTypes.add(importedType);
96 }
97 }
98
99
100
101
102 public String getFormattedContent() {
103 StringBuilder sb = new StringBuilder();
104
105 for (String commentLine : fileCommentLines) {
106 sb.append(commentLine);
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 int indentLevel = 0;
140
141 addFormattedJavadoc(sb, indentLevel);
142 addFormattedAnnotations(sb, indentLevel);
143
144 sb.append(getVisibility().getValue());
145
146 if (isStatic()) {
147 sb.append("static ");
148 }
149
150 if (isFinal()) {
151 sb.append("final ");
152 }
153
154 sb.append("interface ");
155 sb.append(getType().getShortName());
156
157 if (getSuperInterfaceTypes().size() > 0) {
158 sb.append(" extends ");
159
160 boolean comma = false;
161 for (FullyQualifiedJavaType fqjt : getSuperInterfaceTypes()) {
162 if (comma) {
163 sb.append(", ");
164 } else {
165 comma = true;
166 }
167
168 sb.append(JavaDomUtils.calculateTypeName(this, fqjt));
169 }
170 }
171
172 sb.append(" {");
173 indentLevel++;
174
175 Iterator<Method> mtdIter = getMethods().iterator();
176 while (mtdIter.hasNext()) {
177 newLine(sb);
178 Method method = mtdIter.next();
179 sb.append(method.getFormattedContent(indentLevel, true, this));
180 if (mtdIter.hasNext()) {
181 newLine(sb);
182 }
183 }
184
185 indentLevel--;
186 newLine(sb);
187 javaIndent(sb, indentLevel);
188 sb.append('}');
189
190 return sb.toString();
191 }
192
193
194
195
196
197
198
199 public void addSuperInterface(FullyQualifiedJavaType superInterface) {
200 superInterfaceTypes.add(superInterface);
201 }
202
203
204
205
206
207
208 public List<Method> getMethods() {
209 return methods;
210 }
211
212
213
214
215
216
217
218 public void addMethod(Method method) {
219 methods.add(method);
220 }
221
222
223
224
225
226
227 public FullyQualifiedJavaType getType() {
228 return type;
229 }
230
231
232
233
234 public FullyQualifiedJavaType getSuperClass() {
235
236 return null;
237 }
238
239
240
241
242 public Set<FullyQualifiedJavaType> getSuperInterfaceTypes() {
243 return superInterfaceTypes;
244 }
245
246
247
248
249 public boolean isJavaInterface() {
250 return true;
251 }
252
253
254
255
256 public boolean isJavaEnumeration() {
257 return false;
258 }
259
260
261
262
263 public void addFileCommentLine(String commentLine) {
264 fileCommentLines.add(commentLine);
265 }
266
267
268
269
270 public List<String> getFileCommentLines() {
271 return fileCommentLines;
272 }
273
274
275
276
277 public void addImportedTypes(Set<FullyQualifiedJavaType> importedTypes) {
278 this.importedTypes.addAll(importedTypes);
279 }
280
281
282
283
284 public Set<String> getStaticImports() {
285 return staticImports;
286 }
287
288
289
290
291 public void addStaticImport(String staticImport) {
292 staticImports.add(staticImport);
293 }
294
295
296
297
298 public void addStaticImports(Set<String> staticImports) {
299 this.staticImports.addAll(staticImports);
300 }
301 }