poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ye...@apache.org
Subject svn commit: r1488733 - in /poi/trunk/src: java/org/apache/poi/ss/formula/functions/ testcases/org/apache/poi/ss/formula/functions/
Date Sun, 02 Jun 2013 15:58:41 GMT
Author: yegor
Date: Sun Jun  2 15:58:41 2013
New Revision: 1488733

URL: http://svn.apache.org/r1488733
Log:
Bugzilla 55041: CODE function support, also removed @Override from interfaces to stay compatible
with JDK 1.5

Added:
    poi/trunk/src/java/org/apache/poi/ss/formula/functions/Code.java
    poi/trunk/src/testcases/org/apache/poi/ss/formula/functions/TestCode.java
Modified:
    poi/trunk/src/java/org/apache/poi/ss/formula/functions/Dec2Hex.java
    poi/trunk/src/java/org/apache/poi/ss/formula/functions/Delta.java

Added: poi/trunk/src/java/org/apache/poi/ss/formula/functions/Code.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/ss/formula/functions/Code.java?rev=1488733&view=auto
==============================================================================
--- poi/trunk/src/java/org/apache/poi/ss/formula/functions/Code.java (added)
+++ poi/trunk/src/java/org/apache/poi/ss/formula/functions/Code.java Sun Jun  2 15:58:41 2013
@@ -0,0 +1,37 @@
+package org.apache.poi.ss.formula.functions;
+
+import org.apache.poi.ss.formula.eval.*;
+
+/**
+ * Implementation for Excel CODE () function.<p/>
+ * <p/>
+ * <b>Syntax</b>:<br/> <b>CODE   </b>(<b>text</b>
)<br/>
+ * <p/>
+ * Returns a numeric code for the first character in a text string. The returned code corresponds
to the character set used by your computer.
+ * <p/>
+ * text The text for which you want the code of the first character.
+ *
+ * @author cedric dot walter @ gmail dot com
+ */
+public class Code extends Fixed1ArgFunction {
+
+    public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval textArg) {
+
+        ValueEval veText1;
+        try {
+            veText1 = OperandResolver.getSingleValue(textArg, srcRowIndex, srcColumnIndex);
+        } catch (EvaluationException e) {
+            return e.getErrorEval();
+        }
+        String text = OperandResolver.coerceValueToString(veText1);
+
+        if (text.length() == 0) {
+            return ErrorEval.VALUE_INVALID;
+        }
+
+        int code = (int)text.charAt(0);
+
+        return new StringEval(String.valueOf(code));
+    }
+}
+

Modified: poi/trunk/src/java/org/apache/poi/ss/formula/functions/Dec2Hex.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/ss/formula/functions/Dec2Hex.java?rev=1488733&r1=1488732&r2=1488733&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/ss/formula/functions/Dec2Hex.java (original)
+++ poi/trunk/src/java/org/apache/poi/ss/formula/functions/Dec2Hex.java Sun Jun  2 15:58:41
2013
@@ -17,6 +17,7 @@
 
 package org.apache.poi.ss.formula.functions;
 
+import org.apache.poi.ss.formula.OperationEvaluationContext;
 import org.apache.poi.ss.formula.eval.*;
 
 /**
@@ -51,13 +52,12 @@ import org.apache.poi.ss.formula.eval.*;
  *
  * @author cedric dot walter @ gmail dot com
  */
-public final class Dec2Hex extends Var1or2ArgFunction {
+public final class Dec2Hex extends Var1or2ArgFunction implements FreeRefFunction {
 
     private final static long MIN_VALUE = new Long("-549755813888").longValue();
     private final static long MAX_VALUE = new Long("549755813887").longValue();
     private final static int DEFAULT_PLACES_VALUE = 10;;
 
-    @Override
     public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval number, ValueEval
places) {
         ValueEval veText1;
         try {
@@ -115,8 +115,15 @@ public final class Dec2Hex extends Var1o
         return new StringEval(hex.toUpperCase());
     }
 
-    @Override
     public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0) {
         return this.evaluate(srcRowIndex, srcColumnIndex, arg0, new StringEval(String.valueOf(DEFAULT_PLACES_VALUE)));
     }
+
+    public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) {
+        if (args.length != 2) {
+            return ErrorEval.VALUE_INVALID;
+        }
+        return evaluate(ec.getRowIndex(), ec.getColumnIndex(), args[0], args[1]);
+    }
+
 }
\ No newline at end of file

Modified: poi/trunk/src/java/org/apache/poi/ss/formula/functions/Delta.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/ss/formula/functions/Delta.java?rev=1488733&r1=1488732&r2=1488733&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/ss/formula/functions/Delta.java (original)
+++ poi/trunk/src/java/org/apache/poi/ss/formula/functions/Delta.java Sun Jun  2 15:58:41
2013
@@ -42,7 +42,6 @@ public final class Delta extends Fixed2A
     private final static NumberEval ONE = new NumberEval(1);
     private final static NumberEval ZERO = new NumberEval(0);
 
-    @Override
     public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg1, ValueEval
arg2) {
         ValueEval veText1;
         try {

Added: poi/trunk/src/testcases/org/apache/poi/ss/formula/functions/TestCode.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/ss/formula/functions/TestCode.java?rev=1488733&view=auto
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/ss/formula/functions/TestCode.java (added)
+++ poi/trunk/src/testcases/org/apache/poi/ss/formula/functions/TestCode.java Sun Jun  2 15:58:41
2013
@@ -0,0 +1,59 @@
+/* ====================================================================
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You under the Apache License, Version 2.0
+   (the "License"); you may not use this file except in compliance with
+   the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+==================================================================== */
+package org.apache.poi.ss.formula.functions;
+
+import junit.framework.TestCase;
+import org.apache.poi.ss.formula.eval.ErrorEval;
+import org.apache.poi.ss.formula.eval.StringEval;
+import org.apache.poi.ss.formula.eval.ValueEval;
+
+/**
+ * Tests for {@link Code}
+ *
+ * @author cedric dot walter @ gmail dot com
+ */
+public class TestCode extends TestCase
+{
+    private static ValueEval invokeValue(String number1) {
+        ValueEval[] args = new ValueEval[]{new StringEval(number1),};
+        return new Code().evaluate(args, -1, -1);
+    }
+
+    private static void confirmValue(String msg, String number1, String expected) {
+        ValueEval result = invokeValue(number1);
+        assertEquals(StringEval.class, result.getClass());
+        assertEquals(msg, expected, ((StringEval) result).getStringValue());
+    }
+
+    private static void confirmValueError(String msg, String number1, ErrorEval numError)
{
+        ValueEval result = invokeValue(number1);
+        assertEquals(ErrorEval.class, result.getClass());
+        assertEquals(msg, numError, result);
+    }
+
+
+    public void testBasic() {
+        confirmValue("Displays the numeric code for A (65)", "A", "65");
+        confirmValue("Displays the numeric code for the first character in text ABCDEFGHI
(65)", "ABCDEFGHI", "65");
+
+        confirmValue("Displays the numeric code for ! (33)", "!", "33");
+    }
+
+    public void testErrors() {
+        confirmValueError("Empty text", "", ErrorEval.VALUE_INVALID);
+    }
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org


Mime
View raw message