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.io.neeo.internal.discovery;
15 import java.net.InetAddress;
16 import java.util.List;
17 import java.util.Objects;
18 import java.util.concurrent.CopyOnWriteArrayList;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.openhab.io.neeo.internal.models.NeeoSystemInfo;
24 * This abstract implementation of {@link BrainDiscovery} will provide the listener functionality to discovery
27 * @author Tim Roberts - Initial Contribution
30 abstract class AbstractBrainDiscovery implements BrainDiscovery {
33 private final List<DiscoveryListener> listeners = new CopyOnWriteArrayList<>();
36 public void addListener(DiscoveryListener listener) {
37 Objects.requireNonNull(listener, "listener cannot be null");
38 listeners.add(listener);
42 public void removeListener(DiscoveryListener listener) {
43 Objects.requireNonNull(listener, "listener cannot be null");
44 listeners.remove(listener);
48 * Fires the {@link DiscoveryListener#discovered(NeeoSystemInfo, InetAddress)} method on all listeners
50 * @param sysInfo the non-null {@link NeeoSystemInfo}
51 * @param ipAddress the ip address of the brain
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);
62 * Fires the {@link DiscoveryListener#updated(NeeoSystemInfo, InetAddress, InetAddress)} method on all listeners
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
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);
78 * Fires the {@link DiscoveryListener#removed(NeeoSystemInfo)} method on all listeners
80 * @param sysInfo the non-null {@link NeeoSystemInfo}
82 protected void fireRemoved(NeeoSystemInfo sysInfo) {
83 Objects.requireNonNull(sysInfo, "sysInfo cannot be null");
84 for (DiscoveryListener listener : listeners) {
85 listener.removed(sysInfo);
90 public boolean removeDiscovered(String servletUrl) {