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 actions queried from the Miele REST API.
29 * @author Björn Lange - Initial contribution
32 public class ActionsCollection {
33 private static final java.lang.reflect.Type STRING_ACTIONS_MAP_TYPE = new TypeToken<Map<String, Actions>>() {
36 private final Map<String, Actions> actions;
38 ActionsCollection(Map<String, Actions> actions) {
39 this.actions = actions;
43 * Creates a new {@link ActionsCollection} from the given Json text.
45 * @param json The Json text.
46 * @return The created {@link ActionsCollection}.
47 * @throws MieleSyntaxException if parsing the data from {@code json} fails.
49 public static ActionsCollection fromJson(String json) {
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.");
55 return new ActionsCollection(actions);
56 } catch (JsonSyntaxException e) {
57 throw new MieleSyntaxException("Failed to parse Json.", e);
61 public Set<String> getDeviceIdentifiers() {
62 return actions.keySet();
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);
74 public int hashCode() {
75 return Objects.hash(actions);
79 public boolean equals(@Nullable Object obj) {
86 if (getClass() != obj.getClass()) {
89 ActionsCollection other = (ActionsCollection) obj;
90 return Objects.equals(actions, other.actions);
94 public String toString() {
95 return "ActionsCollection [actions=" + actions + "]";