]> git.basschouten.com Git - openhab-addons.git/blob
4b405667cc21cea5da890bf288ca66b1132bd607
[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.digitalstrom.internal.lib.structure.impl;
14
15 import java.util.LinkedList;
16 import java.util.List;
17
18 import org.openhab.binding.digitalstrom.internal.lib.serverconnection.constants.JSONApiResponseKeysEnum;
19 import org.openhab.binding.digitalstrom.internal.lib.structure.DetailedGroupInfo;
20
21 import com.google.gson.JsonArray;
22 import com.google.gson.JsonObject;
23
24 /**
25  * The {@link JSONDetailedGroupInfoImpl} is the implementation of the {@link DetailedGroupInfo}.
26  *
27  * @author Alexander Betker - Initial contribution
28  * @author Michael Ochel - change from SimpleJSON to GSON
29  * @author Matthias Siegele - change from SimpleJSON to GSON
30  */
31 public class JSONDetailedGroupInfoImpl implements DetailedGroupInfo {
32
33     private String name;
34     private short groupId = 0;
35     private final List<String> deviceList;
36
37     /**
38      * Creates a new {@link JSONDetailedGroupInfoImpl} through the {@link JsonObject}.
39      *
40      * @param jObject of the server response, must not be null
41      */
42     public JSONDetailedGroupInfoImpl(JsonObject jObject) {
43         this.deviceList = new LinkedList<>();
44         if (jObject.get(JSONApiResponseKeysEnum.NAME.getKey()) != null) {
45             name = jObject.get(JSONApiResponseKeysEnum.NAME.getKey()).getAsString();
46         }
47         if (jObject.get(JSONApiResponseKeysEnum.ID.getKey()) != null) {
48             this.groupId = jObject.get(JSONApiResponseKeysEnum.ID.getKey()).getAsShort();
49         }
50         if (jObject.get(JSONApiResponseKeysEnum.DEVICES.getKey()) instanceof JsonArray) {
51             JsonArray array = (JsonArray) jObject.get(JSONApiResponseKeysEnum.DEVICES.getKey());
52
53             for (int i = 0; i < array.size(); i++) {
54                 if (array.get(i) != null) {
55                     deviceList.add(array.get(i).getAsString());
56                 }
57             }
58         }
59     }
60
61     @Override
62     public short getGroupID() {
63         return groupId;
64     }
65
66     @Override
67     public String getGroupName() {
68         return name;
69     }
70
71     @Override
72     public List<String> getDeviceList() {
73         return deviceList;
74     }
75
76     @Override
77     public boolean equals(Object obj) {
78         if (obj instanceof DetailedGroupInfo) {
79             DetailedGroupInfo group = (DetailedGroupInfo) obj;
80             return group.getGroupID() == this.getGroupID();
81         }
82         return false;
83     }
84 }