From 0dfb528355c0447399f93a97906a932dfc7fc66b Mon Sep 17 00:00:00 2001 From: Yegor Kozlov Date: Sun, 2 Jun 2013 15:58:41 +0000 Subject: [PATCH] Bugzilla 55041: CODE function support, also removed @Override from interfaces to stay compatible with JDK 1.5 git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1488733 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/ss/formula/functions/Code.java | 37 ++++++++++++ .../poi/ss/formula/functions/Dec2Hex.java | 13 +++- .../poi/ss/formula/functions/Delta.java | 1 - .../poi/ss/formula/functions/TestCode.java | 59 +++++++++++++++++++ 4 files changed, 106 insertions(+), 4 deletions(-) create mode 100644 src/java/org/apache/poi/ss/formula/functions/Code.java create mode 100644 src/testcases/org/apache/poi/ss/formula/functions/TestCode.java 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 index 000000000..660102648 --- /dev/null +++ b/src/java/org/apache/poi/ss/formula/functions/Code.java @@ -0,0 +1,37 @@ +package org.apache.poi.ss.formula.functions; + +import org.apache.poi.ss.formula.eval.*; + +/** + * Implementation for Excel CODE () function.

+ *

+ * Syntax:
CODE (text )
+ *

+ * Returns a numeric code for the first character in a text string. The returned code corresponds to the character set used by your computer. + *

+ * 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)); + } +} + diff --git a/src/java/org/apache/poi/ss/formula/functions/Dec2Hex.java b/src/java/org/apache/poi/ss/formula/functions/Dec2Hex.java index 85d5df5e7..c4d36214e 100644 --- a/src/java/org/apache/poi/ss/formula/functions/Dec2Hex.java +++ b/src/java/org/apache/poi/ss/formula/functions/Dec2Hex.java @@ -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 Var1or2ArgFunction { 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 diff --git a/src/java/org/apache/poi/ss/formula/functions/Delta.java b/src/java/org/apache/poi/ss/formula/functions/Delta.java index af56f72bf..8488189aa 100644 --- a/src/java/org/apache/poi/ss/formula/functions/Delta.java +++ b/src/java/org/apache/poi/ss/formula/functions/Delta.java @@ -42,7 +42,6 @@ public final class Delta extends Fixed2ArgFunction { 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 { 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 index 000000000..7a83f9c57 --- /dev/null +++ b/src/testcases/org/apache/poi/ss/formula/functions/TestCode.java @@ -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); + } +}