1 /*
2 * Copyright 2020-2022 Foreseeti AB <https://foreseeti.com>
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
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 * Immutable class representing a variable of an asset in a MAL language.
28 *
29 * @since 1.0.0
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 * Returns the name of this {@code Variable} object.
43 *
44 * @return the name of this {@code Variable} object
45 * @since 1.0.0
46 */
47 public String getName() {
48 return this.name;
49 }
50
51 /**
52 * Returns the asset of this {@code Variable} object.
53 *
54 * @return the asset of this {@code Variable} object
55 * @since 1.0.0
56 */
57 public Asset getAsset() {
58 return this.asset;
59 }
60
61 /**
62 * Returns the step expression of this {@code Variable} object.
63 *
64 * @return the step expression of this {@code Variable} object.
65 * @since 1.0.0
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 * Returns whether this {@code Variable} object has a super variable.
77 *
78 * @return whether this {@code Variable} object has a super variable
79 * @since 1.*.0
80 */
81 public boolean hasSuperVariable() {
82 return this.asset.hasSuperAsset() && this.asset.getSuperAsset().hasVariable(this.name);
83 }
84
85 /**
86 * Returns the super variable of this {@code Variable} object.
87 *
88 * @return the super variable of this {@code Variable} object
89 * @throws java.lang.UnsupportedOperationException if this {@code Variable} object does not have a
90 * super variable
91 * @since 1.0.0
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 }