01: /**************************************************************************************
02: * Copyright (c) Jonas Bon?r, Alexandre Vasseur. All rights reserved. *
03: * http://aspectwerkz.codehaus.org *
04: * ---------------------------------------------------------------------------------- *
05: * The software in this package is published under the terms of the LGPL license *
06: * a copy of which has been included with this distribution in the license.txt file. *
07: **************************************************************************************/package org.codehaus.aspectwerkz.expression;
08:
09: /**
10: * Helper class to have boolean operation on true / false and null, null is assumed to be undetermined, and "not null"="null"
11: * A "false && null" will stay false, but a "true && null" will become undetermined (null).
12: *
13: * <p/>
14: * This is used when the expression cannot be resolved entirely (early matching, cflow, runtime check residuals)
15: *
16: * @author <a href="mailto:alex@gnilux.com">Alexandre Vasseur</a>
17: */
18: public abstract class Undeterministic {
19:
20: /**
21: * And operation
22: *
23: * @param lhs
24: * @param rhs
25: * @return
26: */
27: public static Boolean and(Boolean lhs, Boolean rhs) {
28: if (lhs != null && rhs != null) {
29: // regular AND
30: if (lhs.equals(Boolean.TRUE) && rhs.equals(Boolean.TRUE)) {
31: return Boolean.TRUE;
32: } else {
33: return Boolean.FALSE;
34: }
35: } else if (lhs != null && lhs.equals(Boolean.FALSE)) {
36: // one is undetermined and the other is false, so result is false
37: return Boolean.FALSE;
38: } else if (rhs != null && rhs.equals(Boolean.FALSE)) {
39: // one is undetermined and the other is false, so result is false
40: return Boolean.FALSE;
41: } else {
42: // both are undetermined, or one is true and the other undetermined
43: return null;
44: }
45: }
46:
47: /**
48: * Or operation
49: *
50: * @param lhs
51: * @param rhs
52: * @return
53: */
54: public static Boolean or(Boolean lhs, Boolean rhs) {
55: if (lhs != null && rhs != null) {
56: // regular OR
57: if (lhs.equals(Boolean.TRUE) || rhs.equals(Boolean.TRUE)) {
58: return Boolean.TRUE;
59: } else {
60: return Boolean.FALSE;
61: }
62: } else {
63: // one or both is/are undetermined
64: // OR cannot be resolved
65: return null;
66: }
67: }
68:
69: /**
70: * Not operation
71: *
72: * @param b
73: * @return
74: */
75: public static Boolean not(Boolean b) {
76: if (b != null) {
77: // regular NOT
78: if (b.equals(Boolean.TRUE)) {
79: return Boolean.FALSE;
80: } else {
81: return Boolean.TRUE;
82: }
83: } else {
84: return null;
85: }
86: }
87:
88: }
|