KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gov > nasa > jpf > search > PropertyMulticaster


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.search;
20
21 import gov.nasa.jpf.Property;
22 import gov.nasa.jpf.VM;
23
24 import java.io.PrintWriter JavaDoc;
25
26 /**
27  * simple list abstracting a set of Properties. This version cannot deal with
28  * reporting multiple failures
29  */

30 public class PropertyMulticaster implements Property {
31   Property head, tail;
32   boolean headFailed, tailFailed;
33   
34   public static Property add (Property oldProperty, Property newProperty) {
35     if (newProperty == null) {
36       return oldProperty;
37     }
38     if (oldProperty == null) {
39       return newProperty;
40     }
41     
42     // we store in the order of registration, multiple entries are allowed
43
// (but generally useless)
44
return new PropertyMulticaster(oldProperty, newProperty);
45   }
46   
47   public static Property remove (Property oldProperty, Property removeProperty){
48     if (oldProperty == removeProperty) {
49       return null;
50     }
51     if (oldProperty instanceof PropertyMulticaster){
52       return ((PropertyMulticaster)oldProperty).remove( removeProperty);
53     }
54     
55     return oldProperty;
56   }
57   
58   protected Property remove (Property p) {
59     if (p == head) {
60       return tail;
61     }
62     if (p == tail){
63       return head;
64     }
65     
66     if (head instanceof PropertyMulticaster) {
67       if (tail instanceof PropertyMulticaster){
68         return new PropertyMulticaster( ((PropertyMulticaster)head).remove(p),
69                                         ((PropertyMulticaster)tail).remove(p));
70       } else {
71         return new PropertyMulticaster( ((PropertyMulticaster)head).remove(p), tail);
72       }
73     } else if (tail instanceof PropertyMulticaster) {
74       return new PropertyMulticaster( head, ((PropertyMulticaster)tail).remove(p));
75     }
76     
77     return this;
78   }
79   
80   public PropertyMulticaster (Property h, Property t) {
81     head = h;
82     tail = t;
83   }
84   
85   public boolean check (VM vm, Object JavaDoc arg) {
86     if (!head.check(vm,arg)){
87       headFailed = true;
88       return false;
89     }
90     if (!tail.check(vm,arg)){
91       tailFailed = true;
92       return false;
93     }
94     
95     return true;
96   }
97
98   public String JavaDoc getErrorMessage () {
99     if (headFailed) {
100       return head.getErrorMessage();
101     } else if (tailFailed){
102       return tail.getErrorMessage();
103     }
104     
105     return null;
106   }
107
108   public void printOn (PrintWriter JavaDoc pw) {
109     if (headFailed){
110       head.printOn(pw);
111     } else if (tailFailed) {
112       tail.printOn(pw);
113     }
114   }
115 }
116
Popular Tags