]> git.basschouten.com Git - openhab-addons.git/blob
a7f5c0f8a79a02ab4f012a29a734d5ca762e8cc9
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.nest.internal.update;
14
15 import java.util.ArrayList;
16 import java.util.HashSet;
17 import java.util.List;
18 import java.util.Map;
19 import java.util.Set;
20 import java.util.concurrent.ConcurrentHashMap;
21 import java.util.concurrent.CopyOnWriteArraySet;
22
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.binding.nest.internal.listener.NestThingDataListener;
26
27 /**
28  * Handles the updates of one type of data by notifying listeners of changes and storing the update value.
29  *
30  * @author Wouter Born - Initial contribution
31  *
32  * @param <T> the type of update data
33  */
34 @NonNullByDefault
35 public class NestUpdateHandler<T> {
36
37     /**
38      * The ID used for listeners that subscribe to any Nest update.
39      */
40     private static final String ANY_ID = "*";
41
42     private final Map<String, @Nullable T> lastUpdates = new ConcurrentHashMap<>();
43     private final Map<String, @Nullable Set<NestThingDataListener<T>>> listenersMap = new ConcurrentHashMap<>();
44
45     public boolean addListener(NestThingDataListener<T> listener) {
46         return addListener(ANY_ID, listener);
47     }
48
49     public boolean addListener(String nestId, NestThingDataListener<T> listener) {
50         return getOrCreateListeners(nestId).add(listener);
51     }
52
53     public @Nullable T getLastUpdate(String nestId) {
54         return lastUpdates.get(nestId);
55     }
56
57     public List<T> getLastUpdates() {
58         return new ArrayList<>(lastUpdates.values());
59     }
60
61     private Set<NestThingDataListener<T>> getListeners(String nestId) {
62         Set<NestThingDataListener<T>> listeners = new HashSet<>();
63         if (listenersMap.get(nestId) != null) {
64             listeners.addAll(listenersMap.get(nestId));
65         }
66         if (listenersMap.get(ANY_ID) != null) {
67             listeners.addAll(listenersMap.get(ANY_ID));
68         }
69         return listeners;
70     }
71
72     private Set<NestThingDataListener<T>> getOrCreateListeners(String nestId) {
73         Set<NestThingDataListener<T>> listeners = listenersMap.get(nestId);
74         if (listeners == null) {
75             listeners = new CopyOnWriteArraySet<>();
76             listenersMap.put(nestId, listeners);
77         }
78         return listeners;
79     }
80
81     public void handleMissingNestIds(Set<String> nestIds) {
82         nestIds.forEach(nestId -> {
83             lastUpdates.remove(nestId);
84             getListeners(nestId).forEach(l -> l.onMissingData(nestId));
85         });
86     }
87
88     public void handleUpdate(Class<T> dataClass, String nestId, T update) {
89         T lastUpdate = getLastUpdate(nestId);
90         lastUpdates.put(nestId, update);
91         notifyListeners(nestId, lastUpdate, update);
92     }
93
94     private void notifyListeners(String nestId, @Nullable T lastUpdate, T update) {
95         Set<NestThingDataListener<T>> listeners = getListeners(nestId);
96         if (lastUpdate == null) {
97             listeners.forEach(l -> l.onNewData(update));
98         } else if (!lastUpdate.equals(update)) {
99             listeners.forEach(l -> l.onUpdatedData(lastUpdate, update));
100         }
101     }
102
103     public boolean removeListener(NestThingDataListener<T> listener) {
104         return removeListener(ANY_ID, listener);
105     }
106
107     public boolean removeListener(String nestId, NestThingDataListener<T> listener) {
108         return getOrCreateListeners(nestId).remove(listener);
109     }
110
111     public void resendLastUpdates() {
112         lastUpdates.forEach((nestId, update) -> notifyListeners(nestId, null, update));
113     }
114 }