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.kotlin;
17  
18  import java.util.ArrayList;
19  import java.util.HashSet;
20  import java.util.List;
21  import java.util.Objects;
22  import java.util.Set;
23  
24  public class KotlinType extends KotlinNamedItemContainer {
25  
26      public static final String DEFAULT_COMPANION_OBJECT_NAME = "companion";
27      private final List<KotlinProperty> constructorProperties = new ArrayList<>();
28      private final Type type;
29      private final Set<String> superTypes = new HashSet<>();
30  
31      public enum Type {
32          CLASS("class"), //$NON-NLS-1$
33          INTERFACE("interface"), //$NON-NLS-1$
34          OBJECT("object"), //$NON-NLS-1$
35          COMPANION_OBJECT("companion object"); //$NON-NLS-1$
36  
37          private final String value;
38  
39          Type(String value) {
40              this.value = value;
41          }
42  
43          public String getValue() {
44              return value;
45          }
46      }
47  
48      private KotlinType(Builder builder) {
49          super(builder);
50          this.type = Objects.requireNonNull(builder.type);
51          constructorProperties.addAll(builder.constructorProperties);
52          superTypes.addAll(builder.superTypes);
53      }
54  
55      public List<KotlinProperty> getConstructorProperties() {
56          return constructorProperties;
57      }
58  
59      public Type getType() {
60          return type;
61      }
62  
63      public Set<String> getSuperTypes() {
64          return superTypes;
65      }
66  
67      public void addConstructorProperty(KotlinProperty property) {
68          constructorProperties.add(property);
69      }
70  
71      public void addSuperType(String superType) {
72          superTypes.add(superType);
73      }
74  
75      @Override
76      public <R> R accept(KotlinNamedItemVisitor<R> visitor) {
77          return visitor.visit(this);
78      }
79  
80      public static Builder newClass(String name) {
81          return new Builder(Type.CLASS, name);
82      }
83  
84      public static Builder newInterface(String name) {
85          return new Builder(Type.INTERFACE, name);
86      }
87  
88      public static Builder newObject(String name) {
89          return new Builder(Type.OBJECT, name);
90      }
91  
92      public static Builder newCompanionObject() {
93          return new Builder(Type.COMPANION_OBJECT, DEFAULT_COMPANION_OBJECT_NAME);
94      }
95  
96      public static Builder newCompanionObject(String name) {
97          return new Builder(Type.COMPANION_OBJECT, name);
98      }
99  
100     public static class Builder extends NamedItemContainerBuilder<Builder> {
101         private final Type type;
102         private final List<KotlinProperty> constructorProperties = new ArrayList<>();
103         private final List<String> superTypes = new ArrayList<>();
104 
105         private Builder(Type type, String name) {
106             super(name);
107             this.type = type;
108         }
109 
110         public Builder withConstructorProperty(KotlinProperty kotlinProperty) {
111             constructorProperties.add(kotlinProperty);
112             return this;
113         }
114 
115         public Builder withSuperType(String superType) {
116             superTypes.add(superType);
117             return this;
118         }
119 
120         public Builder getThis() {
121             return this;
122         }
123 
124         public KotlinType build() {
125             return new KotlinType(this);
126         }
127     }
128 }