--- /dev/null
+/* ====================================================================\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
--- /dev/null
+/* ====================================================================\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