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;
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.Multiplicity;
24  
25  /**
26   * A builder for creating {@link org.mal_lang.langspec.Association} objects.
27   *
28   * @since 1.0.0
29   */
30  public final class AssociationBuilder {
31    private final String name;
32    private final MetaBuilder meta = new MetaBuilder();
33    private final String leftAsset;
34    private final String leftField;
35    private final Multiplicity leftMultiplicity;
36    private final String rightAsset;
37    private final String rightField;
38    private final Multiplicity rightMultiplicity;
39  
40    /**
41     * Constructs a new {@code AssociationBuilder} object.
42     *
43     * @param name the name of the association
44     * @param leftAsset the left asset of the association
45     * @param leftField the left field of the association
46     * @param leftMultiplicity the left multiplicity of the association
47     * @param rightAsset the right asset of the association
48     * @param rightField the right field of the association
49     * @param rightMultiplicity the right multiplicity of the association
50     * @throws java.lang.NullPointerException if {@code name}, {@code leftAsset}, {@code leftField},
51     *     {@code leftMultiplicity}, {@code rightAsset}, {@code rightField}, or {@code
52     *     rightMultiplicity} is {@code null}
53     * @throws java.lang.IllegalArgumentException if {@code name}, {@code leftAsset}, {@code
54     *     leftField}, {@code rightAsset}, or {@code rightField} is not a valid identifier
55     * @since 1.0.0
56     */
57    public AssociationBuilder(
58        String name,
59        String leftAsset,
60        String leftField,
61        Multiplicity leftMultiplicity,
62        String rightAsset,
63        String rightField,
64        Multiplicity rightMultiplicity) {
65      this.name = requireIdentifier(name);
66      this.leftAsset = requireIdentifier(leftAsset);
67      this.leftField = requireIdentifier(leftField);
68      this.leftMultiplicity = requireNonNull(leftMultiplicity);
69      this.rightAsset = requireIdentifier(rightAsset);
70      this.rightField = requireIdentifier(rightField);
71      this.rightMultiplicity = requireNonNull(rightMultiplicity);
72    }
73  
74    /**
75     * Returns the name of this {@code AssociationBuilder} object.
76     *
77     * @return the name of this {@code AssociationBuilder} object
78     * @since 1.0.0
79     */
80    public String getName() {
81      return this.name;
82    }
83  
84    /**
85     * Returns the meta info of this {@code AssociationBuilder} object.
86     *
87     * @return the meta info of this {@code AssociationBuilder} object
88     * @since 1.0.0
89     */
90    public MetaBuilder getMeta() {
91      return this.meta;
92    }
93  
94    /**
95     * Returns the left asset of this {@code AssociationBuilder} object.
96     *
97     * @return the left asset of this {@code AssociationBuilder} object
98     * @since 1.0.0
99     */
100   public String getLeftAsset() {
101     return this.leftAsset;
102   }
103 
104   /**
105    * Returns the left field of this {@code AssociationBuilder} object.
106    *
107    * @return the left field of this {@code AssociationBuilder} object
108    * @since 1.0.0
109    */
110   public String getLeftField() {
111     return this.leftField;
112   }
113 
114   /**
115    * Returns the left multiplicity of this {@code AssociationBuilder} object.
116    *
117    * @return the left multiplicity of this {@code AssociationBuilder} object
118    * @since 1.0.0
119    */
120   public Multiplicity getLeftMultiplicity() {
121     return this.leftMultiplicity;
122   }
123 
124   /**
125    * Returns the right asset of this {@code AssociationBuilder} object.
126    *
127    * @return the right asset of this {@code AssociationBuilder} object
128    * @since 1.0.0
129    */
130   public String getRightAsset() {
131     return this.rightAsset;
132   }
133 
134   /**
135    * Returns the right field of this {@code AssociationBuilder} object.
136    *
137    * @return the right field of this {@code AssociationBuilder} object
138    * @since 1.0.0
139    */
140   public String getRightField() {
141     return this.rightField;
142   }
143 
144   /**
145    * Returns the right multiplicity of this {@code AssociationBuilder} object.
146    *
147    * @return the right multiplicity of this {@code AssociationBuilder} object
148    * @since 1.0.0
149    */
150   public Multiplicity getRightMultiplicity() {
151     return this.rightMultiplicity;
152   }
153 
154   /**
155    * Creates a new {@code AssociationBuilder} from a {@link jakarta.json.JsonObject}.
156    *
157    * @param jsonAssociation the {@link jakarta.json.JsonObject}
158    * @return a new {@code AssociationBuilder}
159    * @throws java.lang.NullPointerException if {@code jsonAssociation} is {@code null}
160    * @since 1.0.0
161    */
162   public static AssociationBuilder fromJson(JsonObject jsonAssociation) {
163     requireNonNull(jsonAssociation);
164     var builder =
165         new AssociationBuilder(
166             jsonAssociation.getString("name"),
167             jsonAssociation.getString("leftAsset"),
168             jsonAssociation.getString("leftField"),
169             Multiplicity.fromJson(jsonAssociation.getJsonObject("leftMultiplicity")),
170             jsonAssociation.getString("rightAsset"),
171             jsonAssociation.getString("rightField"),
172             Multiplicity.fromJson(jsonAssociation.getJsonObject("rightMultiplicity")));
173     builder.getMeta().fromJson(jsonAssociation.getJsonObject("meta"));
174     return builder;
175   }
176 }