View Javadoc
1 /*** 2 * Copyright 2003, 2004, 2005. CodeStreet LLC. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 7 * 8 * Unless required by applicable law or agreed to in writing, software 9 * distributed under the License is distributed on an "AS IS" BASIS, 10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 * See the License for the specific language governing permissions and 12 * limitations under the License. 13 */ 14 15 package com.codestreet.selector.parser; 16 17 import java.util.Map; 18 19 /*** 20 * Typesafe enumeration of valid conditional operators. 21 * 22 * @author Jawaid Hakim. 23 */ 24 abstract class ConditionalOpImpl 25 { 26 /*** 27 * Ctor. 28 * 29 * @param operator 30 * Operator. 31 */ 32 private ConditionalOpImpl(final String operator) 33 { 34 operator_ = operator; 35 } 36 37 public String toString() 38 { 39 return operator_; 40 } 41 42 public abstract Object eval(final Map identifiers, final IExpression lhs, 43 final IExpression rhs); 44 45 public abstract Object eval(final IValueProvider provider, 46 final Object corr, final IExpression lhs, final IExpression rhs); 47 48 /*** 49 * AND operator. 50 */ 51 public static final ConditionalOpImpl AND = new ConditionalOpImpl(" AND ") 52 { 53 public Object eval(final Map identifiers, final IExpression lhs, 54 final IExpression rhs) 55 { 56 Result bLhs = (Result) lhs.eval(identifiers); 57 // Short circuit evaulation if LHS is FALSE or UNKNOWN 58 if (bLhs == Result.RESULT_FALSE) 59 return Result.RESULT_FALSE; 60 else if (bLhs == Result.RESULT_UNKNOWN) 61 return Result.RESULT_UNKNOWN; 62 63 return (Result) rhs.eval(identifiers); 64 } 65 66 public Object eval(final IValueProvider provider, final Object corr, 67 final IExpression lhs, final IExpression rhs) 68 { 69 Result bLhs = (Result) lhs.eval(provider, corr); 70 // Short circuit evaulation if LHS is FALSE or UNKNOWN 71 if (bLhs == Result.RESULT_FALSE) 72 return Result.RESULT_FALSE; 73 else if (bLhs == Result.RESULT_UNKNOWN) 74 return Result.RESULT_UNKNOWN; 75 76 return (Result) rhs.eval(provider, corr); 77 } 78 }; 79 80 /*** 81 * OR operator. 82 */ 83 public static final ConditionalOpImpl OR = new ConditionalOpImpl(" OR ") 84 { 85 public Object eval(final Map identifiers, final IExpression lhs, 86 final IExpression rhs) 87 { 88 Result bLhs = (Result) lhs.eval(identifiers); 89 // Short circuit evaulation if LHS is TRUE 90 if (bLhs == Result.RESULT_TRUE) 91 return Result.RESULT_TRUE; 92 93 Result bRhs = (Result) rhs.eval(identifiers); 94 if (bRhs == Result.RESULT_TRUE) 95 return Result.RESULT_TRUE; 96 else if (bLhs == Result.RESULT_UNKNOWN 97 || bRhs == Result.RESULT_UNKNOWN) 98 return Result.RESULT_UNKNOWN; 99 else 100 return Result.RESULT_FALSE; 101 } 102 103 public Object eval(final IValueProvider provider, final Object corr, 104 final IExpression lhs, final IExpression rhs) 105 { 106 Result bLhs = (Result) lhs.eval(provider, corr); 107 // Short circuit evaulation if LHS is TRUE 108 if (bLhs == Result.RESULT_TRUE) 109 return Result.RESULT_TRUE; 110 111 Result bRhs = (Result) rhs.eval(provider, corr); 112 if (bRhs == Result.RESULT_TRUE) 113 return Result.RESULT_TRUE; 114 else if (bLhs == Result.RESULT_UNKNOWN 115 || bRhs == Result.RESULT_UNKNOWN) 116 return Result.RESULT_UNKNOWN; 117 else 118 return Result.RESULT_FALSE; 119 } 120 }; 121 122 /*** 123 * NOT operator. 124 */ 125 public static final ConditionalOpImpl NOT = new ConditionalOpImpl(" NOT ") 126 { 127 public Object eval(final Map identifiers, final IExpression lhs, 128 final IExpression rhs) 129 { 130 Result bRhs = (Result) rhs.eval(identifiers); 131 if (bRhs == Result.RESULT_TRUE) 132 return Result.RESULT_FALSE; 133 else if (bRhs == Result.RESULT_FALSE) 134 return Result.RESULT_TRUE; 135 else 136 return Result.RESULT_UNKNOWN; 137 } 138 139 public Object eval(final IValueProvider provider, final Object corr, 140 final IExpression lhs, final IExpression rhs) 141 { 142 Result bRhs = (Result) rhs.eval(provider, corr); 143 if (bRhs == Result.RESULT_TRUE) 144 return Result.RESULT_FALSE; 145 else if (bRhs == Result.RESULT_FALSE) 146 return Result.RESULT_TRUE; 147 else 148 return Result.RESULT_UNKNOWN; 149 } 150 }; 151 152 private final String operator_; 153 }

This page was automatically generated by Maven