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 java.util.List;
20  import java.util.Map;
21  import org.mal_lang.langspec.Asset;
22  import org.mal_lang.langspec.builders.AssetBuilder;
23  import org.mal_lang.langspec.builders.VariableBuilder;
24  
25  /**
26   * A builder for creating {@link org.mal_lang.langspec.step.StepVariable} objects.
27   *
28   * @since 1.0.0
29   */
30  public final class StepVariableBuilder extends StepReferenceBuilder {
31    /**
32     * Constructs a new {@code StepVariableBuilder} object.
33     *
34     * @param name the name of the variable
35     * @throws java.lang.NullPointerException if {@code name} is {@code null}
36     * @throws java.lang.IllegalArgumentException if {@code name} is not a valid identifier
37     * @since 1.0.0
38     */
39    public StepVariableBuilder(String name) {
40      super(name);
41    }
42  
43    private static AssetBuilder getAssetBuilder(String assetName, List<AssetBuilder> assetBuilders) {
44      for (var assetBuilder : assetBuilders) {
45        if (assetBuilder.getName().equals(assetName)) {
46          return assetBuilder;
47        }
48      }
49      throw new IllegalStateException();
50    }
51  
52    private static VariableBuilder getVariableBuilder(
53        String assetName, String variableName, List<AssetBuilder> assetBuilders) {
54      var assetBuilder = StepVariableBuilder.getAssetBuilder(assetName, assetBuilders);
55      for (var variableBuilder : assetBuilder.getVariables()) {
56        if (variableBuilder.getName().equals(variableName)) {
57          return variableBuilder;
58        }
59      }
60      var superAsset = assetBuilder.getSuperAsset();
61      if (superAsset != null) {
62        return StepVariableBuilder.getVariableBuilder(superAsset, variableName, assetBuilders);
63      }
64      throw new IllegalStateException();
65    }
66  
67    @Override
68    public Asset getTarget(
69        Asset sourceAsset, Map<String, Asset> assets, List<AssetBuilder> assetBuilders) {
70      if (!sourceAsset.hasVariable(this.getName())) {
71        throw new IllegalArgumentException(
72            String.format("Variable %s.%s not found", sourceAsset.getName(), this.getName()));
73      }
74      return StepVariableBuilder.getVariableBuilder(
75              sourceAsset.getName(), this.getName(), assetBuilders)
76          .getStepExpression()
77          .getTarget(sourceAsset, assets, assetBuilders);
78    }
79  }