]> source.dussan.org Git - poi.git/commitdiff
Bug 55079: patch for missing function IMAGINARY
authorCédric Walter <cedricwalter@apache.org>
Fri, 16 Aug 2013 17:59:01 +0000 (17:59 +0000)
committerCédric Walter <cedricwalter@apache.org>
Fri, 16 Aug 2013 17:59:01 +0000 (17:59 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1514824 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/ss/formula/atp/AnalysisToolPak.java
src/java/org/apache/poi/ss/formula/functions/Imaginary.java [new file with mode: 0644]
src/testcases/org/apache/poi/ss/formula/functions/TestImaginaryFunctionsFromSpreadsheet.java [new file with mode: 0644]
test-data/spreadsheet/FormulaEvalTestData.xls
test-data/spreadsheet/ImaginaryFunctionTestCaseData.xls [new file with mode: 0644]

index 373b653827e133fc8a169afac320d18742cacd2f..f91cbfc798aad314a47a17a8e682eb602283a8ed 100644 (file)
@@ -112,7 +112,7 @@ public final class AnalysisToolPak implements UDFFinder {
         r(m, "HEX2OCT", null);
         r(m, "IFERROR", IfError.instance);
         r(m, "IMABS", null);
-        r(m, "IMAGINARY", null);
+        r(m, "IMAGINARY", Imaginary.instance);
         r(m, "IMARGUMENT", null);
         r(m, "IMCONJUGATE", null);
         r(m, "IMCOS", null);
diff --git a/src/java/org/apache/poi/ss/formula/functions/Imaginary.java b/src/java/org/apache/poi/ss/formula/functions/Imaginary.java
new file mode 100644 (file)
index 0000000..2b13093
--- /dev/null
@@ -0,0 +1,109 @@
+/* ====================================================================\r
+   Licensed to the Apache Software Foundation (ASF) under one or more\r
+   contributor license agreements.  See the NOTICE file distributed with\r
+   this work for additional information regarding copyright ownership.\r
+   The ASF licenses this file to You under the Apache License, Version 2.0\r
+   (the "License"); you may not use this file except in compliance with\r
+   the License.  You may obtain a copy of the License at\r
+\r
+       http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+   Unless required by applicable law or agreed to in writing, software\r
+   distributed under the License is distributed on an "AS IS" BASIS,\r
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+   See the License for the specific language governing permissions and\r
+   limitations under the License.\r
+==================================================================== */\r
+\r
+package org.apache.poi.ss.formula.functions;\r
+\r
+import org.apache.poi.ss.formula.OperationEvaluationContext;\r
+import org.apache.poi.ss.formula.eval.*;\r
+\r
+import java.util.regex.Matcher;\r
+import java.util.regex.Pattern;\r
+\r
+/**\r
+ * Implementation for Excel IMAGINARY() function.<p/>\r
+ * <p/>\r
+ * <b>Syntax</b>:<br/> <b>IMAGINARY  </b>(<b>Inumber</b>)<br/>\r
+ * <p/>\r
+ * Returns the imaginary coefficient of a complex number in x + yi or x + yj text format.\r
+ * <p/>\r
+ * Inumber     is a complex number for which you want the imaginary coefficient.\r
+ * <p/>\r
+ * Remarks\r
+ * <ul>\r
+ * <li>Use COMPLEX to convert real and imaginary coefficients into a complex number.</li>\r
+ * </ul>\r
+ *\r
+ * @author cedric dot walter @ gmail dot com\r
+ */\r
+public class Imaginary extends Fixed1ArgFunction implements FreeRefFunction {\r
+\r
+    public static final FreeRefFunction instance = new Imaginary();\r
+\r
+    public static final String GROUP1_REAL_SIGN_REGEX = "([+-]?)";\r
+    public static final String GROUP2_REAL_INTEGER_OR_DOUBLE_REGEX = "([0-9]+\\.[0-9]+|[0-9]*)";\r
+    public static final String GROUP3_IMAGINARY_SIGN_REGEX = "([+-]?)";\r
+    public static final String GROUP4_IMAGINARY_INTEGER_OR_DOUBLE_REGEX = "([0-9]+\\.[0-9]+|[0-9]*)";\r
+    public static final String GROUP5_IMAGINARY_GROUP_REGEX = "([ij]?)";\r
+\r
+    public static final Pattern COMPLEX_NUMBER_PATTERN\r
+            = Pattern.compile(GROUP1_REAL_SIGN_REGEX + GROUP2_REAL_INTEGER_OR_DOUBLE_REGEX +\r
+            GROUP3_IMAGINARY_SIGN_REGEX + GROUP4_IMAGINARY_INTEGER_OR_DOUBLE_REGEX + GROUP5_IMAGINARY_GROUP_REGEX);\r
+\r
+    public static final int GROUP1_REAL_SIGN = 1;\r
+    public static final int GROUP2_IMAGINARY_INTEGER_OR_DOUBLE = 2;\r
+    public static final int GROUP3_IMAGINARY_SIGN = 3;\r
+    public static final int GROUP4_IMAGINARY_INTEGER_OR_DOUBLE = 4;\r
+\r
+    public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval inumberVE) {\r
+        ValueEval veText1;\r
+        try {\r
+            veText1 = OperandResolver.getSingleValue(inumberVE, srcRowIndex, srcColumnIndex);\r
+        } catch (EvaluationException e) {\r
+            return e.getErrorEval();\r
+        }\r
+        String iNumber = OperandResolver.coerceValueToString(veText1);\r
+\r
+        Matcher m = COMPLEX_NUMBER_PATTERN.matcher(iNumber);\r
+        boolean result = m.matches();\r
+\r
+        String imaginary = "";\r
+        if (result == true) {\r
+            String imaginaryGroup = m.group(5);\r
+            boolean hasImaginaryPart = imaginaryGroup.equals("i") || imaginaryGroup.equals("j");\r
+\r
+            if (imaginaryGroup.length() == 0) {\r
+                return new StringEval(String.valueOf(0));\r
+            }\r
+\r
+            if (hasImaginaryPart) {\r
+                String sign = "";\r
+                String imaginarySign = m.group(GROUP3_IMAGINARY_SIGN);\r
+                if (imaginarySign.length() != 0 && !(imaginarySign.equals("+"))) {\r
+                    sign = imaginarySign;\r
+                }\r
+\r
+                String groupImaginaryNumber = m.group(GROUP4_IMAGINARY_INTEGER_OR_DOUBLE);\r
+                if (groupImaginaryNumber.length() != 0) {\r
+                    imaginary = sign + groupImaginaryNumber;\r
+                } else {\r
+                    imaginary = sign + "1";\r
+                }\r
+            }\r
+        } else {\r
+            return ErrorEval.NUM_ERROR;\r
+        }\r
+\r
+        return new StringEval(imaginary);\r
+    }\r
+\r
+    public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) {\r
+        if (args.length != 1) {\r
+            return ErrorEval.VALUE_INVALID;\r
+        }\r
+        return evaluate(ec.getRowIndex(), ec.getColumnIndex(), args[0]);\r
+    }\r
+}\r
diff --git a/src/testcases/org/apache/poi/ss/formula/functions/TestImaginaryFunctionsFromSpreadsheet.java b/src/testcases/org/apache/poi/ss/formula/functions/TestImaginaryFunctionsFromSpreadsheet.java
new file mode 100644 (file)
index 0000000..c5b74ac
--- /dev/null
@@ -0,0 +1,30 @@
+/* ====================================================================\r
+   Licensed to the Apache Software Foundation (ASF) under one or more\r
+   contributor license agreements.  See the NOTICE file distributed with\r
+   this work for additional information regarding copyright ownership.\r
+   The ASF licenses this file to You under the Apache License, Version 2.0\r
+   (the "License"); you may not use this file except in compliance with\r
+   the License.  You may obtain a copy of the License at\r
+\r
+       http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+   Unless required by applicable law or agreed to in writing, software\r
+   distributed under the License is distributed on an "AS IS" BASIS,\r
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+   See the License for the specific language governing permissions and\r
+   limitations under the License.\r
+==================================================================== */\r
+\r
+package org.apache.poi.ss.formula.functions;\r
+\r
+/**\r
+ * Tests Imaginary() as loaded from a test data spreadsheet.<p/>\r
+ *\r
+ * @author cedric dot walter @ gmail dot com\r
+ */\r
+public class TestImaginaryFunctionsFromSpreadsheet extends BaseTestFunctionsFromSpreadsheet {\r
+\r
+    protected String getFilename() {\r
+        return "ImaginaryFunctionTestCaseData.xls";\r
+    }\r
+}\r
index c585b823ef687f1e4bc9d0e3501e85602b5623b1..ff8e932ac1b65ed0f91ed6d42e2da5a1039f552b 100644 (file)
Binary files a/test-data/spreadsheet/FormulaEvalTestData.xls and b/test-data/spreadsheet/FormulaEvalTestData.xls differ
diff --git a/test-data/spreadsheet/ImaginaryFunctionTestCaseData.xls b/test-data/spreadsheet/ImaginaryFunctionTestCaseData.xls
new file mode 100644 (file)
index 0000000..ce079f3
Binary files /dev/null and b/test-data/spreadsheet/ImaginaryFunctionTestCaseData.xls differ