-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnumDecl.java
More file actions
108 lines (90 loc) · 3.44 KB
/
Copy pathEnumDecl.java
File metadata and controls
108 lines (90 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package jtree.nodes;
import static jtree.util.Utils.*;
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NonNull;
import lombok.Setter;
@EqualsAndHashCode
@Getter @Setter
public class EnumDecl extends TypeDecl {
protected @NonNull List<GenericType> interfaces;
protected @NonNull List<EnumField> constants;
public EnumDecl(Name name, List<EnumField> constants, List<? extends Member> members) {
this(name, emptyList(), constants, members);
}
public EnumDecl(Name name, List<GenericType> interfaces, List<EnumField> constants, List<? extends Member> members) {
this(name, interfaces, constants, members, emptyList(), emptyList(), Optional.empty());
}
public EnumDecl(Name name, List<EnumField> constants, List<? extends Member> members, List<Modifier> modifiers, List<Annotation> annotations, Optional<String> docComment) {
this(name, emptyList(), constants, members, modifiers, annotations, docComment);
}
public EnumDecl(Name name, List<GenericType> interfaces,
List<EnumField> constants, List<? extends Member> members, List<Modifier> modifiers, List<Annotation> annotations, Optional<String> docComment) {
super(name, emptyList(), members, modifiers, annotations, docComment);
setInterfaces(interfaces);
setConstants(constants);
}
@Override
public EnumDecl clone() {
return new EnumDecl(getName(), clone(getInterfaces()), clone(getConstants()), clone(getMembers()), clone(getModifiers()), clone(getAnnotations()), getDocComment());
}
@Override
public String toCode() {
var interfaces = getInterfaces();
var constants = getConstants();
var members = getMembers();
var result = docString() + annotationString() + modifierString() + "enum " + getName()
+ (interfaces.isEmpty()? "" : " implements " + joinNodes(", ", interfaces))
+ " {";
if(!constants.isEmpty() || !members.isEmpty()) {
if(!constants.isEmpty()) {
result += "\n" + join("", constants, constant -> constant.toCode().indent(4));
}
if(!members.isEmpty()) {
if(!constants.isEmpty() && result.endsWith("\n")) {
result = result.substring(0, result.length()-1);
} else {
result += "\n ";
}
result += ";\n\n" + join("", members, member -> member.toCode().indent(4));
}
}
return result + "}";
}
public void setInterfaces(@NonNull List<GenericType> interfaces) {
this.interfaces = newList(interfaces);
}
public final void setInterfaces(GenericType... interfaces) {
setInterfaces(List.of(interfaces));
}
public void setConstants(@NonNull List<EnumField> constants) {
this.constants = newList(constants);
}
public final void setConstants(EnumField... constants) {
setConstants(List.of(constants));
}
@Override
public void setTypeParameters(@NonNull List<TypeParameter> typeParameters) {
if(!typeParameters.isEmpty()) {
throw new IllegalArgumentException("Enums cannot have type parameters");
}
}
@Override
public List<TypeParameter> getTypeParameters() {
return emptyList();
}
@Override
public <N extends INode> void accept(TreeVisitor visitor, Node parent, Consumer<N> replacer) {
if(visitor.visitEnumDecl(this, parent, cast(replacer))) {
getName().accept(visitor, this, this::setName);
visitList(visitor, getInterfaces());
visitList(visitor, getConstants());
visitList(visitor, getMembers());
visitList(visitor, getModifiers());
visitList(visitor, getAnnotations());
}
}
}