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;
18  
19  import static java.util.Objects.requireNonNull;
20  
21  import jakarta.json.Json;
22  import jakarta.json.JsonObject;
23  
24  /**
25   * Enum representing the multiplicity of a field of an asset in a MAL language.
26   *
27   * @since 1.0.0
28   */
29  public enum Multiplicity {
30    /**
31     * Enum constant representing the multiplicity {@code 0..1}.
32     *
33     * @since 1.0.0
34     */
35    ZERO_OR_ONE(0, 1),
36  
37    /**
38     * Enum constant representing the multiplicity {@code 0..*} / {@code *}.
39     *
40     * @since 1.0.0
41     */
42    ZERO_OR_MORE(0, Integer.MAX_VALUE),
43  
44    /**
45     * Enum constant representing the multiplicity {@code 1..1} / {@code 1}.
46     *
47     * @since 1.0.0
48     */
49    ONE(1, 1),
50  
51    /**
52     * Enum constant representing the multiplicity {@code 1..*}.
53     *
54     * @since 1.0.0
55     */
56    ONE_OR_MORE(1, Integer.MAX_VALUE);
57  
58    private final int min;
59    private final int max;
60  
61    Multiplicity(int min, int max) {
62      this.min = min;
63      this.max = max;
64    }
65  
66    /**
67     * Returns the minimum of this {@code Multiplicity} object.
68     *
69     * @return the minimum of this {@code Multiplicity} object
70     * @since 1.0.0
71     */
72    public int getMin() {
73      return this.min;
74    }
75  
76    /**
77     * Returns the maximum of this {@code Multiplicity} object.
78     *
79     * @return the maximum of this {@code Multiplicity} object
80     * @since 1.0.0
81     */
82    public int getMax() {
83      return this.max;
84    }
85  
86    JsonObject toJson() {
87      var jsonMultiplicity = Json.createObjectBuilder().add("min", this.min);
88      if (this.max == Integer.MAX_VALUE) {
89        jsonMultiplicity.addNull("max");
90      } else {
91        jsonMultiplicity.add("max", this.max);
92      }
93      return jsonMultiplicity.build();
94    }
95  
96    /**
97     * Creates a new {@code Multiplicity} from a {@link jakarta.json.JsonObject}.
98     *
99     * @param jsonMultiplicity the {@link jakarta.json.JsonObject}
100    * @return a new {@code Multiplicity}
101    * @throws java.lang.NullPointerException if {@code jsonMultiplicity} is {@code null}
102    * @since 1.0.0
103    */
104   public static Multiplicity fromJson(JsonObject jsonMultiplicity) {
105     requireNonNull(jsonMultiplicity);
106     int min = jsonMultiplicity.getInt("min");
107     int max = jsonMultiplicity.isNull("max") ? Integer.MAX_VALUE : jsonMultiplicity.getInt("max");
108     if (min == 0 && max == 1) {
109       return ZERO_OR_ONE;
110     }
111     if (min == 0 && max == Integer.MAX_VALUE) {
112       return ZERO_OR_MORE;
113     }
114     if (min == 1 && max == 1) {
115       return ONE;
116     }
117     if (min == 1 && max == Integer.MAX_VALUE) {
118       return ONE_OR_MORE;
119     }
120     throw new RuntimeException(
121         String.format("Invalid multiplicity {min = %d, max = %d}", min, max));
122   }
123 }