View Javadoc
1   /**
2    *    Copyright 2006-2015 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   * @author Jeff Butler
33   */
34  public class Interface extends JavaElement implements CompilationUnit {
35      private Set<FullyQualifiedJavaType> importedTypes;
36      
37      private Set<String> staticImports;
38  
39      private FullyQualifiedJavaType type;
40  
41      private Set<FullyQualifiedJavaType> superInterfaceTypes;
42  
43      private List<Method> methods;
44  
45      private List<String> fileCommentLines;
46  
47      /**
48       *  
49       */
50      public Interface(FullyQualifiedJavaType type) {
51          super();
52          this.type = type;
53          superInterfaceTypes = new LinkedHashSet<FullyQualifiedJavaType>();
54          methods = new ArrayList<Method>();
55          importedTypes = new TreeSet<FullyQualifiedJavaType>();
56          fileCommentLines = new ArrayList<String>();
57          staticImports = new TreeSet<String>();
58      }
59  
60      public Interface(String type) {
61          this(new FullyQualifiedJavaType(type));
62      }
63  
64      public Set<FullyQualifiedJavaType> getImportedTypes() {
65          return Collections.unmodifiableSet(importedTypes);
66      }
67  
68      public void addImportedType(FullyQualifiedJavaType importedType) {
69          if (importedType.isExplicitlyImported()
70                  && !importedType.getPackageName().equals(type.getPackageName())) {
71              importedTypes.add(importedType);
72          }
73      }
74  
75      public String getFormattedContent() {
76          StringBuilder sb = new StringBuilder();
77  
78          for (String commentLine : fileCommentLines) {
79              sb.append(commentLine);
80              newLine(sb);
81          }
82  
83          if (stringHasValue(getType().getPackageName())) {
84              sb.append("package "); //$NON-NLS-1$
85              sb.append(getType().getPackageName());
86              sb.append(';');
87              newLine(sb);
88              newLine(sb);
89          }
90  
91          for (String staticImport : staticImports) {
92              sb.append("import static "); //$NON-NLS-1$
93              sb.append(staticImport);
94              sb.append(';');
95              newLine(sb);
96          }
97          
98          if (staticImports.size() > 0) {
99              newLine(sb);
100         }
101         
102         Set<String> importStrings = calculateImports(importedTypes);
103         for (String importString : importStrings) {
104             sb.append(importString);
105             newLine(sb);
106         }
107 
108         if (importStrings.size() > 0) {
109             newLine(sb);
110         }
111 
112         int indentLevel = 0;
113 
114         addFormattedJavadoc(sb, indentLevel);
115         addFormattedAnnotations(sb, indentLevel);
116 
117         sb.append(getVisibility().getValue());
118 
119         if (isStatic()) {
120             sb.append("static "); //$NON-NLS-1$
121         }
122 
123         if (isFinal()) {
124             sb.append("final "); //$NON-NLS-1$
125         }
126 
127         sb.append("interface "); //$NON-NLS-1$
128         sb.append(getType().getShortName());
129 
130         if (getSuperInterfaceTypes().size() > 0) {
131             sb.append(" extends "); //$NON-NLS-1$
132 
133             boolean comma = false;
134             for (FullyQualifiedJavaType fqjt : getSuperInterfaceTypes()) {
135                 if (comma) {
136                     sb.append(", "); //$NON-NLS-1$
137                 } else {
138                     comma = true;
139                 }
140 
141                 sb.append(fqjt.getShortName());
142             }
143         }
144 
145         sb.append(" {"); //$NON-NLS-1$
146         indentLevel++;
147 
148         Iterator<Method> mtdIter = getMethods().iterator();
149         while (mtdIter.hasNext()) {
150             newLine(sb);
151             Method method = mtdIter.next();
152             sb.append(method.getFormattedContent(indentLevel, true));
153             if (mtdIter.hasNext()) {
154                 newLine(sb);
155             }
156         }
157 
158         indentLevel--;
159         newLine(sb);
160         javaIndent(sb, indentLevel);
161         sb.append('}');
162 
163         return sb.toString();
164     }
165 
166     public void addSuperInterface(FullyQualifiedJavaType superInterface) {
167         superInterfaceTypes.add(superInterface);
168     }
169 
170     /**
171      * @return Returns the methods.
172      */
173     public List<Method> getMethods() {
174         return methods;
175     }
176 
177     public void addMethod(Method method) {
178         methods.add(method);
179     }
180 
181     /**
182      * @return Returns the type.
183      */
184     public FullyQualifiedJavaType getType() {
185         return type;
186     }
187 
188     public FullyQualifiedJavaType getSuperClass() {
189         // interfaces do not have superclasses
190         return null;
191     }
192 
193     public Set<FullyQualifiedJavaType> getSuperInterfaceTypes() {
194         return superInterfaceTypes;
195     }
196 
197     public boolean isJavaInterface() {
198         return true;
199     }
200 
201     public boolean isJavaEnumeration() {
202         return false;
203     }
204 
205     public void addFileCommentLine(String commentLine) {
206         fileCommentLines.add(commentLine);
207     }
208 
209     public List<String> getFileCommentLines() {
210         return fileCommentLines;
211     }
212 
213     public void addImportedTypes(Set<FullyQualifiedJavaType> importedTypes) {
214         this.importedTypes.addAll(importedTypes);
215     }
216 
217     public Set<String> getStaticImports() {
218         return staticImports;
219     }
220 
221     public void addStaticImport(String staticImport) {
222         staticImports.add(staticImport);
223     }
224 
225     public void addStaticImports(Set<String> staticImports) {
226         this.staticImports.addAll(staticImports);
227     }
228 }