]> git.basschouten.com Git - openhab-addons.git/blob
39c9431d2f0b9ed898d3f55a557339bb43a37ff9
[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 devices queried from the Miele REST API.
28  *
29  * @author Björn Lange - Initial contribution
30  */
31 @NonNullByDefault
32 public class DeviceCollection {
33     private static final java.lang.reflect.Type STRING_DEVICE_MAP_TYPE = new TypeToken<Map<String, Device>>() {
34     }.getType();
35
36     private final Map<String, Device> devices;
37
38     DeviceCollection(Map<String, Device> devices) {
39         this.devices = devices;
40     }
41
42     /**
43      * Creates a new {@link DeviceCollection} from the given Json text.
44      *
45      * @param json The Json text.
46      * @return The created {@link DeviceCollection}.
47      * @throws MieleSyntaxException if parsing the data from {@code json} fails.
48      */
49     public static DeviceCollection fromJson(String json) {
50         try {
51             Map<String, Device> devices = new Gson().fromJson(json, STRING_DEVICE_MAP_TYPE);
52             if (devices == null) {
53                 throw new MieleSyntaxException("Failed to parse Json.");
54             }
55             return new DeviceCollection(devices);
56         } catch (JsonSyntaxException e) {
57             throw new MieleSyntaxException("Failed to parse Json.", e);
58         }
59     }
60
61     public Set<String> getDeviceIdentifiers() {
62         return devices.keySet();
63     }
64
65     public Device getDevice(String identifier) {
66         Device device = devices.get(identifier);
67         if (device == null) {
68             throw new IllegalArgumentException("There is no device for identifier " + identifier);
69         }
70         return device;
71     }
72
73     @Override
74     public int hashCode() {
75         return Objects.hash(devices);
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         DeviceCollection other = (DeviceCollection) obj;
90         return Objects.equals(devices, other.devices);
91     }
92
93     @Override
94     public String toString() {
95         return "DeviceCollection [devices=" + devices + "]";
96     }
97 }