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