KickJava   Java API By Example, From Geeks To Geeks.

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


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 SearchListenerMulticaster implements SearchListener {
26     
27   SearchListener head;
28   SearchListener tail;
29
30   public static SearchListener add (SearchListener oldListener, SearchListener 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 SearchListenerMulticaster(oldListener, newListener);
41   }
42
43   public static SearchListener remove (SearchListener oldListener, SearchListener removeListener){
44     if (oldListener == removeListener) {
45       return null;
46     }
47     if (oldListener instanceof SearchListenerMulticaster){
48       return ((SearchListenerMulticaster)oldListener).remove( removeListener);
49     }
50     
51     return oldListener;
52   }
53   
54   protected SearchListener remove (SearchListener listener) {
55     if (listener == head) {
56       return tail;
57     }
58     if (listener == tail){
59       return head;
60     }
61     
62     SearchListenerMulticaster h,t;
63     if (head instanceof SearchListenerMulticaster) {
64       h = (SearchListenerMulticaster)head;
65       if (tail instanceof SearchListenerMulticaster){
66         t = (SearchListenerMulticaster)tail;
67         return new SearchListenerMulticaster( h.remove(listener),t.remove(listener));
68       } else {
69         return new SearchListenerMulticaster( h.remove(listener), tail);
70       }
71     } else if (tail instanceof SearchListenerMulticaster) {
72       t = (SearchListenerMulticaster)tail;
73       return new SearchListenerMulticaster( head, t.remove(listener));
74     }
75     
76     return this;
77   }
78
79   
80   public SearchListenerMulticaster (SearchListener h, SearchListener t) {
81     head = h;
82     tail = t;
83   }
84
85   public void stateAdvanced (Search search) {
86     head.stateAdvanced(search);
87     tail.stateAdvanced(search);
88   }
89
90   public void stateProcessed (Search search) {
91     head.stateProcessed(search);
92     tail.stateProcessed(search);
93   }
94   
95   public void stateBacktracked (Search search) {
96     head.stateBacktracked(search);
97     tail.stateBacktracked(search);
98   }
99
100   public void stateRestored (Search search) {
101     head.stateRestored(search);
102     tail.stateRestored(search);
103   }
104   
105   public void propertyViolated (Search search) {
106     head.propertyViolated(search);
107     tail.propertyViolated(search);
108   }
109
110   public void searchStarted(Search search) {
111     head.searchStarted(search);
112     tail.searchStarted(search);
113   }
114   
115   public void searchFinished(Search search) {
116     head.searchFinished(search);
117     tail.searchFinished(search);
118   }
119   
120   public void searchConstraintHit(Search search) {
121     head.searchConstraintHit(search);
122     tail.searchConstraintHit(search);
123   }
124
125 }
126
Popular Tags