2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.mielecloud.internal.webservice.api.json;
16 import java.util.Objects;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
22 import com.google.gson.Gson;
23 import com.google.gson.JsonSyntaxException;
24 import com.google.gson.reflect.TypeToken;
27 * Immutable POJO representing a collection of devices queried from the Miele REST API.
29 * @author Björn Lange - Initial contribution
32 public class DeviceCollection {
33 private static final java.lang.reflect.Type STRING_DEVICE_MAP_TYPE = new TypeToken<Map<String, Device>>() {
36 private final Map<String, Device> devices;
38 DeviceCollection(Map<String, Device> devices) {
39 this.devices = devices;
43 * Creates a new {@link DeviceCollection} from the given Json text.
45 * @param json The Json text.
46 * @return The created {@link DeviceCollection}.
47 * @throws MieleSyntaxException if parsing the data from {@code json} fails.
49 public static DeviceCollection fromJson(String json) {
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.");
55 return new DeviceCollection(devices);
56 } catch (JsonSyntaxException e) {
57 throw new MieleSyntaxException("Failed to parse Json.", e);
61 public Set<String> getDeviceIdentifiers() {
62 return devices.keySet();
65 public Device getDevice(String identifier) {
66 Device device = devices.get(identifier);
68 throw new IllegalArgumentException("There is no device for identifier " + identifier);
74 public int hashCode() {
75 return Objects.hash(devices);
79 public boolean equals(@Nullable Object obj) {
86 if (getClass() != obj.getClass()) {
89 DeviceCollection other = (DeviceCollection) obj;
90 return Objects.equals(devices, other.devices);
94 public String toString() {
95 return "DeviceCollection [devices=" + devices + "]";