View Javadoc
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 java.util.ArrayList;
24  import java.util.List;
25  import org.mal_lang.langspec.builders.StepsBuilder;
26  import org.mal_lang.langspec.step.StepExpression;
27  
28  /**
29   * Immutable class representing steps of an attack step in a MAL language.
30   *
31   * @since 1.0.0
32   */
33  public final class Steps {
34    private final boolean overrides;
35    private final List<StepExpression> stepExpressions = new ArrayList<>();
36  
37    private Steps(boolean overrides) {
38      this.overrides = overrides;
39    }
40  
41    /**
42     * Returns whether this {@code Steps} object overrides.
43     *
44     * @return whether this {@code Steps} object overrides
45     * @since 1.0.0
46     */
47    public boolean overrides() {
48      return this.overrides;
49    }
50  
51    /**
52     * Returns a list of all step expressions in this {@code Steps} object.
53     *
54     * @return a list of all step expressions in this {@code Steps} object
55     * @since 1.0.0
56     */
57    public List<StepExpression> getStepExpressions() {
58      return List.copyOf(this.stepExpressions);
59    }
60  
61    void addStepExpression(StepExpression stepExpression) {
62      this.stepExpressions.add(requireNonNull(stepExpression));
63    }
64  
65    JsonObject toJson() {
66      var jsonStepExpressions = Json.createArrayBuilder();
67      for (var stepExpression : this.stepExpressions) {
68        jsonStepExpressions.add(stepExpression.toJson());
69      }
70      return Json.createObjectBuilder()
71          .add("overrides", this.overrides)
72          .add("stepExpressions", jsonStepExpressions)
73          .build();
74    }
75  
76    static Steps fromBuilder(StepsBuilder builder) {
77      requireNonNull(builder);
78      return new Steps(builder.overrides());
79    }
80  }