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  import java.util.LinkedHashMap;
24  import java.util.List;
25  import java.util.Map;
26  import org.mal_lang.langspec.builders.CategoryBuilder;
27  
28  /**
29   * Immutable class representing a category in a MAL language.
30   *
31   * @since 1.0.0
32   */
33  public final class Category {
34    private final String name;
35    private final Meta meta;
36    private final Map<String, Asset> assets = new LinkedHashMap<>();
37  
38    private Category(String name, Meta meta) {
39      this.name = requireNonNull(name);
40      this.meta = requireNonNull(meta);
41    }
42  
43    /**
44     * Returns the name of this {@code Category} object.
45     *
46     * @return the name of this {@code Category} object
47     * @since 1.0.0
48     */
49    public String getName() {
50      return this.name;
51    }
52  
53    /**
54     * Returns the meta info of this {@code Category} object.
55     *
56     * @return the meta info of this {@code Category} object
57     * @since 1.0.0
58     */
59    public Meta getMeta() {
60      return this.meta;
61    }
62  
63    /**
64     * Returns whether {@code name} is the name of an asset in this {@code Category} object.
65     *
66     * @param name the name of the asset
67     * @return whether {@code name} is the name of an asset in this {@code Category} object
68     * @throws java.lang.NullPointerException if {@code name} is {@code null}
69     * @since 1.0.0
70     */
71    public boolean hasAsset(String name) {
72      return this.assets.containsKey(requireNonNull(name));
73    }
74  
75    /**
76     * Returns the asset with the name {@code name} in this {@code Category} object.
77     *
78     * @param name the name of the asset
79     * @return the asset with the name {@code name} in this {@code Category} object
80     * @throws java.lang.NullPointerException if {@code name} is {@code null}
81     * @throws java.lang.IllegalArgumentException if {@code name} is not the name of an asset in this
82     *     {@code Category} object
83     * @since 1.0.0
84     */
85    public Asset getAsset(String name) {
86      if (!this.hasAsset(name)) {
87        throw new IllegalArgumentException(String.format("Asset \"%s\" not found", name));
88      }
89      return this.assets.get(name);
90    }
91  
92    /**
93     * Returns a list of all assets in this {@code Category} object.
94     *
95     * @return a list of all assets in this {@code Category} object
96     * @since 1.0.0
97     */
98    public List<Asset> getAssets() {
99      return List.copyOf(this.assets.values());
100   }
101 
102   void addAsset(Asset asset) {
103     requireNonNull(asset);
104     this.assets.put(asset.getName(), asset);
105   }
106 
107   JsonObject toJson() {
108     return Json.createObjectBuilder()
109         .add("name", this.name)
110         .add("meta", this.meta.toJson())
111         .build();
112   }
113 
114   static Category fromBuilder(CategoryBuilder builder) {
115     requireNonNull(builder);
116     return new Category(builder.getName(), Meta.fromBuilder(builder.getMeta()));
117   }
118 }