poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From n...@apache.org
Subject svn commit: r1442392 - in /poi/trunk/src: java/org/apache/poi/ss/formula/atp/AnalysisToolPak.java java/org/apache/poi/ss/formula/functions/EDate.java testcases/org/apache/poi/ss/formula/functions/TestEDate.java
Date Mon, 04 Feb 2013 22:01:55 GMT
Author: nick
Date: Mon Feb  4 22:01:55 2013
New Revision: 1442392

URL: http://svn.apache.org/viewvc?rev=1442392&view=rev
Log:
EDate function implementation from Detlef Brendle from bug #54508

Added:
    poi/trunk/src/java/org/apache/poi/ss/formula/functions/EDate.java
    poi/trunk/src/testcases/org/apache/poi/ss/formula/functions/TestEDate.java
Modified:
    poi/trunk/src/java/org/apache/poi/ss/formula/atp/AnalysisToolPak.java

Modified: poi/trunk/src/java/org/apache/poi/ss/formula/atp/AnalysisToolPak.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/ss/formula/atp/AnalysisToolPak.java?rev=1442392&r1=1442391&r2=1442392&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/ss/formula/atp/AnalysisToolPak.java (original)
+++ poi/trunk/src/java/org/apache/poi/ss/formula/atp/AnalysisToolPak.java Mon Feb  4 22:01:55
2013
@@ -15,6 +15,7 @@ import org.apache.poi.ss.formula.eval.No
 import org.apache.poi.ss.formula.eval.ValueEval;
 import org.apache.poi.ss.formula.function.FunctionMetadata;
 import org.apache.poi.ss.formula.function.FunctionMetadataRegistry;
+import org.apache.poi.ss.formula.functions.EDate;
 import org.apache.poi.ss.formula.functions.FreeRefFunction;
 import org.apache.poi.ss.formula.functions.Sumifs;
 import org.apache.poi.ss.formula.udf.UDFFinder;
@@ -99,7 +100,7 @@ public final class AnalysisToolPak imple
         r(m, "DOLLARDE", null);
         r(m, "DOLLARFR", null);
         r(m, "DURATION", null);
-        r(m, "EDATE", null);
+        r(m, "EDATE", EDate.instance);
         r(m, "EFFECT", null);
         r(m, "EOMONTH", null);
         r(m, "ERF", null);

Added: poi/trunk/src/java/org/apache/poi/ss/formula/functions/EDate.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/ss/formula/functions/EDate.java?rev=1442392&view=auto
==============================================================================
--- poi/trunk/src/java/org/apache/poi/ss/formula/functions/EDate.java (added)
+++ poi/trunk/src/java/org/apache/poi/ss/formula/functions/EDate.java Mon Feb  4 22:01:55
2013
@@ -0,0 +1,43 @@
+package org.apache.poi.ss.formula.functions;
+
+import org.apache.poi.ss.formula.OperationEvaluationContext;
+import org.apache.poi.ss.formula.eval.*;
+import org.apache.poi.ss.usermodel.DateUtil;
+
+import java.util.Calendar;
+import java.util.Date;
+
+public class EDate implements FreeRefFunction {
+    public static final FreeRefFunction instance = new EDate();
+
+    @Override
+    public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) {
+        if (args.length != 2) {
+            return ErrorEval.VALUE_INVALID;
+        }
+        try {
+            double startDateAsNumber = getValue(args[0]);
+            NumberEval offsetInYearsValue = (NumberEval) args[1];
+            int offsetInMonthAsNumber = (int) offsetInYearsValue.getNumberValue();
+
+            Date startDate = DateUtil.getJavaDate(startDateAsNumber);
+            Calendar calendar = Calendar.getInstance();
+            calendar.setTime(startDate);
+            calendar.add(Calendar.MONTH, offsetInMonthAsNumber);
+            return new NumberEval(DateUtil.getExcelDate(calendar.getTime()));
+        } catch (EvaluationException e) {
+            return e.getErrorEval();
+        }
+    }
+
+    private double getValue(ValueEval arg) throws EvaluationException {
+        if (arg instanceof NumberEval) {
+            return ((NumberEval) arg).getNumberValue();
+        }
+        if (arg instanceof RefEval) {
+            ValueEval innerValueEval = ((RefEval) arg).getInnerValueEval();
+            return ((NumberEval) innerValueEval).getNumberValue();
+        }
+        throw new EvaluationException(ErrorEval.REF_INVALID);
+    }
+}

Added: poi/trunk/src/testcases/org/apache/poi/ss/formula/functions/TestEDate.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/ss/formula/functions/TestEDate.java?rev=1442392&view=auto
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/ss/formula/functions/TestEDate.java (added)
+++ poi/trunk/src/testcases/org/apache/poi/ss/formula/functions/TestEDate.java Mon Feb  4
22:01:55 2013
@@ -0,0 +1,51 @@
+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.NumberEval;
+import org.apache.poi.ss.formula.eval.ValueEval;
+import org.apache.poi.ss.usermodel.DateUtil;
+import org.apache.poi.ss.usermodel.ErrorConstants;
+
+import java.util.Calendar;
+import java.util.Date;
+
+public class TestEDate extends TestCase{
+
+    public void testEDateProperValues() {
+        EDate eDate = new EDate();
+        NumberEval result = (NumberEval) eDate.evaluate(new ValueEval[]{new NumberEval(1000),
new NumberEval(0)}, null);
+        assertEquals(1000d, result.getNumberValue());
+    }
+
+    public void testEDateInvalidValues() {
+        EDate eDate = new EDate();
+        ErrorEval result = (ErrorEval) eDate.evaluate(new ValueEval[]{new NumberEval(1000)},
null);
+        assertEquals(ErrorConstants.ERROR_VALUE, result.getErrorCode());
+    }
+
+    public void testEDateIncrease() {
+        EDate eDate = new EDate();
+        Date startDate = new Date();
+        int offset = 2;
+        NumberEval result = (NumberEval) eDate.evaluate(new ValueEval[]{new NumberEval(DateUtil.getExcelDate(startDate)),
new NumberEval(offset)}, null);
+        Date resultDate = DateUtil.getJavaDate(result.getNumberValue());
+        Calendar instance = Calendar.getInstance();
+        instance.setTime(startDate);
+        instance.add(Calendar.MONTH, offset);
+        assertEquals(resultDate, instance.getTime());
+
+    }
+
+    public void testEDateDecrease() {
+        EDate eDate = new EDate();
+        Date startDate = new Date();
+        int offset = -2;
+        NumberEval result = (NumberEval) eDate.evaluate(new ValueEval[]{new NumberEval(DateUtil.getExcelDate(startDate)),
new NumberEval(offset)}, null);
+        Date resultDate = DateUtil.getJavaDate(result.getNumberValue());
+        Calendar instance = Calendar.getInstance();
+        instance.setTime(startDate);
+        instance.add(Calendar.MONTH, offset);
+        assertEquals(resultDate, instance.getTime());
+    }
+}



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


Mime
View raw message