From f43a8cf5b72d63a7564613bae4506b1720e1332d Mon Sep 17 00:00:00 2001
From: Huiqing Xu <3x3h3q@gmail.com>
Date: Wed, 24 Jul 2013 13:09:44 +0800
Subject: [PATCH 1/2] in ms excel, 100>"23" , "100">"23" will be converted to
Number to compare, hope poi do it the same
---
.../formula/eval/RelationalOperationEval.java | 263 +++++++++---------
1 file changed, 132 insertions(+), 131 deletions(-)
diff --git a/src/java/org/apache/poi/ss/formula/eval/RelationalOperationEval.java b/src/java/org/apache/poi/ss/formula/eval/RelationalOperationEval.java
index 8b3be191994..db17327b7f0 100644
--- a/src/java/org/apache/poi/ss/formula/eval/RelationalOperationEval.java
+++ b/src/java/org/apache/poi/ss/formula/eval/RelationalOperationEval.java
@@ -28,141 +28,142 @@ Licensed to the Apache Software Foundation (ASF) under one or more
*/
public abstract class RelationalOperationEval extends Fixed2ArgFunction {
- /**
- * Converts a standard compare result (-1, 0, 1) to true
or false
- * according to subclass' comparison type.
- */
- protected abstract boolean convertComparisonResult(int cmpResult);
+ /**
+ * Converts a standard compare result (-1, 0, 1) to true
or false
+ * according to subclass' comparison type.
+ */
+ protected abstract boolean convertComparisonResult(int cmpResult);
- /**
- * This is a description of how the relational operators apply in MS Excel.
- * Use this as a guideline when testing/implementing the evaluate methods
- * for the relational operators Evals.
- *
- *
- * Bool.TRUE > any number. - * Bool > any string. ALWAYS - * Bool.TRUE > Bool.FALSE - * Bool.FALSE == Blank - * - * Strings are never converted to numbers or booleans - * String > any number. ALWAYS - * Non-empty String > Blank - * Empty String == Blank - * String are sorted dictionary wise - * - * Blank > Negative numbers - * Blank == 0 - * Blank < Positive numbers - *- */ - public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, ValueEval arg1) { + /** + * This is a description of how the relational operators apply in MS Excel. + * Use this as a guideline when testing/implementing the evaluate methods + * for the relational operators Evals. + * + *
+ * Bool.TRUE > any number. + * Bool > any string. ALWAYS + * Bool.TRUE > Bool.FALSE + * Bool.FALSE == Blank + * + * Strings can converted to numbers or booleans to compare + * Non-empty String > Blank + * Empty String == Blank + * String are sorted dictionary wise + * + * Blank > Negative numbers + * Blank == 0 + * Blank < Positive numbers + *+ */ + public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, ValueEval arg1) { - ValueEval vA; - ValueEval vB; - try { - vA = OperandResolver.getSingleValue(arg0, srcRowIndex, srcColumnIndex); - vB = OperandResolver.getSingleValue(arg1, srcRowIndex, srcColumnIndex); - } catch (EvaluationException e) { - return e.getErrorEval(); - } - int cmpResult = doCompare(vA, vB); - boolean result = convertComparisonResult(cmpResult); - return BoolEval.valueOf(result); - } + ValueEval vA; + ValueEval vB; + try { + vA = OperandResolver.getSingleValue(arg0, srcRowIndex, srcColumnIndex); + vB = OperandResolver.getSingleValue(arg1, srcRowIndex, srcColumnIndex); + } catch (EvaluationException e) { + return e.getErrorEval(); + } + int cmpResult = doCompare(vA, vB); + boolean result = convertComparisonResult(cmpResult); + return BoolEval.valueOf(result); + } - private static int doCompare(ValueEval va, ValueEval vb) { - // special cases when one operand is blank - if (va == BlankEval.instance) { - return compareBlank(vb); - } - if (vb == BlankEval.instance) { - return -compareBlank(va); - } + private static int doCompare(ValueEval va, ValueEval vb) { + // special cases when one operand is blank + if (va == BlankEval.instance) { + return compareBlank(vb); + } + if (vb == BlankEval.instance) { + return -compareBlank(va); + } - if (va instanceof BoolEval) { - if (vb instanceof BoolEval) { - BoolEval bA = (BoolEval) va; - BoolEval bB = (BoolEval) vb; - if (bA.getBooleanValue() == bB.getBooleanValue()) { - return 0; - } - return bA.getBooleanValue() ? 1 : -1; - } - return 1; - } - if (vb instanceof BoolEval) { - return -1; - } - if (va instanceof StringEval) { - if (vb instanceof StringEval) { - StringEval sA = (StringEval) va; - StringEval sB = (StringEval) vb; - return sA.getStringValue().compareToIgnoreCase(sB.getStringValue()); - } - return 1; - } - if (vb instanceof StringEval) { - return -1; - } - if (va instanceof NumberEval) { - if (vb instanceof NumberEval) { - NumberEval nA = (NumberEval) va; - NumberEval nB = (NumberEval) vb; - return NumberComparer.compare(nA.getNumberValue(), nB.getNumberValue()); - } - } - throw new IllegalArgumentException("Bad operand types (" + va.getClass().getName() + "), (" - + vb.getClass().getName() + ")"); - } + if (va instanceof BoolEval) { + if (vb instanceof BoolEval) { + BoolEval bA = (BoolEval) va; + BoolEval bB = (BoolEval) vb; + if (bA.getBooleanValue() == bB.getBooleanValue()) { + return 0; + } + return bA.getBooleanValue() ? 1 : -1; + } + return 1; + } + if (vb instanceof BoolEval) { + return -1; + } - private static int compareBlank(ValueEval v) { - if (v == BlankEval.instance) { - return 0; - } - if (v instanceof BoolEval) { - BoolEval boolEval = (BoolEval) v; - return boolEval.getBooleanValue() ? -1 : 0; - } - if (v instanceof NumberEval) { - NumberEval ne = (NumberEval) v; - return NumberComparer.compare(0.0, ne.getNumberValue()); - } - if (v instanceof StringEval) { - StringEval se = (StringEval) v; - return se.getStringValue().length() < 1 ? 0 : -1; - } - throw new IllegalArgumentException("bad value class (" + v.getClass().getName() + ")"); - } + try { + Double nA = OperandResolver.coerceValueToDouble(va); + Double nB = OperandResolver.coerceValueToDouble(vb); + return NumberComparer.compare(nA, nB); + } catch (Exception e) { + // + } - public static final Function EqualEval = new RelationalOperationEval() { - protected boolean convertComparisonResult(int cmpResult) { - return cmpResult == 0; - } - }; - public static final Function GreaterEqualEval = new RelationalOperationEval() { - protected boolean convertComparisonResult(int cmpResult) { - return cmpResult >= 0; - } - }; - public static final Function GreaterThanEval = new RelationalOperationEval() { - protected boolean convertComparisonResult(int cmpResult) { - return cmpResult > 0; - } - }; - public static final Function LessEqualEval = new RelationalOperationEval() { - protected boolean convertComparisonResult(int cmpResult) { - return cmpResult <= 0; - } - }; - public static final Function LessThanEval = new RelationalOperationEval() { - protected boolean convertComparisonResult(int cmpResult) { - return cmpResult < 0; - } - }; - public static final Function NotEqualEval = new RelationalOperationEval() { - protected boolean convertComparisonResult(int cmpResult) { - return cmpResult != 0; - } - }; + if (va instanceof StringEval) { + if (vb instanceof StringEval) { + StringEval sA = (StringEval) va; + StringEval sB = (StringEval) vb; + return sA.getStringValue().compareToIgnoreCase(sB.getStringValue()); + } + return 1; + } + if (vb instanceof StringEval) { + return -1; + } + throw new IllegalArgumentException("Bad operand types (" + va.getClass().getName() + "), (" + + vb.getClass().getName() + ")"); + } + + private static int compareBlank(ValueEval v) { + if (v == BlankEval.instance) { + return 0; + } + if (v instanceof BoolEval) { + BoolEval boolEval = (BoolEval) v; + return boolEval.getBooleanValue() ? -1 : 0; + } + if (v instanceof NumberEval) { + NumberEval ne = (NumberEval) v; + return NumberComparer.compare(0.0, ne.getNumberValue()); + } + if (v instanceof StringEval) { + StringEval se = (StringEval) v; + return se.getStringValue().length() < 1 ? 0 : -1; + } + throw new IllegalArgumentException("bad value class (" + v.getClass().getName() + ")"); + } + + public static final Function EqualEval = new RelationalOperationEval() { + protected boolean convertComparisonResult(int cmpResult) { + return cmpResult == 0; + } + }; + public static final Function GreaterEqualEval = new RelationalOperationEval() { + protected boolean convertComparisonResult(int cmpResult) { + return cmpResult >= 0; + } + }; + public static final Function GreaterThanEval = new RelationalOperationEval() { + protected boolean convertComparisonResult(int cmpResult) { + return cmpResult > 0; + } + }; + public static final Function LessEqualEval = new RelationalOperationEval() { + protected boolean convertComparisonResult(int cmpResult) { + return cmpResult <= 0; + } + }; + public static final Function LessThanEval = new RelationalOperationEval() { + protected boolean convertComparisonResult(int cmpResult) { + return cmpResult < 0; + } + }; + public static final Function NotEqualEval = new RelationalOperationEval() { + protected boolean convertComparisonResult(int cmpResult) { + return cmpResult != 0; + } + }; } From 473c8c9794796d10816d4f07ecf0897e9213f01c Mon Sep 17 00:00:00 2001 From: Huiqing Xu <3x3h3q@gmail.com> Date: Wed, 24 Jul 2013 13:17:25 +0800 Subject: [PATCH 2/2] format style the same as poi --- .../formula/eval/RelationalOperationEval.java | 253 +++++++++--------- 1 file changed, 127 insertions(+), 126 deletions(-) diff --git a/src/java/org/apache/poi/ss/formula/eval/RelationalOperationEval.java b/src/java/org/apache/poi/ss/formula/eval/RelationalOperationEval.java index db17327b7f0..813c585584c 100644 --- a/src/java/org/apache/poi/ss/formula/eval/RelationalOperationEval.java +++ b/src/java/org/apache/poi/ss/formula/eval/RelationalOperationEval.java @@ -28,142 +28,143 @@ Licensed to the Apache Software Foundation (ASF) under one or more */ public abstract class RelationalOperationEval extends Fixed2ArgFunction { - /** - * Converts a standard compare result (-1, 0, 1) to
true
or false
- * according to subclass' comparison type.
- */
- protected abstract boolean convertComparisonResult(int cmpResult);
+ /**
+ * Converts a standard compare result (-1, 0, 1) to true
or false
+ * according to subclass' comparison type.
+ */
+ protected abstract boolean convertComparisonResult(int cmpResult);
- /**
- * This is a description of how the relational operators apply in MS Excel.
- * Use this as a guideline when testing/implementing the evaluate methods
- * for the relational operators Evals.
- *
- * - * Bool.TRUE > any number. - * Bool > any string. ALWAYS - * Bool.TRUE > Bool.FALSE - * Bool.FALSE == Blank - * - * Strings can converted to numbers or booleans to compare - * Non-empty String > Blank - * Empty String == Blank - * String are sorted dictionary wise - * - * Blank > Negative numbers - * Blank == 0 - * Blank < Positive numbers - *- */ - public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, ValueEval arg1) { + /** + * This is a description of how the relational operators apply in MS Excel. + * Use this as a guideline when testing/implementing the evaluate methods + * for the relational operators Evals. + * + *
+ * Bool.TRUE > any number. + * Bool > any string. ALWAYS + * Bool.TRUE > Bool.FALSE + * Bool.FALSE == Blank + * + * Strings can converted to numbers or booleans to compare + * String > any number. ALWAYS + * Non-empty String > Blank + * Empty String == Blank + * String are sorted dictionary wise + * + * Blank > Negative numbers + * Blank == 0 + * Blank < Positive numbers + *+ */ + public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, ValueEval arg1) { - ValueEval vA; - ValueEval vB; - try { - vA = OperandResolver.getSingleValue(arg0, srcRowIndex, srcColumnIndex); - vB = OperandResolver.getSingleValue(arg1, srcRowIndex, srcColumnIndex); - } catch (EvaluationException e) { - return e.getErrorEval(); - } - int cmpResult = doCompare(vA, vB); - boolean result = convertComparisonResult(cmpResult); - return BoolEval.valueOf(result); - } + ValueEval vA; + ValueEval vB; + try { + vA = OperandResolver.getSingleValue(arg0, srcRowIndex, srcColumnIndex); + vB = OperandResolver.getSingleValue(arg1, srcRowIndex, srcColumnIndex); + } catch (EvaluationException e) { + return e.getErrorEval(); + } + int cmpResult = doCompare(vA, vB); + boolean result = convertComparisonResult(cmpResult); + return BoolEval.valueOf(result); + } - private static int doCompare(ValueEval va, ValueEval vb) { - // special cases when one operand is blank - if (va == BlankEval.instance) { - return compareBlank(vb); - } - if (vb == BlankEval.instance) { - return -compareBlank(va); - } - - if (va instanceof BoolEval) { - if (vb instanceof BoolEval) { - BoolEval bA = (BoolEval) va; - BoolEval bB = (BoolEval) vb; - if (bA.getBooleanValue() == bB.getBooleanValue()) { - return 0; - } - return bA.getBooleanValue() ? 1 : -1; - } - return 1; - } - if (vb instanceof BoolEval) { - return -1; - } + private static int doCompare(ValueEval va, ValueEval vb) { + // special cases when one operand is blank + if (va == BlankEval.instance) { + return compareBlank(vb); + } + if (vb == BlankEval.instance) { + return -compareBlank(va); + } - try { + if (va instanceof BoolEval) { + if (vb instanceof BoolEval) { + BoolEval bA = (BoolEval) va; + BoolEval bB = (BoolEval) vb; + if (bA.getBooleanValue() == bB.getBooleanValue()) { + return 0; + } + return bA.getBooleanValue() ? 1 : -1; + } + return 1; + } + if (vb instanceof BoolEval) { + return -1; + } + + try { Double nA = OperandResolver.coerceValueToDouble(va); Double nB = OperandResolver.coerceValueToDouble(vb); return NumberComparer.compare(nA, nB); } catch (Exception e) { // } + + if (va instanceof StringEval) { + if (vb instanceof StringEval) { + StringEval sA = (StringEval) va; + StringEval sB = (StringEval) vb; + return sA.getStringValue().compareToIgnoreCase(sB.getStringValue()); + } + return 1; + } + if (vb instanceof StringEval) { + return -1; + } + throw new IllegalArgumentException("Bad operand types (" + va.getClass().getName() + "), (" + + vb.getClass().getName() + ")"); + } - if (va instanceof StringEval) { - if (vb instanceof StringEval) { - StringEval sA = (StringEval) va; - StringEval sB = (StringEval) vb; - return sA.getStringValue().compareToIgnoreCase(sB.getStringValue()); - } - return 1; - } - if (vb instanceof StringEval) { - return -1; - } - throw new IllegalArgumentException("Bad operand types (" + va.getClass().getName() + "), (" - + vb.getClass().getName() + ")"); - } + private static int compareBlank(ValueEval v) { + if (v == BlankEval.instance) { + return 0; + } + if (v instanceof BoolEval) { + BoolEval boolEval = (BoolEval) v; + return boolEval.getBooleanValue() ? -1 : 0; + } + if (v instanceof NumberEval) { + NumberEval ne = (NumberEval) v; + return NumberComparer.compare(0.0, ne.getNumberValue()); + } + if (v instanceof StringEval) { + StringEval se = (StringEval) v; + return se.getStringValue().length() < 1 ? 0 : -1; + } + throw new IllegalArgumentException("bad value class (" + v.getClass().getName() + ")"); + } - private static int compareBlank(ValueEval v) { - if (v == BlankEval.instance) { - return 0; - } - if (v instanceof BoolEval) { - BoolEval boolEval = (BoolEval) v; - return boolEval.getBooleanValue() ? -1 : 0; - } - if (v instanceof NumberEval) { - NumberEval ne = (NumberEval) v; - return NumberComparer.compare(0.0, ne.getNumberValue()); - } - if (v instanceof StringEval) { - StringEval se = (StringEval) v; - return se.getStringValue().length() < 1 ? 0 : -1; - } - throw new IllegalArgumentException("bad value class (" + v.getClass().getName() + ")"); - } - - public static final Function EqualEval = new RelationalOperationEval() { - protected boolean convertComparisonResult(int cmpResult) { - return cmpResult == 0; - } - }; - public static final Function GreaterEqualEval = new RelationalOperationEval() { - protected boolean convertComparisonResult(int cmpResult) { - return cmpResult >= 0; - } - }; - public static final Function GreaterThanEval = new RelationalOperationEval() { - protected boolean convertComparisonResult(int cmpResult) { - return cmpResult > 0; - } - }; - public static final Function LessEqualEval = new RelationalOperationEval() { - protected boolean convertComparisonResult(int cmpResult) { - return cmpResult <= 0; - } - }; - public static final Function LessThanEval = new RelationalOperationEval() { - protected boolean convertComparisonResult(int cmpResult) { - return cmpResult < 0; - } - }; - public static final Function NotEqualEval = new RelationalOperationEval() { - protected boolean convertComparisonResult(int cmpResult) { - return cmpResult != 0; - } - }; + public static final Function EqualEval = new RelationalOperationEval() { + protected boolean convertComparisonResult(int cmpResult) { + return cmpResult == 0; + } + }; + public static final Function GreaterEqualEval = new RelationalOperationEval() { + protected boolean convertComparisonResult(int cmpResult) { + return cmpResult >= 0; + } + }; + public static final Function GreaterThanEval = new RelationalOperationEval() { + protected boolean convertComparisonResult(int cmpResult) { + return cmpResult > 0; + } + }; + public static final Function LessEqualEval = new RelationalOperationEval() { + protected boolean convertComparisonResult(int cmpResult) { + return cmpResult <= 0; + } + }; + public static final Function LessThanEval = new RelationalOperationEval() { + protected boolean convertComparisonResult(int cmpResult) { + return cmpResult < 0; + } + }; + public static final Function NotEqualEval = new RelationalOperationEval() { + protected boolean convertComparisonResult(int cmpResult) { + return cmpResult != 0; + } + }; }