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.builders;
18
19 import static java.util.Objects.requireNonNull;
20
21 import jakarta.json.JsonObject;
22 import java.util.ArrayList;
23 import java.util.List;
24 import org.mal_lang.langspec.builders.step.StepExpressionBuilder;
25
26 /**
27 * A builder for creating {@link org.mal_lang.langspec.Steps} objects.
28 *
29 * @since 1.0.0
30 */
31 public final class StepsBuilder {
32 private final boolean overrides;
33 private final ArrayList<StepExpressionBuilder> stepExpressions = new ArrayList<>();
34
35 /**
36 * Constructs a new {@code StepsBuilder} object.
37 *
38 * @param overrides whether this {@code StepsBuilder} overrides
39 * @since 1.0.0
40 */
41 public StepsBuilder(boolean overrides) {
42 this.overrides = overrides;
43 }
44
45 /**
46 * Returns whether this {@code StepsBuilder} object overrides.
47 *
48 * @return whether this {@code StepsBuilder} object overrides
49 * @since 1.0.0
50 */
51 public boolean overrides() {
52 return this.overrides;
53 }
54
55 /**
56 * Returns a list of all step expressions in this {@code StepsBuilder} object.
57 *
58 * @return a list of all step expressions in this {@code StepsBuilder} object
59 * @since 1.0.0
60 */
61 public List<StepExpressionBuilder> getStepExpressions() {
62 return List.copyOf(this.stepExpressions);
63 }
64
65 /**
66 * Adds a step expression to this {@code StepsBuilder} object.
67 *
68 * @param stepExpression the step expression to add
69 * @return this {@code StepsBuilder} object
70 * @throws java.lang.NullPointerException if {@code stepExpression} is {@code null}
71 * @since 1.0.0
72 */
73 public StepsBuilder addStepExpression(StepExpressionBuilder stepExpression) {
74 this.stepExpressions.add(requireNonNull(stepExpression));
75 return this;
76 }
77
78 /**
79 * Creates a new {@code StepsBuilder} from a {@link jakarta.json.JsonObject}.
80 *
81 * @param jsonSteps the {@link jakarta.json.JsonObject}
82 * @return a new {@code StepsBuilder}
83 * @throws java.lang.NullPointerException if {@code jsonSteps} is {@code null}
84 * @since 1.0.0
85 */
86 public static StepsBuilder fromJson(JsonObject jsonSteps) {
87 requireNonNull(jsonSteps);
88 var builder = new StepsBuilder(jsonSteps.getBoolean("overrides"));
89 for (var jsonStepExpression : jsonSteps.getJsonArray("stepExpressions")) {
90 builder.addStepExpression(StepExpressionBuilder.fromJson(jsonStepExpression.asJsonObject()));
91 }
92 return builder;
93 }
94 }