1 61 62 63 64 package org.jaxen.expr; 65 66 import java.util.Iterator ; 67 import java.util.List ; 68 import org.jaxen.Context; 69 import org.jaxen.JaxenException; 70 import org.jaxen.Navigator; 71 import org.jaxen.function.NumberFunction; 72 73 abstract class DefaultRelationalExpr extends DefaultTruthExpr implements RelationalExpr 74 { 75 DefaultRelationalExpr( Expr lhs, Expr rhs ) 76 { 77 super( lhs, rhs ); 78 } 79 80 public String toString() 81 { 82 return "[(DefaultRelationalExpr): " + getLHS() + ", " + getRHS() + "]"; 83 } 84 85 public Object evaluate( Context context ) throws JaxenException 86 { 87 Object lhsValue = getLHS().evaluate( context ); 88 Object rhsValue = getRHS().evaluate( context ); 89 Navigator nav = context.getNavigator(); 90 91 if( bothAreSets( lhsValue, rhsValue ) ) 92 { 93 return evaluateSetSet( (List ) lhsValue, (List ) rhsValue, nav ); 94 } 95 96 if( eitherIsSet( lhsValue, rhsValue ) ) 97 { 98 if( isSet( lhsValue ) ) 99 { 100 return evaluateSetSet( (List ) lhsValue, convertToList( rhsValue ), nav ); 101 } 102 else 103 { 104 return evaluateSetSet( convertToList( lhsValue ), (List ) rhsValue, nav ); 105 } 106 } 107 108 return evaluateObjectObject( lhsValue, rhsValue, nav ) ? Boolean.TRUE : Boolean.FALSE; 109 } 110 111 private Object evaluateSetSet( List lhsSet, List rhsSet, Navigator nav ) 112 { 113 if( setIsEmpty( lhsSet ) || setIsEmpty( rhsSet ) ) { 115 return Boolean.FALSE; 116 } 117 118 for( Iterator lhsIterator = lhsSet.iterator(); lhsIterator.hasNext(); ) 119 { 120 Object lhs = lhsIterator.next(); 121 122 for( Iterator rhsIterator = rhsSet.iterator(); rhsIterator.hasNext(); ) 123 { 124 Object rhs = rhsIterator.next(); 125 126 if( evaluateObjectObject( lhs, rhs, nav ) ) 127 { 128 return Boolean.TRUE; 129 } 130 } 131 } 132 133 return Boolean.FALSE; 134 } 135 136 private boolean evaluateObjectObject( Object lhs, Object rhs, Navigator nav ) 137 { 138 if( lhs == null || rhs == null ) 139 { 140 return false; 141 } 142 143 Double lhsNum = NumberFunction.evaluate( lhs, nav ); 144 Double rhsNum = NumberFunction.evaluate( rhs, nav ); 145 146 if( NumberFunction.isNaN( lhsNum ) || NumberFunction.isNaN( rhsNum ) ) 147 { 148 return false; 149 } 150 151 return evaluateDoubleDouble( lhsNum, rhsNum ); 152 } 153 154 protected abstract boolean evaluateDoubleDouble( Double lhs, Double rhs ); 155 } 156 157 | Popular Tags |