Prev
Next

 1 //but what does it get from EventListener? Nothing!
 2 interface ColorListener extends EventListener {
 3 
 4     public void colorChanged (ColorChangeEvent e);
 5 }
 6 
 7 interface ColorChanger {
 8     public void addColorListener (ColorListener l);
 9     public void removeColorListener (ColorListener l);
10 }
11 
12 class ColorEventMulticaster implements ColorListener {
13 
14     ColorListener a, b;
15 
16     ColorEventMulticaster (ColorListener l1, ColorListener l2) {
17 	a = l1;
18 	b = l2;
19     }
20     ColorEventMulticaster () {
21 	    Cthulhu.summon (new CthulhuSummoningListener () {
22 		    //observe the Java capitalization conventions, or English?
23 		    cthulhuSummoned (CthulhuSummoningEvent e) {
24 			((SunMicrosystemsEmployee) (e.getSource ())).suicide ();
25 		    }
26 		});
27     }
28 
29     //OK, I admit I made up the contents of that last method
30     //...
31     //This should probably conform to other Multicaster interface
32     //even though there's not one line of code in common
33     public static synchronized ColorListener add (ColorListener parent, ColorListener child) {
34 	if (parent == null) {
35 	    parent = new ColorEventMulticaster ();
36 	}
37 	((ColorEventMulticaster) parent).addColorListener (child);
38 	return parent;
39     }
40     public static synchronized  ColorListener remove (ColorListener parent, ColorListener child) {
41 	if (parent instanceof ColorEventMulticaster) {
42 	    ((ColorEventMulticaster) parent).removeColorListener (child);
43 	}
44 	return parent;
45     }
46     public synchronized void addColorListener (ColorListener l) {
47 	if (a == null) {
48 	    a = l;
49 	} else if (b == null) {
50 	    b = l;
51 	} else {
52 	    //a and b are full, fork one.
53 	    //I think this is a fibonacci tree.
54 	    if (a instanceof ColorEventMulticaster) {
55 		b = new ColorEventMulticaster (b, l);
56 	    } else {
57 		a = new ColorEventMulticaster (a, l);
58 	    }
59 	}
60     }
61     public synchronized void removeColorListener (ColorListener l) {
62 
63 	if (a == l) {
64 	    a = null;
65 	} else if (a instanceof ColorEventMulticaster) {
66 	    ((ColorEventMulticaster) a).removeColorListener (l);	    
67 	}
68 	if (b == l) {
69 	    b = null;
70 	} else if (b instanceof ColorEventMulticaster) {
71 	    ((ColorEventMulticaster) b).removeColorListener (l);
72 	}
73     }
74 
75     public void colorChanged (ColorChangeEvent e) {
76 	if (a != null) {
77 	    a.colorChanged (e);	
78 	}
79 	if (b != null) {
80 	    b.colorChanged (e);		
81 	}
82     }
83 }


Prev
Next