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.digitalstrom.internal.lib.event.types;
15 import java.util.HashMap;
17 import java.util.Map.Entry;
20 import org.openhab.binding.digitalstrom.internal.lib.event.constants.EventResponseEnum;
21 import org.openhab.binding.digitalstrom.internal.lib.serverconnection.constants.JSONApiResponseKeysEnum;
23 import com.google.gson.JsonElement;
24 import com.google.gson.JsonObject;
27 * The {@link EventItemImpl} is the implementation of the {@link EventItem}.
29 * @author Michael Ochel - Initial contribution
30 * @author Mathias Siegele - Initial contribution
32 public class EventItemImpl implements EventItem {
34 private final String name;
35 private Map<EventResponseEnum, String> properties;
36 private Map<EventResponseEnum, String> source;
39 * Creates a new {@link EventItemImpl} from the given digitalSTROM-Event-Item {@link JsonObject}.
41 * @param jsonEventItem must not be null
43 public EventItemImpl(JsonObject jsonEventItem) {
44 name = jsonEventItem.get(JSONApiResponseKeysEnum.NAME.getKey()).getAsString();
46 if (jsonEventItem.get(JSONApiResponseKeysEnum.PROPERTIES.getKey()).isJsonObject()) {
47 Set<Entry<String, JsonElement>> propObjEntrySet = jsonEventItem
48 .get(JSONApiResponseKeysEnum.PROPERTIES.getKey()).getAsJsonObject().entrySet();
49 properties = new HashMap<>(propObjEntrySet.size());
50 for (Entry<String, JsonElement> entry : propObjEntrySet) {
51 if (EventResponseEnum.containsId(entry.getKey())) {
52 addProperty(EventResponseEnum.getProperty(entry.getKey()), entry.getValue().getAsString());
56 if (jsonEventItem.get(JSONApiResponseKeysEnum.SOURCE.getKey()).isJsonObject()) {
57 Set<Entry<String, JsonElement>> sourceObjEntrySet = jsonEventItem
58 .get(JSONApiResponseKeysEnum.SOURCE.getKey()).getAsJsonObject().entrySet();
59 source = new HashMap<>(sourceObjEntrySet.size());
60 for (Entry<String, JsonElement> entry : sourceObjEntrySet) {
61 if (EventResponseEnum.containsId(entry.getKey())) {
62 addSource(EventResponseEnum.getProperty(entry.getKey()), entry.getValue().getAsString());
68 private void addProperty(EventResponseEnum propertieKey, String value) {
69 properties.put(propertieKey, value);
72 private void addSource(EventResponseEnum sourceKey, String value) {
73 source.put(sourceKey, value);
77 public String getName() {
82 public Map<EventResponseEnum, String> getProperties() {
87 public Map<EventResponseEnum, String> getSource() {
94 * @see java.lang.Object#toString()
97 public String toString() {
98 return "EventItemImpl [name=" + name + ", properties=" + properties + ", source=" + source + "]";