]> source.dussan.org Git - poi.git/commitdiff
Bugzilla 55041: CODE function support, also removed @Override from interfaces to...
authorYegor Kozlov <yegor@apache.org>
Sun, 2 Jun 2013 15:58:41 +0000 (15:58 +0000)
committerYegor Kozlov <yegor@apache.org>
Sun, 2 Jun 2013 15:58:41 +0000 (15:58 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1488733 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/ss/formula/functions/Code.java [new file with mode: 0644]
src/java/org/apache/poi/ss/formula/functions/Dec2Hex.java
src/java/org/apache/poi/ss/formula/functions/Delta.java
src/testcases/org/apache/poi/ss/formula/functions/TestCode.java [new file with mode: 0644]

diff --git a/src/java/org/apache/poi/ss/formula/functions/Code.java b/src/java/org/apache/poi/ss/formula/functions/Code.java
new file mode 100644 (file)
index 0000000..6601026
--- /dev/null
@@ -0,0 +1,37 @@
+package org.apache.poi.ss.formula.functions;\r
+\r
+import org.apache.poi.ss.formula.eval.*;\r
+\r
+/**\r
+ * Implementation for Excel CODE () function.<p/>\r
+ * <p/>\r
+ * <b>Syntax</b>:<br/> <b>CODE   </b>(<b>text</b> )<br/>\r
+ * <p/>\r
+ * Returns a numeric code for the first character in a text string. The returned code corresponds to the character set used by your computer.\r
+ * <p/>\r
+ * text The text for which you want the code of the first character.\r
+ *\r
+ * @author cedric dot walter @ gmail dot com\r
+ */\r
+public class Code extends Fixed1ArgFunction {\r
+\r
+    public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval textArg) {\r
+\r
+        ValueEval veText1;\r
+        try {\r
+            veText1 = OperandResolver.getSingleValue(textArg, srcRowIndex, srcColumnIndex);\r
+        } catch (EvaluationException e) {\r
+            return e.getErrorEval();\r
+        }\r
+        String text = OperandResolver.coerceValueToString(veText1);\r
+\r
+        if (text.length() == 0) {\r
+            return ErrorEval.VALUE_INVALID;\r
+        }\r
+\r
+        int code = (int)text.charAt(0);\r
+\r
+        return new StringEval(String.valueOf(code));\r
+    }\r
+}\r
+\r
index 85d5df5e71326f3698bd0f93feecf462b90f6888..c4d36214e5d09b3a981c60baec7f0c7ac984104c 100644 (file)
@@ -17,6 +17,7 @@
 \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
 /**\r
@@ -51,13 +52,12 @@ import org.apache.poi.ss.formula.eval.*;
  *\r
  * @author cedric dot walter @ gmail dot com\r
  */\r
-public final class Dec2Hex extends Var1or2ArgFunction {\r
+public final class Dec2Hex extends Var1or2ArgFunction implements FreeRefFunction {\r
 \r
     private final static long MIN_VALUE = new Long("-549755813888").longValue();\r
     private final static long MAX_VALUE = new Long("549755813887").longValue();\r
     private final static int DEFAULT_PLACES_VALUE = 10;;\r
 \r
-    @Override\r
     public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval number, ValueEval places) {\r
         ValueEval veText1;\r
         try {\r
@@ -115,8 +115,15 @@ public final class Dec2Hex extends Var1or2ArgFunction {
         return new StringEval(hex.toUpperCase());\r
     }\r
 \r
-    @Override\r
     public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0) {\r
         return this.evaluate(srcRowIndex, srcColumnIndex, arg0, new StringEval(String.valueOf(DEFAULT_PLACES_VALUE)));\r
     }\r
+\r
+    public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) {\r
+        if (args.length != 2) {\r
+            return ErrorEval.VALUE_INVALID;\r
+        }\r
+        return evaluate(ec.getRowIndex(), ec.getColumnIndex(), args[0], args[1]);\r
+    }\r
+\r
 }
\ No newline at end of file
index af56f72bf4db3652ffb6b04c248fac34181aa9da..8488189aa545f87421e85bdda94f27b1214249f0 100644 (file)
@@ -42,7 +42,6 @@ public final class Delta extends Fixed2ArgFunction {
     private final static NumberEval ONE = new NumberEval(1);\r
     private final static NumberEval ZERO = new NumberEval(0);\r
 \r
-    @Override\r
     public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg1, ValueEval arg2) {\r
         ValueEval veText1;\r
         try {\r
diff --git a/src/testcases/org/apache/poi/ss/formula/functions/TestCode.java b/src/testcases/org/apache/poi/ss/formula/functions/TestCode.java
new file mode 100644 (file)
index 0000000..7a83f9c
--- /dev/null
@@ -0,0 +1,59 @@
+/* ====================================================================\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
+package org.apache.poi.ss.formula.functions;\r
+\r
+import junit.framework.TestCase;\r
+import org.apache.poi.ss.formula.eval.ErrorEval;\r
+import org.apache.poi.ss.formula.eval.StringEval;\r
+import org.apache.poi.ss.formula.eval.ValueEval;\r
+\r
+/**\r
+ * Tests for {@link Code}\r
+ *\r
+ * @author cedric dot walter @ gmail dot com\r
+ */\r
+public class TestCode extends TestCase\r
+{\r
+    private static ValueEval invokeValue(String number1) {\r
+        ValueEval[] args = new ValueEval[]{new StringEval(number1),};\r
+        return new Code().evaluate(args, -1, -1);\r
+    }\r
+\r
+    private static void confirmValue(String msg, String number1, String expected) {\r
+        ValueEval result = invokeValue(number1);\r
+        assertEquals(StringEval.class, result.getClass());\r
+        assertEquals(msg, expected, ((StringEval) result).getStringValue());\r
+    }\r
+\r
+    private static void confirmValueError(String msg, String number1, ErrorEval numError) {\r
+        ValueEval result = invokeValue(number1);\r
+        assertEquals(ErrorEval.class, result.getClass());\r
+        assertEquals(msg, numError, result);\r
+    }\r
+\r
+\r
+    public void testBasic() {\r
+        confirmValue("Displays the numeric code for A (65)", "A", "65");\r
+        confirmValue("Displays the numeric code for the first character in text ABCDEFGHI (65)", "ABCDEFGHI", "65");\r
+\r
+        confirmValue("Displays the numeric code for ! (33)", "!", "33");\r
+    }\r
+\r
+    public void testErrors() {\r
+        confirmValueError("Empty text", "", ErrorEval.VALUE_INVALID);\r
+    }\r
+}\r