KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gov > nasa > jpf > VMListenerMulticaster


1 //
2
// Copyright (C) 2005 United States Government as represented by the
3
// Administrator of the National Aeronautics and Space Administration
4
// (NASA). All Rights Reserved.
5
//
6
// This software is distributed under the NASA Open Source Agreement
7
// (NOSA), version 1.3. The NOSA has been approved by the Open Source
8
// Initiative. See the file NOSA-1.3-JPF at the top of the distribution
9
// directory tree for the complete NOSA document.
10
//
11
// THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY OF ANY
12
// KIND, EITHER EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT
13
// LIMITED TO, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL CONFORM TO
14
// SPECIFICATIONS, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR
15
// A PARTICULAR PURPOSE, OR FREEDOM FROM INFRINGEMENT, ANY WARRANTY THAT
16
// THE SUBJECT SOFTWARE WILL BE ERROR FREE, OR ANY WARRANTY THAT
17
// DOCUMENTATION, IF PROVIDED, WILL CONFORM TO THE SUBJECT SOFTWARE.
18
//
19
package gov.nasa.jpf;
20
21 /**
22  * helper class to avoid indirection if there is just one observer
23  * (the usual case). Typical 'Container' pattern implementation
24  */

25 public class VMListenerMulticaster implements VMListener
26 {
27   VMListener head;
28   VMListener tail;
29
30   public static VMListener add (VMListener oldListener, VMListener newListener) {
31     if (newListener == null) {
32       return oldListener;
33     }
34     if (oldListener == null) {
35       return newListener;
36     }
37     
38     // we store in the order of registration, multiple entries are allowed
39
// (but generally useless)
40
return new VMListenerMulticaster(oldListener, newListener);
41   }
42
43   public static VMListener remove (VMListener oldListener, VMListener removeListener){
44     if (oldListener == removeListener) {
45       return null;
46     }
47     if (oldListener instanceof VMListenerMulticaster){
48       return ((VMListenerMulticaster)oldListener).remove( removeListener);
49     }
50     
51     return oldListener;
52   }
53   
54   protected VMListener remove (VMListener listener) {
55     if (listener == head) {
56       return tail;
57     }
58     if (listener == tail){
59       return head;
60     }
61     
62     VMListenerMulticaster h,t;
63     if (head instanceof VMListenerMulticaster) {
64       h = (VMListenerMulticaster)head;
65       if (tail instanceof VMListenerMulticaster){
66         t = (VMListenerMulticaster)tail;
67         return new VMListenerMulticaster( h.remove(listener),t.remove(listener));
68       } else {
69         return new VMListenerMulticaster( h.remove(listener), tail);
70       }
71     } else if (tail instanceof VMListenerMulticaster) {
72       t = (VMListenerMulticaster)tail;
73       return new VMListenerMulticaster( head, t.remove(listener));
74     }
75     
76     return this;
77   }
78
79   
80   public VMListenerMulticaster (VMListener h, VMListener t) {
81     head = h;
82     tail = t;
83   }
84
85   public void instructionExecuted (VM vm) {
86     head.instructionExecuted(vm);
87     tail.instructionExecuted(vm);
88   }
89   
90   public void threadStarted (VM vm) {
91     head.threadStarted(vm);
92     tail.threadStarted(vm);
93   }
94
95   public void threadTerminated (VM vm) {
96     head.threadTerminated(vm);
97     tail.threadTerminated(vm);
98   }
99   
100   public void classLoaded (VM vm) {
101     head.classLoaded(vm);
102     tail.classLoaded(vm);
103   }
104   
105   public void objectCreated (VM vm) {
106     head.objectCreated(vm);
107     tail.objectCreated(vm);
108   }
109   
110   public void objectReleased (VM vm) {
111     head.objectReleased(vm);
112     tail.objectReleased(vm);
113   }
114
115   public void gcBegin (VM vm) {
116     head.gcBegin(vm);
117     tail.gcBegin(vm);
118   }
119
120   public void gcEnd (VM vm) {
121     head.gcEnd(vm);
122     tail.gcEnd(vm);
123   }
124   
125   public void exceptionThrown (VM vm) {
126     head.exceptionThrown(vm);
127     tail.exceptionThrown(vm);
128   }
129   
130 }
131
132
Popular Tags