]> git.basschouten.com Git - openhab-addons.git/blob
af63e16c097eaf6016410a4dd642103ba4b1ab53
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.mielecloud.internal.webservice.api.json;
14
15 import java.util.Map;
16 import java.util.Objects;
17 import java.util.Set;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21
22 import com.google.gson.Gson;
23 import com.google.gson.JsonSyntaxException;
24 import com.google.gson.reflect.TypeToken;
25
26 /**
27  * Immutable POJO representing a collection of actions queried from the Miele REST API.
28  *
29  * @author Björn Lange - Initial contribution
30  */
31 @NonNullByDefault
32 public class ActionsCollection {
33     private static final java.lang.reflect.Type STRING_ACTIONS_MAP_TYPE = new TypeToken<Map<String, Actions>>() {
34     }.getType();
35
36     private final Map<String, Actions> actions;
37
38     ActionsCollection(Map<String, Actions> actions) {
39         this.actions = actions;
40     }
41
42     /**
43      * Creates a new {@link ActionsCollection} from the given Json text.
44      *
45      * @param json The Json text.
46      * @return The created {@link ActionsCollection}.
47      * @throws MieleSyntaxException if parsing the data from {@code json} fails.
48      */
49     public static ActionsCollection fromJson(String json) {
50         try {
51             Map<String, Actions> actions = new Gson().fromJson(json, STRING_ACTIONS_MAP_TYPE);
52             if (actions == null) {
53                 throw new MieleSyntaxException("Failed to parse Json.");
54             }
55             return new ActionsCollection(actions);
56         } catch (JsonSyntaxException e) {
57             throw new MieleSyntaxException("Failed to parse Json.", e);
58         }
59     }
60
61     public Set<String> getDeviceIdentifiers() {
62         return actions.keySet();
63     }
64
65     public Actions getActions(String identifier) {
66         Actions actions = this.actions.get(identifier);
67         if (actions == null) {
68             throw new IllegalArgumentException("There are no actions for identifier " + identifier);
69         }
70         return actions;
71     }
72
73     @Override
74     public int hashCode() {
75         return Objects.hash(actions);
76     }
77
78     @Override
79     public boolean equals(@Nullable Object obj) {
80         if (this == obj) {
81             return true;
82         }
83         if (obj == null) {
84             return false;
85         }
86         if (getClass() != obj.getClass()) {
87             return false;
88         }
89         ActionsCollection other = (ActionsCollection) obj;
90         return Objects.equals(actions, other.actions);
91     }
92
93     @Override
94     public String toString() {
95         return "ActionsCollection [actions=" + actions + "]";
96     }
97 }