]> git.basschouten.com Git - openhab-addons.git/blob
b0a9fa7218132275d951c555031a7f05d5116ada
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.russound.internal.rio;
14
15 import java.util.concurrent.CopyOnWriteArrayList;
16
17 import org.openhab.core.types.State;
18
19 /**
20  * Abstract implementation of {@link RioHandlerCallback} that will provide listener services (adding/removing and firing
21  * of state)
22  *
23  * @author Tim Roberts - Initial contribution
24  */
25 public abstract class AbstractRioHandlerCallback implements RioHandlerCallback {
26     /** Listener array */
27     private final CopyOnWriteArrayList<ListenerState> listeners = new CopyOnWriteArrayList<>();
28
29     /**
30      * Adds a listener to {@link #listeners} wrapping the listener in a {@link ListenerState}
31      */
32     @Override
33     public void addListener(String channelId, RioHandlerCallbackListener listener) {
34         listeners.add(new ListenerState(channelId, listener));
35     }
36
37     /**
38      * Remove a listener from {@link #listeners} if the channelID matches
39      */
40     @Override
41     public void removeListener(String channelId, RioHandlerCallbackListener listener) {
42         for (ListenerState listenerState : listeners) {
43             if (listenerState.channelId.equals(channelId) && listenerState.listener == listener) {
44                 listeners.remove(listenerState);
45             }
46         }
47     }
48
49     /**
50      * Fires a stateUpdate message to all listeners for the channelId and state
51      *
52      * @param channelId a non-null, non-empty channelId
53      * @param state a non-null state
54      * @throws IllegalArgumentException if channelId is null or empty.
55      * @throws IllegalArgumentException if state is null
56      */
57     protected void fireStateUpdated(String channelId, State state) {
58         if (channelId == null || channelId.isEmpty()) {
59             throw new IllegalArgumentException("channelId cannot be null or empty)");
60         }
61         if (state == null) {
62             throw new IllegalArgumentException("state cannot be null");
63         }
64         for (ListenerState listenerState : listeners) {
65             if (listenerState.channelId.equals(channelId)) {
66                 listenerState.listener.stateUpdate(channelId, state);
67             }
68         }
69     }
70
71     /**
72      * Internal class used to associate a listener with a channel id
73      *
74      * @author Tim Roberts
75      */
76     private class ListenerState {
77         /** The channelID */
78         private final String channelId;
79         /** The listener associated with it */
80         private final RioHandlerCallbackListener listener;
81
82         /**
83          * Create the listener state from the channelID and listener
84          *
85          * @param channelId the channelID
86          * @param listener the listener
87          */
88         ListenerState(String channelId, RioHandlerCallbackListener listener) {
89             this.channelId = channelId;
90             this.listener = listener;
91         }
92     }
93 }