1 package org.votech.plastic.managers;
2
3 import java.util.Collection;
4 import java.util.HashSet;
5 import java.util.Iterator;
6
7 /**
8 * Handles the notification of listeners.
9 * @author jdt
10 *
11 */
12 public abstract class AbstractObservableManager implements ObservableManager {
13
14 public interface ManagerObserver {
15
16 void down();
17
18 void up();
19
20 }
21
22
23
24 private Collection observers = new HashSet();
25
26 public AbstractObservableManager() {
27
28 Runtime.getRuntime().addShutdownHook(new Thread() {
29 public void run() {
30 disconnect();
31 }
32 });
33 }
34
35
36
37
38 public synchronized void addObserver(ManagerObserver observer) {
39 observers.add(observer);
40 }
41
42
43
44
45 public synchronized void removeObserver(ManagerObserver observer) {
46 observers.remove(observer);
47 }
48
49 protected synchronized void notifyObserversUp() {
50 Iterator it = observers.iterator();
51 while(it.hasNext()) {
52 ManagerObserver observer = (ManagerObserver) it.next();
53 observer.up();
54 }
55
56 }
57
58 protected synchronized void notifyObserversDown() {
59 Iterator it = observers.iterator();
60 while(it.hasNext()) {
61 ManagerObserver observer = (ManagerObserver) it.next();
62 observer.down();
63 }
64 }
65
66 }