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