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.builders.step;
18  
19  import static java.util.Objects.requireNonNull;
20  
21  import jakarta.json.JsonObject;
22  
23  /**
24   * A builder for creating {@link org.mal_lang.langspec.step.StepBinaryOperation} objects.
25   *
26   * @since 1.0.0
27   */
28  public abstract class StepBinaryOperationBuilder extends StepExpressionBuilder {
29    private final StepExpressionBuilder lhs;
30    private final StepExpressionBuilder rhs;
31  
32    /**
33     * Constructs a new {@code StepBinaryOperationBuilder} object.
34     *
35     * @param lhs the left-hand side of the binary operation
36     * @param rhs the right-hand side of the binary operation
37     * @throws java.lang.NullPointerException if {@code lhs} or {@code rhs} is {@code null}
38     * @since 1.0.0
39     */
40    protected StepBinaryOperationBuilder(StepExpressionBuilder lhs, StepExpressionBuilder rhs) {
41      this.lhs = requireNonNull(lhs);
42      this.rhs = requireNonNull(rhs);
43    }
44  
45    /**
46     * Returns the left-hand side of this {@code StepBinaryOperationBuilder} object.
47     *
48     * @return the left-hand side of this {@code StepBinaryOperationBuilder} object
49     * @since 1.0.0
50     */
51    public StepExpressionBuilder getLhs() {
52      return this.lhs;
53    }
54  
55    /**
56     * Returns the right-hand side of this {@code StepBinaryOperationBuilder} object.
57     *
58     * @return the right-hand side of this {@code StepBinaryOperationBuilder} object
59     * @since 1.0.0
60     */
61    public StepExpressionBuilder getRhs() {
62      return this.rhs;
63    }
64  
65    /**
66     * Creates a new {@code StepBinaryOperationBuilder} from a {@link jakarta.json.JsonObject}.
67     *
68     * @param jsonStepBinaryOperation the {@link jakarta.json.JsonObject}
69     * @return a new {@code StepBinaryOperationBuilder}
70     * @throws java.lang.NullPointerException if {@code jsonStepBinaryOperation} is {@code null}
71     * @since 1.0.0
72     */
73    public static StepBinaryOperationBuilder fromJson(JsonObject jsonStepBinaryOperation) {
74      requireNonNull(jsonStepBinaryOperation);
75      var lhs = StepExpressionBuilder.fromJson(jsonStepBinaryOperation.getJsonObject("lhs"));
76      var rhs = StepExpressionBuilder.fromJson(jsonStepBinaryOperation.getJsonObject("rhs"));
77      switch (jsonStepBinaryOperation.getString("type")) {
78        case "union":
79          return new StepUnionBuilder(lhs, rhs);
80        case "intersection":
81          return new StepIntersectionBuilder(lhs, rhs);
82        case "difference":
83          return new StepDifferenceBuilder(lhs, rhs);
84        case "collect":
85          return new StepCollectBuilder(lhs, rhs);
86        default:
87          throw new RuntimeException(
88              String.format(
89                  "Invalid step expression type \"%s\"", jsonStepBinaryOperation.getString("type")));
90      }
91    }
92  }