blob: 1f7eed11de853f5204ee3d1890467535ede8f7ec (
plain)
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
|
/*
* Copyright (c) 2013 Isode Limited.
* All rights reserved.
* See the COPYING file for more information.
*/
/*
* Copyright (c) 2015 Tarun Gupta.
* Licensed under the simplified BSD license.
* See Documentation/Licenses/BSD-simplified.txt for more information.
*/
package com.isode.stroke.base;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
public class Listenable<T> {
private Set<T> listeners = new CopyOnWriteArraySet<T>();
public void addListener(T listener) {
listeners.add(listener);
}
public void removeListener(T listener) {
while(listeners.contains(listener)) {
listeners.remove(listener);
}
}
// Swiften code takes advantage of boost binding
// which we can't do in java. Easiest solution seems
// to be to pass listener to the caller to handle which
// method should be called
public void notifyListeners(ListenableCallback<? super T> callback) {
for (T listener : listeners) {
callback.call(listener);
}
}
/**
* Callback for subclasses to specify how the listers should be
* called
* @param <T> Type of listener that can registered with the {@link Listenable}
*/
public static interface ListenableCallback<S> {
/**
* Called by the parent {@link Listenable} class for all
* registered listeners.
* @param listener A registered listener. May be {@code null}
* if a {@code null} was registered as a listener.
*/
public void call(S listener);
}
}
|