--- /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 ImReal() function.<p/>\r
+ * <p/>\r
+ * <b>Syntax</b>:<br/> <b>ImReal </b>(<b>Inumber</b>)<br/>\r
+ * <p/>\r
+ * Returns the real coefficient of a complex number in x + yi or x + yj text format.\r
+ * <p/>\r
+ * Inumber A complex number for which you want the real coefficient.\r
+ * <p/>\r
+ * Remarks\r
+ * <ul>\r
+ * <li>If inumber is not in the form x + yi or x + yj, this function returns the #NUM! error value.</li>\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 ImReal extends Fixed1ArgFunction implements FreeRefFunction {\r
+\r
+ public static final FreeRefFunction instance = new ImReal();\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 = Imaginary.COMPLEX_NUMBER_PATTERN.matcher(iNumber);\r
+ boolean result = m.matches();\r
+\r
+ String real = "";\r
+ if (result == true) {\r
+ String realGroup = m.group(2);\r
+ boolean hasRealPart = realGroup.length() != 0;\r
+\r
+ if (realGroup.length() == 0) {\r
+ return new StringEval(String.valueOf(0));\r
+ }\r
+\r
+ if (hasRealPart) {\r
+ String sign = "";\r
+ String realSign = m.group(Imaginary.GROUP1_REAL_SIGN);\r
+ if (realSign.length() != 0 && !(realSign.equals("+"))) {\r
+ sign = realSign;\r
+ }\r
+\r
+ String groupRealNumber = m.group(Imaginary.GROUP2_IMAGINARY_INTEGER_OR_DOUBLE);\r
+ if (groupRealNumber.length() != 0) {\r
+ real = sign + groupRealNumber;\r
+ } else {\r
+ real = sign + "1";\r
+ }\r
+ }\r
+ } else {\r
+ return ErrorEval.NUM_ERROR;\r
+ }\r
+\r
+ return new StringEval(real);\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 ImReal() as loaded from a test data spreadsheet.<p/>\r
+ *\r
+ * @author cedric dot walter @ gmail dot com\r
+ */\r
+public class TestImRealFunctionsFromSpreadsheet extends BaseTestFunctionsFromSpreadsheet {\r
+\r
+ protected String getFilename() {\r
+ return "ImRealFunctionTestCaseData.xls";\r
+ }\r
+}\r