-
Notifications
You must be signed in to change notification settings - Fork 241
/
Broadcaster.java
145 lines (133 loc) · 3.83 KB
/
Broadcaster.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
* *****************************************************************************
* Copyright (C) 2014-2023 Dennis Sheirer
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
* ****************************************************************************
*/
package io.github.dsheirer.sample;
import io.github.dsheirer.log.LoggingSuppressor;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import org.slf4j.LoggerFactory;
/**
* Broadcasts an item to multiple listeners
*/
public class Broadcaster<T> implements Listener<T>
{
private static LoggingSuppressor sLoggingSuppressor;
private List<Listener<T>> mListeners = new CopyOnWriteArrayList<>();
/**
* Constructs an instance
*/
public Broadcaster()
{
sLoggingSuppressor = new LoggingSuppressor(LoggerFactory.getLogger(Broadcaster.class));
}
/**
* Implements the Listener<T> interface to receive an element and broadcast that element to all registered
* listeners.
*
* @param t element to broadcast
*/
@Override
public void receive(T t)
{
broadcast(t);
}
/**
* Clear listeners to prepare for garbage collection
*/
public void dispose()
{
clear();
}
/**
* Indicates if this broadcaster has any listeners registered
*/
public boolean hasListeners()
{
return !mListeners.isEmpty();
}
/**
* The count of listeners currently registered with this broadcaster
*/
public int getListenerCount()
{
return mListeners.size();
}
/**
* The list of listeners currently registered with this broadcaster
*/
public List<Listener<T>> getListeners()
{
return Collections.unmodifiableList(mListeners);
}
/**
* Registers the listener to receive elements from this broadcaster
*
* @param listener
*/
public void addListener(Listener<T> listener)
{
if(listener != null && !mListeners.contains(listener))
{
mListeners.add(listener);
}
}
/**
* Deregisters the listener from receiving elements from this broadcaster
*/
public void removeListener(Listener<T> listener)
{
if(listener != null)
{
mListeners.remove(listener);
}
}
/**
* Deregisters all listeners from this broadcaster
*/
public void clear()
{
mListeners.clear();
}
/**
* Broadcasts the element to all registered listeners
*/
public void broadcast(T t)
{
for(Listener<T> listener: mListeners)
{
try
{
listener.receive(t);
}
catch(Exception e)
{
if(t != null)
{
sLoggingSuppressor.error(t.getClass().toGenericString(), 5,
"Error while broadcasting [" + t.getClass() + "] to listeners", e);
}
else
{
sLoggingSuppressor.error("null broadcast object", 5, "Can't broadcast null " +
"object to listener [" + listener.getClass() + "]", e);
}
}
}
}
}