KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > aspectwerkz > expression > Undeterministic


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.aspectwerkz.expression;
5
6 /**
7  * Helper class to have boolean operation on true / false and null, null is assumed to be undetermined, and "not null"="null"
8  * A "false && null" will stay false, but a "true && null" will become undetermined (null).
9  * <p/>
10  * <p/>
11  * This is used when the expression cannot be resolved entirely (early matching, cflow, runtime check residuals)
12  *
13  * @author <a HREF="mailto:alex@gnilux.com">Alexandre Vasseur</a>
14  */

15 public abstract class Undeterministic {
16
17   /**
18    * And operation
19    *
20    * @param lhs
21    * @param rhs
22    * @return
23    */

24   public static Boolean JavaDoc and(Boolean JavaDoc lhs, Boolean JavaDoc rhs) {
25     if (lhs != null && rhs != null) {
26       // regular AND
27
if (lhs.equals(Boolean.TRUE) && rhs.equals(Boolean.TRUE)) {
28         return Boolean.TRUE;
29       } else {
30         return Boolean.FALSE;
31       }
32     } else if (lhs != null && lhs.equals(Boolean.FALSE)) {
33       // one is undetermined and the other is false, so result is false
34
return Boolean.FALSE;
35     } else if (rhs != null && rhs.equals(Boolean.FALSE)) {
36       // one is undetermined and the other is false, so result is false
37
return Boolean.FALSE;
38     } else {
39       // both are undetermined, or one is true and the other undetermined
40
return null;
41     }
42   }
43
44   /**
45    * Or operation
46    *
47    * @param lhs
48    * @param rhs
49    * @return
50    */

51   public static Boolean JavaDoc or(Boolean JavaDoc lhs, Boolean JavaDoc rhs) {
52     if (lhs != null && rhs != null) {
53       // regular OR
54
if (lhs.equals(Boolean.TRUE) || rhs.equals(Boolean.TRUE)) {
55         return Boolean.TRUE;
56       } else {
57         return Boolean.FALSE;
58       }
59     } else {
60       // one or both is/are undetermined
61
// OR cannot be resolved
62
return null;
63     }
64   }
65
66   /**
67    * Not operation
68    *
69    * @param b
70    * @return
71    */

72   public static Boolean JavaDoc not(Boolean JavaDoc b) {
73     if (b != null) {
74       // regular NOT
75
if (b.equals(Boolean.TRUE)) {
76         return Boolean.FALSE;
77       } else {
78         return Boolean.TRUE;
79       }
80     } else {
81       return null;
82     }
83   }
84
85
86 }
87
Popular Tags