From 90b41cc7a3cbf7867b181f40f4312ab965785a67 Mon Sep 17 00:00:00 2001 From: "Amol S. Deshmukh" Date: Wed, 22 Jun 2005 20:46:55 +0000 Subject: [PATCH] implemented the NOT boolean function git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@353725 13f79535-47bb-0310-9956-ffa450edef68 --- .../hssf/record/formula/functions/Not.java | 101 +++++++++++++++++- 1 file changed, 97 insertions(+), 4 deletions(-) diff --git a/src/scratchpad/src/org/apache/poi/hssf/record/formula/functions/Not.java b/src/scratchpad/src/org/apache/poi/hssf/record/formula/functions/Not.java index 65893f5fba..0d8519d2ec 100644 --- a/src/scratchpad/src/org/apache/poi/hssf/record/formula/functions/Not.java +++ b/src/scratchpad/src/org/apache/poi/hssf/record/formula/functions/Not.java @@ -4,11 +4,104 @@ */ package org.apache.poi.hssf.record.formula.functions; +import org.apache.poi.hssf.record.formula.eval.AreaEval; +import org.apache.poi.hssf.record.formula.eval.BlankEval; +import org.apache.poi.hssf.record.formula.eval.BoolEval; +import org.apache.poi.hssf.record.formula.eval.ErrorEval; +import org.apache.poi.hssf.record.formula.eval.Eval; +import org.apache.poi.hssf.record.formula.eval.NumberEval; +import org.apache.poi.hssf.record.formula.eval.RefEval; +import org.apache.poi.hssf.record.formula.eval.StringEval; +import org.apache.poi.hssf.record.formula.eval.ValueEval; + /** - * @author - * + * @author Amol S. Deshmukh < amol at apache dot org > + * The NOT boolean function. Returns negation of specified value + * (treated as a boolean). If the specified arg is a number, + * then it is true <=> 'number is non-zero' */ -public class Not extends DefaultFunctionImpl { - +public class Not extends BooleanFunction { + + + public Eval evaluate(Eval[] operands, int srcRow, short srcCol) { + ValueEval retval = null; + boolean b = true; + ValueEval tempVe = null; + + switch (operands.length) { + default: + retval = ErrorEval.VALUE_INVALID; + break; + case 1: + if (operands[0] instanceof AreaEval) { + AreaEval ae = (AreaEval) operands[0]; + if (ae.isRow() && ae.containsColumn(srcCol)) { + ValueEval ve = ae.getValueAt(ae.getFirstRow(), srcCol); + tempVe = singleOperandEvaluate(ve); + } else if (ae.isColumn() && ae.containsRow(srcRow)) { + ValueEval ve = ae.getValueAt(srcRow, ae.getFirstColumn()); + tempVe = singleOperandEvaluate(ve); + } else { + retval = ErrorEval.VALUE_INVALID; + } + } + else { + tempVe = singleOperandEvaluate(operands[0]); + if (tempVe instanceof StringEval) { + retval = ErrorEval.VALUE_INVALID; + } + else if (tempVe instanceof ErrorEval) { + retval = tempVe; + } + } + } + + if (retval == null) { // if no error + if (tempVe instanceof BoolEval) { + b = b && ((BoolEval) tempVe).getBooleanValue(); + } + else if (tempVe instanceof StringEval) { + retval = ErrorEval.VALUE_INVALID; + } + else if (tempVe instanceof ErrorEval) { + retval = tempVe; + } + retval = b ? BoolEval.FALSE : BoolEval.TRUE; + } + + return retval; + } + + + protected ValueEval singleOperandEvaluate(Eval ve) { + ValueEval retval = ErrorEval.VALUE_INVALID; + if (ve instanceof RefEval) { + RefEval re = (RefEval) ve; + retval = singleOperandEvaluate(re.getInnerValueEval()); + } + else if (ve instanceof BoolEval) { + retval = (BoolEval) ve; + } + else if (ve instanceof NumberEval) { + NumberEval ne = (NumberEval) ve; + retval = ne.getNumberValue() != 0 ? BoolEval.TRUE : BoolEval.FALSE; + } + else if (ve instanceof StringEval) { + StringEval se = (StringEval) ve; + String str = se.getStringValue(); + retval = str.equalsIgnoreCase("true") + ? BoolEval.TRUE + : str.equalsIgnoreCase("false") + ? BoolEval.FALSE + : (ValueEval) ErrorEval.VALUE_INVALID; + } + else if (ve instanceof BlankEval) { + retval = BoolEval.FALSE; + } + else { + retval = ErrorEval.VALUE_INVALID; + } + return retval; + } } -- 2.39.5