View Javadoc
1   /*
2    *    Copyright 2006-2023 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    *       https://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 java.util.ArrayList;
19  import java.util.List;
20  import java.util.Set;
21  import java.util.TreeSet;
22  
23  public class TopLevelClass extends InnerClass implements CompilationUnit {
24  
25      private final Set<FullyQualifiedJavaType> importedTypes = new TreeSet<>();
26  
27      private final Set<String> staticImports = new TreeSet<>();
28  
29      private final List<String> fileCommentLines = new ArrayList<>();
30  
31      public TopLevelClass(FullyQualifiedJavaType type) {
32          super(type);
33      }
34  
35      public TopLevelClass(String typeName) {
36          this(new FullyQualifiedJavaType(typeName));
37      }
38  
39      @Override
40      public Set<FullyQualifiedJavaType> getImportedTypes() {
41          return importedTypes;
42      }
43  
44      public void addImportedType(String importedType) {
45          addImportedType(new FullyQualifiedJavaType(importedType));
46      }
47  
48      @Override
49      public void addImportedType(FullyQualifiedJavaType importedType) {
50          if (importedType != null
51                  && importedType.isExplicitlyImported()
52                  && !importedType.getPackageName().equals(
53                          getType().getPackageName())
54                  && !importedType.getShortName().equals(getType().getShortName())) {
55              importedTypes.add(importedType);
56          }
57      }
58  
59      @Override
60      public void addFileCommentLine(String commentLine) {
61          fileCommentLines.add(commentLine);
62      }
63  
64      @Override
65      public List<String> getFileCommentLines() {
66          return fileCommentLines;
67      }
68  
69      @Override
70      public void addImportedTypes(Set<FullyQualifiedJavaType> importedTypes) {
71          this.importedTypes.addAll(importedTypes);
72      }
73  
74      @Override
75      public Set<String> getStaticImports() {
76          return staticImports;
77      }
78  
79      @Override
80      public void addStaticImport(String staticImport) {
81          staticImports.add(staticImport);
82      }
83  
84      @Override
85      public void addStaticImports(Set<String> staticImports) {
86          this.staticImports.addAll(staticImports);
87      }
88  
89      @Override
90      public <R> R accept(CompilationUnitVisitor<R> visitor) {
91          return visitor.visit(this);
92      }
93  }