1
2
3
4
5
6
7
8
9
10
11
12
13
14
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"),
33 INTERFACE("interface"),
34 OBJECT("object"),
35 COMPANION_OBJECT("companion object");
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 }