-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSwitch.java
More file actions
59 lines (46 loc) · 1.39 KB
/
Copy pathSwitch.java
File metadata and controls
59 lines (46 loc) · 1.39 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
package jtree.nodes;
import static jtree.util.Utils.*;
import java.util.List;
import java.util.function.Consumer;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.NonNull;
@EqualsAndHashCode
@Getter @Setter
public class Switch extends Node implements Expression, CompoundStmt {
protected @NonNull Expression expression;
protected @NonNull List<SwitchCase> cases;
public Switch(Expression expression, SwitchCase... cases) {
this(expression, List.of(cases));
}
public Switch(Expression expression, List<SwitchCase> cases) {
setExpression(expression);
setCases(cases);
}
@Override
public Precedence precedence() {
return Precedence.PRIMARY;
}
@Override
public Switch clone() {
return new Switch(getExpression().clone(), clone(getCases()));
}
@Override
public String toCode() {
return "switch(" + expression.toCode() + ") {\n" + join("", getCases(), aCase -> aCase.toCode().indent(4)) + "}";
}
public void setCases(@NonNull List<SwitchCase> cases) {
this.cases = newList(cases);
}
public final void setCases(SwitchCase... cases) {
setCases(List.of(cases));
}
@Override
public <N extends INode> void accept(TreeVisitor visitor, Node parent, Consumer<N> replacer) {
if(visitor.visitSwitch(this, parent, cast(replacer))) {
getExpression().accept(visitor, this, this::setExpression);
visitList(visitor, getCases());
}
}
}