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 import static org.mal_lang.langspec.Utils.requireIdentifier; 21 22 import jakarta.json.JsonObject; 23 import org.mal_lang.langspec.builders.step.StepExpressionBuilder; 24 25 /** 26 * A builder for creating {@link org.mal_lang.langspec.Variable} objects. 27 * 28 * @since 1.0.0 29 */ 30 public final class VariableBuilder { 31 private final String name; 32 private final StepExpressionBuilder stepExpression; 33 34 /** 35 * Constructs a new {@code VariableBuilder} object. 36 * 37 * @param name the name of the variable 38 * @param stepExpression the step expression of the variable 39 * @throws java.lang.NullPointerException if {@code name} or {@code stepExpression} is {@code 40 * null} 41 * @throws java.lang.IllegalArgumentException if {@code name} is not a valid identifier 42 * @since 1.0.0 43 */ 44 public VariableBuilder(String name, StepExpressionBuilder stepExpression) { 45 this.name = requireIdentifier(name); 46 this.stepExpression = requireNonNull(stepExpression); 47 } 48 49 /** 50 * Returns the name of this {@code VariableBuilder} object. 51 * 52 * @return the name of this {@code VariableBuilder} object 53 * @since 1.0.0 54 */ 55 public String getName() { 56 return this.name; 57 } 58 59 /** 60 * Returns the step expression of this {@code VariableBuilder} object. 61 * 62 * @return the step expression of this {@code VariableBuilder} object 63 * @since 1.0.0 64 */ 65 public StepExpressionBuilder getStepExpression() { 66 return this.stepExpression; 67 } 68 69 /** 70 * Creates a new {@code VariableBuilder} from a {@link jakarta.json.JsonObject}. 71 * 72 * @param jsonVariable the {@link jakarta.json.JsonObject} 73 * @return a new {@code VariableBuilder} from a {@link jakarta.json.JsonObject} 74 * @throws java.lang.NullPointerException if {@code jsonVariable} is {@code null} 75 * @since 1.0.0 76 */ 77 public static VariableBuilder fromJson(JsonObject jsonVariable) { 78 requireNonNull(jsonVariable); 79 return new VariableBuilder( 80 jsonVariable.getString("name"), 81 StepExpressionBuilder.fromJson(jsonVariable.getJsonObject("stepExpression"))); 82 } 83 }