KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > codehaus > aspectwerkz > expression > Undeterministic


1 /**************************************************************************************
2  * Copyright (c) Jonas Bon?r, Alexandre Vasseur. All rights reserved. *
3  * http://aspectwerkz.codehaus.org *
4  * ---------------------------------------------------------------------------------- *
5  * The software in this package is published under the terms of the LGPL license *
6  * a copy of which has been included with this distribution in the license.txt file. *
7  **************************************************************************************/

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

19 public abstract class Undeterministic {
20
21     /**
22      * And operation
23      *
24      * @param lhs
25      * @param rhs
26      * @return
27      */

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

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

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