001: //
002: // Copyright (C) 2005 United States Government as represented by the
003: // Administrator of the National Aeronautics and Space Administration
004: // (NASA). All Rights Reserved.
005: //
006: // This software is distributed under the NASA Open Source Agreement
007: // (NOSA), version 1.3. The NOSA has been approved by the Open Source
008: // Initiative. See the file NOSA-1.3-JPF at the top of the distribution
009: // directory tree for the complete NOSA document.
010: //
011: // THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY OF ANY
012: // KIND, EITHER EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT
013: // LIMITED TO, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL CONFORM TO
014: // SPECIFICATIONS, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR
015: // A PARTICULAR PURPOSE, OR FREEDOM FROM INFRINGEMENT, ANY WARRANTY THAT
016: // THE SUBJECT SOFTWARE WILL BE ERROR FREE, OR ANY WARRANTY THAT
017: // DOCUMENTATION, IF PROVIDED, WILL CONFORM TO THE SUBJECT SOFTWARE.
018: //
019: package gov.nasa.jpf;
020:
021: /**
022: * helper class to avoid indirection if there is just one observer
023: * (the usual case) Typical 'Container' pattern implementation
024: */
025: public class SearchListenerMulticaster implements SearchListener {
026:
027: SearchListener head;
028: SearchListener tail;
029:
030: public static SearchListener add(SearchListener oldListener,
031: SearchListener newListener) {
032: if (newListener == null) {
033: return oldListener;
034: }
035: if (oldListener == null) {
036: return newListener;
037: }
038:
039: // we store in the order of registration, multiple entries are allowed
040: // (but generally useless)
041: return new SearchListenerMulticaster(oldListener, newListener);
042: }
043:
044: public static SearchListener remove(SearchListener oldListener,
045: SearchListener removeListener) {
046: if (oldListener == removeListener) {
047: return null;
048: }
049: if (oldListener instanceof SearchListenerMulticaster) {
050: return ((SearchListenerMulticaster) oldListener)
051: .remove(removeListener);
052: }
053:
054: return oldListener;
055: }
056:
057: protected SearchListener remove(SearchListener listener) {
058: if (listener == head) {
059: return tail;
060: }
061: if (listener == tail) {
062: return head;
063: }
064:
065: SearchListenerMulticaster h, t;
066: if (head instanceof SearchListenerMulticaster) {
067: h = (SearchListenerMulticaster) head;
068: if (tail instanceof SearchListenerMulticaster) {
069: t = (SearchListenerMulticaster) tail;
070: return new SearchListenerMulticaster(
071: h.remove(listener), t.remove(listener));
072: } else {
073: return new SearchListenerMulticaster(
074: h.remove(listener), tail);
075: }
076: } else if (tail instanceof SearchListenerMulticaster) {
077: t = (SearchListenerMulticaster) tail;
078: return new SearchListenerMulticaster(head, t
079: .remove(listener));
080: }
081:
082: return this ;
083: }
084:
085: public SearchListenerMulticaster(SearchListener h, SearchListener t) {
086: head = h;
087: tail = t;
088: }
089:
090: public void stateAdvanced(Search search) {
091: head.stateAdvanced(search);
092: tail.stateAdvanced(search);
093: }
094:
095: public void stateProcessed(Search search) {
096: head.stateProcessed(search);
097: tail.stateProcessed(search);
098: }
099:
100: public void stateBacktracked(Search search) {
101: head.stateBacktracked(search);
102: tail.stateBacktracked(search);
103: }
104:
105: public void stateRestored(Search search) {
106: head.stateRestored(search);
107: tail.stateRestored(search);
108: }
109:
110: public void propertyViolated(Search search) {
111: head.propertyViolated(search);
112: tail.propertyViolated(search);
113: }
114:
115: public void searchStarted(Search search) {
116: head.searchStarted(search);
117: tail.searchStarted(search);
118: }
119:
120: public void searchFinished(Search search) {
121: head.searchFinished(search);
122: tail.searchFinished(search);
123: }
124:
125: public void searchConstraintHit(Search search) {
126: head.searchConstraintHit(search);
127: tail.searchConstraintHit(search);
128: }
129:
130: }
|