forked from vijayontheweb/Concurrency
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVisualComponent.java
More file actions
48 lines (39 loc) · 1.41 KB
/
VisualComponent.java
File metadata and controls
48 lines (39 loc) · 1.41 KB
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
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package concurrency.composingobjects;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
/**
* Delegating Thread Safety to Multiple Underlying State Variables.
*
* VisualComponent uses a CopyOnWriteArrayList to store each listener list;
* this is a thread safe List implementation particularly suited for managing
* listener lists (see Section 5.2.3). Each List is thread safe, and
* because there are no constraints coupling the state of one to the
* state of the other, VisualComponent can delegate its thread safety
* responsibilities to the underlying mouseListeners and keyListeners objects.
*
* @author vijay
*/
public class VisualComponent {
public final List<MouseListener> mouseListeners = new CopyOnWriteArrayList<MouseListener>();
public final List<KeyListener> keyListeners = new CopyOnWriteArrayList<KeyListener>();
public void addMouseListener(MouseListener m) {
mouseListeners.add(m);
}
public void removeMouseListener(MouseListener m) {
mouseListeners.remove(m);
}
public void addKeyListener(KeyListener k) {
keyListeners.add(k);
}
public void removeKeyListener(KeyListener k) {
keyListeners.remove(k);
}
}
class MouseListener {
}
class KeyListener {
}