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.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   * The Class Interface.
33   *
34   * @author Jeff Butler
35   */
36  public class Interface extends JavaElement implements CompilationUnit {
37      
38      /** The imported types. */
39      private Set<FullyQualifiedJavaType> importedTypes;
40      
41      /** The static imports. */
42      private Set<String> staticImports;
43  
44      /** The type. */
45      private FullyQualifiedJavaType type;
46  
47      /** The super interface types. */
48      private Set<FullyQualifiedJavaType> superInterfaceTypes;
49  
50      /** The methods. */
51      private List<Method> methods;
52  
53      /** The file comment lines. */
54      private List<String> fileCommentLines;
55  
56      /**
57       * Instantiates a new interface.
58       *
59       * @param type
60       *            the type
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       * Instantiates a new interface.
74       *
75       * @param type
76       *            the type
77       */
78      public Interface(String type) {
79          this(new FullyQualifiedJavaType(type));
80      }
81  
82      /* (non-Javadoc)
83       * @see org.mybatis.generator.api.dom.java.CompilationUnit#getImportedTypes()
84       */
85      public Set<FullyQualifiedJavaType> getImportedTypes() {
86          return Collections.unmodifiableSet(importedTypes);
87      }
88  
89      /* (non-Javadoc)
90       * @see org.mybatis.generator.api.dom.java.CompilationUnit#addImportedType(org.mybatis.generator.api.dom.java.FullyQualifiedJavaType)
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      /* (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 commentLine : fileCommentLines) {
106             sb.append(commentLine);
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         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 "); //$NON-NLS-1$
148         }
149 
150         if (isFinal()) {
151             sb.append("final "); //$NON-NLS-1$
152         }
153 
154         sb.append("interface "); //$NON-NLS-1$
155         sb.append(getType().getShortName());
156 
157         if (getSuperInterfaceTypes().size() > 0) {
158             sb.append(" extends "); //$NON-NLS-1$
159 
160             boolean comma = false;
161             for (FullyQualifiedJavaType fqjt : getSuperInterfaceTypes()) {
162                 if (comma) {
163                     sb.append(", "); //$NON-NLS-1$
164                 } else {
165                     comma = true;
166                 }
167 
168                 sb.append(JavaDomUtils.calculateTypeName(this, fqjt));
169             }
170         }
171 
172         sb.append(" {"); //$NON-NLS-1$
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      * Adds the super interface.
195      *
196      * @param superInterface
197      *            the super interface
198      */
199     public void addSuperInterface(FullyQualifiedJavaType superInterface) {
200         superInterfaceTypes.add(superInterface);
201     }
202 
203     /**
204      * Gets the methods.
205      *
206      * @return Returns the methods.
207      */
208     public List<Method> getMethods() {
209         return methods;
210     }
211 
212     /**
213      * Adds the method.
214      *
215      * @param method
216      *            the method
217      */
218     public void addMethod(Method method) {
219         methods.add(method);
220     }
221 
222     /**
223      * Gets the type.
224      *
225      * @return Returns the type.
226      */
227     public FullyQualifiedJavaType getType() {
228         return type;
229     }
230 
231     /* (non-Javadoc)
232      * @see org.mybatis.generator.api.dom.java.CompilationUnit#getSuperClass()
233      */
234     public FullyQualifiedJavaType getSuperClass() {
235         // interfaces do not have superclasses
236         return null;
237     }
238 
239     /* (non-Javadoc)
240      * @see org.mybatis.generator.api.dom.java.CompilationUnit#getSuperInterfaceTypes()
241      */
242     public Set<FullyQualifiedJavaType> getSuperInterfaceTypes() {
243         return superInterfaceTypes;
244     }
245 
246     /* (non-Javadoc)
247      * @see org.mybatis.generator.api.dom.java.CompilationUnit#isJavaInterface()
248      */
249     public boolean isJavaInterface() {
250         return true;
251     }
252 
253     /* (non-Javadoc)
254      * @see org.mybatis.generator.api.dom.java.CompilationUnit#isJavaEnumeration()
255      */
256     public boolean isJavaEnumeration() {
257         return false;
258     }
259 
260     /* (non-Javadoc)
261      * @see org.mybatis.generator.api.dom.java.CompilationUnit#addFileCommentLine(java.lang.String)
262      */
263     public void addFileCommentLine(String commentLine) {
264         fileCommentLines.add(commentLine);
265     }
266 
267     /* (non-Javadoc)
268      * @see org.mybatis.generator.api.dom.java.CompilationUnit#getFileCommentLines()
269      */
270     public List<String> getFileCommentLines() {
271         return fileCommentLines;
272     }
273 
274     /* (non-Javadoc)
275      * @see org.mybatis.generator.api.dom.java.CompilationUnit#addImportedTypes(java.util.Set)
276      */
277     public void addImportedTypes(Set<FullyQualifiedJavaType> importedTypes) {
278         this.importedTypes.addAll(importedTypes);
279     }
280 
281     /* (non-Javadoc)
282      * @see org.mybatis.generator.api.dom.java.CompilationUnit#getStaticImports()
283      */
284     public Set<String> getStaticImports() {
285         return staticImports;
286     }
287 
288     /* (non-Javadoc)
289      * @see org.mybatis.generator.api.dom.java.CompilationUnit#addStaticImport(java.lang.String)
290      */
291     public void addStaticImport(String staticImport) {
292         staticImports.add(staticImport);
293     }
294 
295     /* (non-Javadoc)
296      * @see org.mybatis.generator.api.dom.java.CompilationUnit#addStaticImports(java.util.Set)
297      */
298     public void addStaticImports(Set<String> staticImports) {
299         this.staticImports.addAll(staticImports);
300     }
301 }