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.step;
18  
19  import static java.util.Objects.requireNonNull;
20  
21  import jakarta.json.JsonObject;
22  import java.util.Map;
23  import org.mal_lang.langspec.Asset;
24  import org.mal_lang.langspec.Variable;
25  import org.mal_lang.langspec.builders.step.StepAttackStepBuilder;
26  import org.mal_lang.langspec.builders.step.StepCollectBuilder;
27  import org.mal_lang.langspec.builders.step.StepDifferenceBuilder;
28  import org.mal_lang.langspec.builders.step.StepExpressionBuilder;
29  import org.mal_lang.langspec.builders.step.StepFieldBuilder;
30  import org.mal_lang.langspec.builders.step.StepIntersectionBuilder;
31  import org.mal_lang.langspec.builders.step.StepSubTypeBuilder;
32  import org.mal_lang.langspec.builders.step.StepTransitiveBuilder;
33  import org.mal_lang.langspec.builders.step.StepUnionBuilder;
34  import org.mal_lang.langspec.builders.step.StepVariableBuilder;
35  
36  /**
37   * Immutable class representing a step expression in a MAL language.
38   *
39   * @since 1.0.0
40   */
41  public abstract class StepExpression {
42    private final Asset sourceAsset;
43    private final Asset targetAsset;
44  
45    StepExpression(Asset sourceAsset, Asset targetAsset) {
46      this.sourceAsset = requireNonNull(sourceAsset);
47      this.targetAsset = requireNonNull(targetAsset);
48    }
49  
50    /**
51     * Returns the source asset of this {@code StepExpression} object.
52     *
53     * @return the source asset of this {@code StepExpression} object
54     * @since 1.0.0
55     */
56    public Asset getSourceAsset() {
57      return this.sourceAsset;
58    }
59  
60    /**
61     * Returns the target asset of this {@code StepExpression} object.
62     *
63     * @return the target asset of this {@code StepExpression} object
64     * @since 1.0.0
65     */
66    public Asset getTargetAsset() {
67      return this.targetAsset;
68    }
69  
70    /**
71     * Returns the JSON representation of this {@code StepExpression} object.
72     *
73     * @return the JSON representation of this {@code StepExpression} object
74     * @since 1.0.0
75     */
76    public abstract JsonObject toJson();
77  
78    /**
79     * Creates a new {@code StepExpression} object from a {@link
80     * org.mal_lang.langspec.builders.step.StepExpressionBuilder}.
81     *
82     * @param builder the {@link org.mal_lang.langspec.builders.step.StepExpressionBuilder}
83     * @param sourceAsset the source asset of the step expression
84     * @param assets all assets of the language
85     * @param variableTargets a map of target assets of variables
86     * @return a new {@code StepExpression} object
87     * @throws java.lang.NullPointerException if {@code builder}, {@code sourceAsset}, or {@code
88     *     assets} is {@code null}
89     * @since 1.0.0
90     */
91    public static StepExpression fromBuilder(
92        StepExpressionBuilder builder,
93        Asset sourceAsset,
94        Map<String, Asset> assets,
95        Map<Variable, Asset> variableTargets) {
96      requireNonNull(builder);
97      requireNonNull(sourceAsset);
98      requireNonNull(assets);
99      requireNonNull(variableTargets);
100     if (builder instanceof StepUnionBuilder) {
101       return StepUnion.fromBuilder(
102           (StepUnionBuilder) builder, sourceAsset, assets, variableTargets);
103     }
104     if (builder instanceof StepIntersectionBuilder) {
105       return StepIntersection.fromBuilder(
106           (StepIntersectionBuilder) builder, sourceAsset, assets, variableTargets);
107     }
108     if (builder instanceof StepDifferenceBuilder) {
109       return StepDifference.fromBuilder(
110           (StepDifferenceBuilder) builder, sourceAsset, assets, variableTargets);
111     }
112     if (builder instanceof StepCollectBuilder) {
113       return StepCollect.fromBuilder(
114           (StepCollectBuilder) builder, sourceAsset, assets, variableTargets);
115     }
116     if (builder instanceof StepTransitiveBuilder) {
117       return StepTransitive.fromBuilder(
118           (StepTransitiveBuilder) builder, sourceAsset, assets, variableTargets);
119     }
120     if (builder instanceof StepSubTypeBuilder) {
121       return StepSubType.fromBuilder(
122           (StepSubTypeBuilder) builder, sourceAsset, assets, variableTargets);
123     }
124     if (builder instanceof StepFieldBuilder) {
125       return StepField.fromBuilder(
126           (StepFieldBuilder) builder, sourceAsset, assets, variableTargets);
127     }
128     if (builder instanceof StepAttackStepBuilder) {
129       return StepAttackStep.fromBuilder(
130           (StepAttackStepBuilder) builder, sourceAsset, assets, variableTargets);
131     }
132     if (builder instanceof StepVariableBuilder) {
133       return StepVariable.fromBuilder(
134           (StepVariableBuilder) builder, sourceAsset, assets, variableTargets);
135     }
136     throw new RuntimeException(
137         String.format(
138             "Invalid %s sub-type %s",
139             StepExpressionBuilder.class.getName(), builder.getClass().getName()));
140   }
141 }