/*******************************************************************************
*
* Copyright 2010 Alexandru Craciun, and individual contributors as indicated
* by the @authors tag.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 3 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
******************************************************************************/
/* Generated By:JJTree: Do not edit this line. ASTLogicalExpression.java Version 4.3 */
/* JavaCCOptions:MULTI=true,NODE_USES_PARSER=false,VISITOR=false,TRACK_TOKENS=false,NODE_PREFIX=AST,NODE_EXTENDS=,NODE_FACTORY=,SUPPORT_CLASS_VISIBILITY_PUBLIC=true */
package org.netxilia.spi.impl.formula.parser;
import org.netxilia.api.formula.IFormulaContext;
import org.netxilia.api.value.BooleanValue;
import org.netxilia.api.value.GenericValueType;
import org.netxilia.api.value.IGenericValue;
/**
* An AST node representing a binary logical expression.
*/
public class ASTLogicalExpression extends ASTBinaryExpression {
public ASTLogicalExpression(int id) {
super(id, "Logical");
}
public ASTLogicalExpression(FormulaParser p, int id) {
super(p, id, "Logical");
}
@Override
public IGenericValue eval(IFormulaContext context) {
ASTBaseNode nLeft = (ASTBaseNode) jjtGetChild(0);
ASTBaseNode nRight = (ASTBaseNode) jjtGetChild(1);
IGenericValue gvLeft = nLeft.eval(context);
IGenericValue gvRight = nRight.eval(context);
if (gvLeft.getValueType() == GenericValueType.ERROR) {
return gvLeft;
}
if (gvRight.getValueType() == GenericValueType.ERROR) {
return gvRight;
}
if (gvLeft.getValueType() != gvRight.getValueType()) {
// TODO: refine this? in Excel you can compare apples & oranges... does it make sense?
// ACR: should rather return false !?
return BooleanValue.FALSE;
}
boolean result;
if (gvLeft.getValueType() == GenericValueType.NUMBER) {
result = compare(gvLeft.getNumberValue(), gvRight.getNumberValue());
} else if (gvLeft.getValueType() == GenericValueType.BOOLEAN) {
result = compare(gvLeft.getBooleanValue(), gvRight.getBooleanValue());
} else if (gvLeft.getValueType() == GenericValueType.STRING) {
result = compare(gvLeft.getStringValue(), gvRight.getStringValue());
} else {
throw new IllegalStateException("BUG: Don't know how to compare " + gvLeft.getValueType());
}
return BooleanValue.valueOf(result);
}
// < LOGICAL_OPERATOR: "=" | "<" | "<=" | ">" | ">=" | "<>" | "!=" >
private <T extends Comparable<T>> boolean compare(T left, T right) {
if ("=".equals(operator)) {
return left.equals(right);
}
if ("<".equals(operator)) {
return left.compareTo(right) < 0;
}
if ("<=".equals(operator)) {
return left.compareTo(right) <= 0;
}
if (">".equals(operator)) {
return left.compareTo(right) > 0;
}
if (">=".equals(operator)) {
return left.compareTo(right) >= 0;
}
if ("<>".equals(operator) || "!=".equals(operator)) {
return !left.equals(right);
}
throw new IllegalStateException("BUG: Unknown operator " + operator);
}
}
/* JavaCC - OriginalChecksum=356fe7ccea5f65e8c905463f6abe5940 (do not edit this line) */