]> git.basschouten.com Git - openhab-addons.git/blob
e9eab24474206dcb9996342bf2fc53e6913c235c
[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.io.neeo.internal.discovery;
14
15 import java.net.InetAddress;
16 import java.util.List;
17 import java.util.Objects;
18 import java.util.concurrent.CopyOnWriteArrayList;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.openhab.io.neeo.internal.models.NeeoSystemInfo;
22
23 /**
24  * This abstract implementation of {@link BrainDiscovery} will provide the listener functionality to discovery
25  * implementations.
26  *
27  * @author Tim Roberts - Initial Contribution
28  */
29 @NonNullByDefault
30 abstract class AbstractBrainDiscovery implements BrainDiscovery {
31
32     /** The listeners */
33     private final List<DiscoveryListener> listeners = new CopyOnWriteArrayList<>();
34
35     @Override
36     public void addListener(DiscoveryListener listener) {
37         Objects.requireNonNull(listener, "listener cannot be null");
38         listeners.add(listener);
39     }
40
41     @Override
42     public void removeListener(DiscoveryListener listener) {
43         Objects.requireNonNull(listener, "listener cannot be null");
44         listeners.remove(listener);
45     }
46
47     /**
48      * Fires the {@link DiscoveryListener#discovered(NeeoSystemInfo, InetAddress)} method on all listeners
49      *
50      * @param sysInfo the non-null {@link NeeoSystemInfo}
51      * @param ipAddress the ip address of the brain
52      */
53     protected void fireDiscovered(NeeoSystemInfo sysInfo, InetAddress ipAddress) {
54         Objects.requireNonNull(sysInfo, "sysInfo cannot be null");
55         Objects.requireNonNull(ipAddress, "ipAddress cannot be null");
56         for (DiscoveryListener listener : listeners) {
57             listener.discovered(sysInfo, ipAddress);
58         }
59     }
60
61     /**
62      * Fires the {@link DiscoveryListener#updated(NeeoSystemInfo, InetAddress, InetAddress)} method on all listeners
63      *
64      * @param sysInfo the non-null {@link NeeoSystemInfo}
65      * @param oldIpAddress the non-null old ip address of the brain
66      * @param newIpAddress the non-null new ip address of the brain
67      */
68     protected void fireUpdated(NeeoSystemInfo sysInfo, InetAddress oldIpAddress, InetAddress newIpAddress) {
69         Objects.requireNonNull(sysInfo, "sysInfo cannot be null");
70         Objects.requireNonNull(oldIpAddress, "oldIpAddress cannot be null");
71         Objects.requireNonNull(newIpAddress, "newIpAddress cannot be null");
72         for (DiscoveryListener listener : listeners) {
73             listener.updated(sysInfo, oldIpAddress, newIpAddress);
74         }
75     }
76
77     /**
78      * Fires the {@link DiscoveryListener#removed(NeeoSystemInfo)} method on all listeners
79      *
80      * @param sysInfo the non-null {@link NeeoSystemInfo}
81      */
82     protected void fireRemoved(NeeoSystemInfo sysInfo) {
83         Objects.requireNonNull(sysInfo, "sysInfo cannot be null");
84         for (DiscoveryListener listener : listeners) {
85             listener.removed(sysInfo);
86         }
87     }
88
89     @Override
90     public boolean removeDiscovered(String servletUrl) {
91         return false;
92     }
93
94     @Override
95     public void close() {
96     }
97 }