1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.mal_lang.langspec;
18
19 import static java.util.Objects.requireNonNull;
20
21 import jakarta.json.Json;
22 import jakarta.json.JsonObject;
23 import org.mal_lang.langspec.builders.VariableBuilder;
24 import org.mal_lang.langspec.step.StepExpression;
25
26
27
28
29
30
31 public final class Variable {
32 private final String name;
33 private final Asset asset;
34 private StepExpression stepExpression;
35
36 private Variable(String name, Asset asset) {
37 this.name = requireNonNull(name);
38 this.asset = requireNonNull(asset);
39 }
40
41
42
43
44
45
46
47 public String getName() {
48 return this.name;
49 }
50
51
52
53
54
55
56
57 public Asset getAsset() {
58 return this.asset;
59 }
60
61
62
63
64
65
66
67 public StepExpression getStepExpression() {
68 return this.stepExpression;
69 }
70
71 void setStepExpression(StepExpression stepExpression) {
72 this.stepExpression = requireNonNull(stepExpression);
73 }
74
75
76
77
78
79
80
81 public boolean hasSuperVariable() {
82 return this.asset.hasSuperAsset() && this.asset.getSuperAsset().hasVariable(this.name);
83 }
84
85
86
87
88
89
90
91
92
93 public Variable getSuperVariable() {
94 if (!this.hasSuperVariable()) {
95 throw new UnsupportedOperationException("Super variable not found");
96 }
97 return this.asset.getSuperAsset().getVariable(this.name);
98 }
99
100 JsonObject toJson() {
101 return Json.createObjectBuilder()
102 .add("name", this.name)
103 .add("stepExpression", this.stepExpression.toJson())
104 .build();
105 }
106
107 static Variable fromBuilder(VariableBuilder builder, Asset asset) {
108 requireNonNull(builder);
109 requireNonNull(asset);
110 return new Variable(builder.getName(), asset);
111 }
112 }