--- /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.eval.*;\r
+\r
+/**\r
+ * Implementation for Excel WeekNum() function.<p/>\r
+ * <p/>\r
+ * <b>Syntax</b>:<br/> <b>WeekNum </b>(<b>Serial_num</b>,<b>Return_type</b>)<br/>\r
+ * <p/>\r
+ * Returns a number that indicates where the week falls numerically within a year.\r
+ * <p/>\r
+ * <p/>\r
+ * Serial_num is a date within the week. Dates should be entered by using the DATE function,\r
+ * or as results of other formulas or functions. For example, use DATE(2008,5,23)\r
+ * for the 23rd day of May, 2008. Problems can occur if dates are entered as text.\r
+ * Return_type is a number that determines on which day the week begins. The default is 1.\r
+ * 1 Week begins on Sunday. Weekdays are numbered 1 through 7.\r
+ * 2 Week begins on Monday. Weekdays are numbered 1 through 7.\r
+ *\r
+ * @author cedric dot walter @ gmail dot com\r
+ */\r
+public class Roman extends Fixed2ArgFunction {\r
+\r
+ //M (1000), CM (900), D (500), CD (400), C (100), XC (90), L (50), XL (40), X (10), IX (9), V (5), IV (4) and I (1).\r
+ public static final int[] VALUES = new int[]{1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};\r
+ public static final String[] ROMAN = new String[]\r
+ {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};\r
+\r
+\r
+ public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval numberVE, ValueEval formVE) {\r
+ int number = 0;\r
+ try {\r
+ ValueEval ve = OperandResolver.getSingleValue(numberVE, srcRowIndex, srcColumnIndex);\r
+ number = OperandResolver.coerceValueToInt(ve);\r
+ } catch (EvaluationException e) {\r
+ return ErrorEval.VALUE_INVALID;\r
+ }\r
+ if (number < 0) {\r
+ return ErrorEval.VALUE_INVALID;\r
+ }\r
+ if (number > 3999) {\r
+ return ErrorEval.VALUE_INVALID;\r
+ }\r
+ if (number == 0) {\r
+ return new StringEval("");\r
+ }\r
+\r
+ int form = 0;\r
+ try {\r
+ ValueEval ve = OperandResolver.getSingleValue(formVE, srcRowIndex, srcColumnIndex);\r
+ form = OperandResolver.coerceValueToInt(ve);\r
+ } catch (EvaluationException e) {\r
+ return ErrorEval.NUM_ERROR;\r
+ }\r
+\r
+ if (form > 4 || form < 0) {\r
+ return ErrorEval.VALUE_INVALID;\r
+ }\r
+\r
+ String result = this.integerToRoman(number);\r
+\r
+ if (form == 0) {\r
+ return new StringEval(result);\r
+ }\r
+\r
+ return new StringEval(makeConcise(result, form));\r
+ }\r
+\r
+ /**\r
+ * Classic conversion.\r
+ *\r
+ * @param number\r
+ * @return\r
+ */\r
+ private String integerToRoman(int number) {\r
+ StringBuilder result = new StringBuilder();\r
+ for (int i = 0; i < 13; i++) {\r
+ while (number >= VALUES[i]) {\r
+ number -= VALUES[i];\r
+ result.append(ROMAN[i]);\r
+ }\r
+ }\r
+ return result.toString();\r
+ }\r
+\r
+ /**\r
+ * Use conversion rule to factor some parts and make them more concise\r
+ *\r
+ * @param result\r
+ * @param form\r
+ * @return\r
+ */\r
+ public String makeConcise(String result, int form) {\r
+ if (form > 0) {\r
+ result = result.replaceAll("XLV", "VL"); //45\r
+ result = result.replaceAll("XCV", "VC"); //95\r
+ result = result.replaceAll("CDL", "LD"); //450\r
+ result = result.replaceAll("CML", "LM"); //950\r
+ result = result.replaceAll("CMVC", "LMVL"); //995\r
+ }\r
+ if (form == 1) {\r
+ result = result.replaceAll("CDXC", "LDXL"); //490\r
+ result = result.replaceAll("CDVC", "LDVL"); //495\r
+ result = result.replaceAll("CMXC", "LMXL"); //990\r
+ result = result.replaceAll("XCIX", "VCIV"); //99\r
+ result = result.replaceAll("XLIX", "VLIV"); //49\r
+ }\r
+ if (form > 1) {\r
+ result = result.replaceAll("XLIX", "IL"); //49\r
+ result = result.replaceAll("XCIX", "IC"); //99\r
+ result = result.replaceAll("CDXC", "XD"); //490\r
+ result = result.replaceAll("CDVC", "XDV"); //495\r
+ result = result.replaceAll("CDIC", "XDIX"); //499\r
+ result = result.replaceAll("LMVL", "XMV"); //995\r
+ result = result.replaceAll("CMIC", "XMIX"); //999\r
+ result = result.replaceAll("CMXC", "XM"); // 990\r
+ }\r
+ if (form > 2) {\r
+ result = result.replaceAll("XDV", "VD"); //495\r
+ result = result.replaceAll("XDIX", "VDIV"); //499\r
+ result = result.replaceAll("XMV", "VM"); // 995\r
+ result = result.replaceAll("XMIX", "VMIV"); //999\r
+ }\r
+ if (form == 4) {\r
+ result = result.replaceAll("VDIV", "ID"); //499\r
+ result = result.replaceAll("VMIV", "IM"); //999\r
+ }\r
+\r
+ return result;\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 Roman() as loaded from a test data spreadsheet.<p/>\r
+ *\r
+ * @author cedric dot walter @ gmail dot com\r
+ */\r
+public class TestRomanFunctionsFromSpreadsheet extends BaseTestFunctionsFromSpreadsheet {\r
+\r
+ protected String getFilename() {\r
+ return "RomanFunctionTestCaseData.xls";\r
+ }\r
+}\r