You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by ye...@apache.org on 2013/06/02 17:30:58 UTC

svn commit: r1488730 - in /poi/trunk/src: java/org/apache/poi/ss/formula/functions/Delta.java testcases/org/apache/poi/ss/formula/functions/TestDelta.java

Author: yegor
Date: Sun Jun  2 15:30:58 2013
New Revision: 1488730

URL: http://svn.apache.org/r1488730
Log:
Bugzilla 55037: DELTA function support

Added:
    poi/trunk/src/java/org/apache/poi/ss/formula/functions/Delta.java
    poi/trunk/src/testcases/org/apache/poi/ss/formula/functions/TestDelta.java

Added: poi/trunk/src/java/org/apache/poi/ss/formula/functions/Delta.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/ss/formula/functions/Delta.java?rev=1488730&view=auto
==============================================================================
--- poi/trunk/src/java/org/apache/poi/ss/formula/functions/Delta.java (added)
+++ poi/trunk/src/java/org/apache/poi/ss/formula/functions/Delta.java Sun Jun  2 15:30:58 2013
@@ -0,0 +1,76 @@
+/* ====================================================================
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You under the Apache License, Version 2.0
+   (the "License"); you may not use this file except in compliance with
+   the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+==================================================================== */
+
+package org.apache.poi.ss.formula.functions;
+
+import org.apache.poi.ss.formula.eval.*;
+
+import java.math.BigDecimal;
+
+/**
+ * Implementation for Excel DELTA() function.<p/>
+ * <p/>
+ * <b>Syntax</b>:<br/> <b>DELTA </b>(<b>number1</b>,<b>number2</b> )<br/>
+ * <p/>
+ * Tests whether two values are equal. Returns 1 if number1 = number2; returns 0 otherwise.
+ * Use this function to filter a set of values. For example, by summing several DELTA functions
+ * you calculate the count of equal pairs. This function is also known as the Kronecker Delta function.
+ *
+ * <ul>
+ *     <li>If number1 is nonnumeric, DELTA returns the #VALUE! error value.</li>
+ *     <li>If number2 is nonnumeric, DELTA returns the #VALUE! error value.</li>
+ * </ul>
+ *
+ * @author cedric dot walter @ gmail dot com
+ */
+public final class Delta extends Fixed2ArgFunction {
+
+    private final static NumberEval ONE = new NumberEval(1);
+    private final static NumberEval ZERO = new NumberEval(0);
+
+    @Override
+    public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg1, ValueEval arg2) {
+        ValueEval veText1;
+        try {
+            veText1 = OperandResolver.getSingleValue(arg1, srcRowIndex, srcColumnIndex);
+        } catch (EvaluationException e) {
+            return e.getErrorEval();
+        }
+        String strText1 = OperandResolver.coerceValueToString(veText1);
+        Double number1 = OperandResolver.parseDouble(strText1);
+        if (number1 == null) {
+            return ErrorEval.VALUE_INVALID;
+        }
+
+        ValueEval veText2;
+        try {
+            veText2 = OperandResolver.getSingleValue(arg2, srcRowIndex, srcColumnIndex);
+        } catch (EvaluationException e) {
+            return e.getErrorEval();
+        }
+
+        String strText2 = OperandResolver.coerceValueToString(veText2);
+        Double number2 = OperandResolver.parseDouble(strText2);
+        if (number2 == null) {
+            return ErrorEval.VALUE_INVALID;
+        }
+
+        int result = new BigDecimal(number1.doubleValue()).compareTo(new BigDecimal(number2.doubleValue()));
+        return result == 0 ? ONE : ZERO;
+    }
+
+}
\ No newline at end of file

Added: poi/trunk/src/testcases/org/apache/poi/ss/formula/functions/TestDelta.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/ss/formula/functions/TestDelta.java?rev=1488730&view=auto
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/ss/formula/functions/TestDelta.java (added)
+++ poi/trunk/src/testcases/org/apache/poi/ss/formula/functions/TestDelta.java Sun Jun  2 15:30:58 2013
@@ -0,0 +1,63 @@
+/* ====================================================================
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You under the Apache License, Version 2.0
+   (the "License"); you may not use this file except in compliance with
+   the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+==================================================================== */
+
+package org.apache.poi.ss.formula.functions;
+
+import junit.framework.TestCase;
+import org.apache.poi.ss.formula.eval.ErrorEval;
+import org.apache.poi.ss.formula.eval.NumberEval;
+import org.apache.poi.ss.formula.eval.StringEval;
+import org.apache.poi.ss.formula.eval.ValueEval;
+
+/**
+ * Tests for {@link org.apache.poi.ss.formula.functions.Delta}
+ *
+ * @author cedric dot walter @ gmail dot com
+ */
+public final class TestDelta extends TestCase {
+
+	private static ValueEval invokeValue(String number1, String number2) {
+		ValueEval[] args = new ValueEval[] { new StringEval(number1), new StringEval(number2), };
+		return new Delta().evaluate(args, -1, -1);
+	}
+
+	private static void confirmValue(String number1, String number2, double expected) {
+		ValueEval result = invokeValue(number1, number2);
+		assertEquals(NumberEval.class, result.getClass());
+		assertEquals(expected, ((NumberEval) result).getNumberValue(), 0.0);
+	}
+
+    private static void confirmValueError(String number1, String number2) {
+        ValueEval result = invokeValue(number1, number2);
+        assertEquals(ErrorEval.class, result.getClass());
+        assertEquals(ErrorEval.VALUE_INVALID, result);
+    }
+
+	public void testBasic() {
+		confirmValue("5","4", 0); // Checks whether 5 equals 4 (0)
+		confirmValue("5","5", 1); // Checks whether 5 equals 5 (1)
+
+        confirmValue("0.5","0", 0); // Checks whether 0.5 equals 0 (0)
+        confirmValue("0.50","0.5", 1);
+        confirmValue("0.5000000000","0.5", 1);
+	}
+
+    public void testErrors() {
+        confirmValueError("A1","B2");
+        confirmValueError("AAAA","BBBB");
+    }
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org