1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.mal_lang.langspec.step;
18
19 import static java.util.Objects.requireNonNull;
20
21 import jakarta.json.Json;
22 import jakarta.json.JsonObject;
23 import java.util.Map;
24 import org.mal_lang.langspec.Asset;
25 import org.mal_lang.langspec.Variable;
26 import org.mal_lang.langspec.builders.step.StepVariableBuilder;
27
28
29
30
31
32
33 public final class StepVariable extends StepReference {
34 private final Variable variable;
35
36 private StepVariable(Asset sourceAsset, Asset targetAsset, Variable variable) {
37 super(sourceAsset, targetAsset);
38 this.variable = requireNonNull(variable);
39 }
40
41
42
43
44
45
46
47 public Variable getVariable() {
48 return this.variable;
49 }
50
51 @Override
52 public JsonObject toJson() {
53 return Json.createObjectBuilder()
54 .add("type", "variable")
55 .add("name", this.variable.getName())
56 .build();
57 }
58
59 static StepVariable fromBuilder(
60 StepVariableBuilder builder,
61 Asset sourceAsset,
62 Map<String, Asset> assets,
63 Map<Variable, Asset> variableTargets) {
64 requireNonNull(builder);
65 requireNonNull(sourceAsset);
66 requireNonNull(assets);
67 requireNonNull(variableTargets);
68 if (!sourceAsset.hasVariable(builder.getName())) {
69 throw new IllegalArgumentException(
70 String.format("Variable \"%s\" not found", builder.getName()));
71 }
72 var variable = sourceAsset.getVariable(builder.getName());
73 var targetAsset = variableTargets.get(variable);
74 return new StepVariable(sourceAsset, targetAsset, variable);
75 }
76 }