From af324a3a2a6a22c859a272b15e788bbb130d9ebf Mon Sep 17 00:00:00 2001 From: "Amol S. Deshmukh" Date: Wed, 22 Jun 2005 20:43:30 +0000 Subject: [PATCH] Implemented this finance function evaluation git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@353723 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/hssf/record/formula/functions/Fv.java | 52 ++++++++++++++++++- .../hssf/record/formula/functions/Nper.java | 52 ++++++++++++++++++- .../hssf/record/formula/functions/Pmt.java | 51 +++++++++++++++++- .../poi/hssf/record/formula/functions/Pv.java | 51 +++++++++++++++++- 4 files changed, 202 insertions(+), 4 deletions(-) diff --git a/src/scratchpad/src/org/apache/poi/hssf/record/formula/functions/Fv.java b/src/scratchpad/src/org/apache/poi/hssf/record/formula/functions/Fv.java index d0ef4ab157..fa88a3bbb3 100644 --- a/src/scratchpad/src/org/apache/poi/hssf/record/formula/functions/Fv.java +++ b/src/scratchpad/src/org/apache/poi/hssf/record/formula/functions/Fv.java @@ -4,10 +4,60 @@ */ package org.apache.poi.hssf.record.formula.functions; +import org.apache.poi.hssf.record.formula.eval.BoolEval; +import org.apache.poi.hssf.record.formula.eval.ErrorEval; +import org.apache.poi.hssf.record.formula.eval.Eval; +import org.apache.poi.hssf.record.formula.eval.NumberEval; +import org.apache.poi.hssf.record.formula.eval.NumericValueEval; +import org.apache.poi.hssf.record.formula.eval.ValueEval; + /** * @author * */ -public class Fv extends DefaultFunctionImpl { +public class Fv extends FinanceFunction { + + public Eval evaluate(Eval[] operands, int srcRow, short srcCol) { + double rate = 0, nper = 0, pmt = 0, pv = 0, d = 0; + boolean type = false; + ValueEval retval = null; + ValueEval ve = null; + + switch (operands.length) { + default: + retval = ErrorEval.VALUE_INVALID; + break; + case 5: + ve = singleOperandNumericAsBoolean(operands[4], srcRow, srcCol); + if (ve instanceof ErrorEval) { retval = ErrorEval.VALUE_INVALID; break; } + type = ((BoolEval) ve).getBooleanValue(); + case 4: + ve = singleOperandEvaluate(operands[3], srcRow, srcCol); + if (ve instanceof NumericValueEval) pv = ((NumericValueEval) ve).getNumberValue(); + else { retval = ErrorEval.VALUE_INVALID; break; } + case 3: + ve = singleOperandEvaluate(operands[1], srcRow, srcCol); + if (ve instanceof NumericValueEval) nper = ((NumericValueEval) ve).getNumberValue(); + else { retval = ErrorEval.VALUE_INVALID; break; } + + ve = singleOperandEvaluate(operands[2], srcRow, srcCol); + if (ve instanceof NumericValueEval) pmt = ((NumericValueEval) ve).getNumberValue(); + else { retval = ErrorEval.VALUE_INVALID; break; } + + ve = singleOperandEvaluate(operands[0], srcRow, srcCol); + if (ve instanceof NumericValueEval) rate = ((NumericValueEval) ve).getNumberValue(); + else { retval = ErrorEval.VALUE_INVALID; break; } + } + + if (retval == null) { + d = FinanceLib.fv(rate, nper, pmt, pv, type); + retval = (Double.isNaN(d)) + ? (ValueEval) ErrorEval.VALUE_INVALID + : (Double.isInfinite(d)) + ? (ValueEval) ErrorEval.NUM_ERROR + : new NumberEval(d); + } + return retval; + } } diff --git a/src/scratchpad/src/org/apache/poi/hssf/record/formula/functions/Nper.java b/src/scratchpad/src/org/apache/poi/hssf/record/formula/functions/Nper.java index 709e493c08..e12bfdbe8c 100644 --- a/src/scratchpad/src/org/apache/poi/hssf/record/formula/functions/Nper.java +++ b/src/scratchpad/src/org/apache/poi/hssf/record/formula/functions/Nper.java @@ -4,10 +4,60 @@ */ package org.apache.poi.hssf.record.formula.functions; +import org.apache.poi.hssf.record.formula.eval.BoolEval; +import org.apache.poi.hssf.record.formula.eval.ErrorEval; +import org.apache.poi.hssf.record.formula.eval.Eval; +import org.apache.poi.hssf.record.formula.eval.NumberEval; +import org.apache.poi.hssf.record.formula.eval.NumericValueEval; +import org.apache.poi.hssf.record.formula.eval.ValueEval; + /** * @author * */ -public class Nper extends DefaultFunctionImpl { +public class Nper extends FinanceFunction { + + public Eval evaluate(Eval[] operands, int srcRow, short srcCol) { + double rate = 0, fv = 0, pmt = 0, pv = 0, d = 0; + boolean type = false; + ValueEval retval = null; + ValueEval ve = null; + + switch (operands.length) { + default: + retval = ErrorEval.VALUE_INVALID; + break; + case 5: + ve = singleOperandNumericAsBoolean(operands[4], srcRow, srcCol); + if (ve instanceof ErrorEval) { retval = ErrorEval.VALUE_INVALID; break; } + type = ((BoolEval) ve).getBooleanValue(); + case 4: + ve = singleOperandEvaluate(operands[0], srcRow, srcCol); + if (ve instanceof NumericValueEval) rate = ((NumericValueEval) ve).getNumberValue(); + else { retval = ErrorEval.VALUE_INVALID; break; } + + ve = singleOperandEvaluate(operands[1], srcRow, srcCol); + if (ve instanceof NumericValueEval) pmt = ((NumericValueEval) ve).getNumberValue(); + else { retval = ErrorEval.VALUE_INVALID; break; } + + ve = singleOperandEvaluate(operands[2], srcRow, srcCol); + if (ve instanceof NumericValueEval) pv = ((NumericValueEval) ve).getNumberValue(); + else { retval = ErrorEval.VALUE_INVALID; break; } + + ve = singleOperandEvaluate(operands[3], srcRow, srcCol); + if (ve instanceof NumericValueEval) fv = ((NumericValueEval) ve).getNumberValue(); + else { retval = ErrorEval.VALUE_INVALID; break; } + } + + if (retval == null) { + d = FinanceLib.nper(rate, pmt, pv, fv, type); + retval = (Double.isNaN(d)) + ? (ValueEval) ErrorEval.VALUE_INVALID + : (Double.isInfinite(d)) + ? (ValueEval) ErrorEval.NUM_ERROR + : new NumberEval(d); + } + return retval; + } } diff --git a/src/scratchpad/src/org/apache/poi/hssf/record/formula/functions/Pmt.java b/src/scratchpad/src/org/apache/poi/hssf/record/formula/functions/Pmt.java index bde301a20a..8e7b9c4e1f 100644 --- a/src/scratchpad/src/org/apache/poi/hssf/record/formula/functions/Pmt.java +++ b/src/scratchpad/src/org/apache/poi/hssf/record/formula/functions/Pmt.java @@ -4,10 +4,59 @@ */ package org.apache.poi.hssf.record.formula.functions; +import org.apache.poi.hssf.record.formula.eval.BoolEval; +import org.apache.poi.hssf.record.formula.eval.ErrorEval; +import org.apache.poi.hssf.record.formula.eval.Eval; +import org.apache.poi.hssf.record.formula.eval.NumberEval; +import org.apache.poi.hssf.record.formula.eval.NumericValueEval; +import org.apache.poi.hssf.record.formula.eval.ValueEval; + /** * @author * */ -public class Pmt extends DefaultFunctionImpl { +public class Pmt extends FinanceFunction { + public Eval evaluate(Eval[] operands, int srcRow, short srcCol) { + double rate = 0, fv = 0, nper = 0, pv = 0, d = 0; + boolean type = false; + ValueEval retval = null; + ValueEval ve = null; + + switch (operands.length) { + default: + retval = ErrorEval.VALUE_INVALID; + break; + case 5: + ve = singleOperandNumericAsBoolean(operands[4], srcRow, srcCol); + if (ve instanceof ErrorEval) { retval = ErrorEval.VALUE_INVALID; break; } + type = ((BoolEval) ve).getBooleanValue(); + case 4: + ve = singleOperandEvaluate(operands[0], srcRow, srcCol); + if (ve instanceof NumericValueEval) rate = ((NumericValueEval) ve).getNumberValue(); + else { retval = ErrorEval.VALUE_INVALID; break; } + + ve = singleOperandEvaluate(operands[1], srcRow, srcCol); + if (ve instanceof NumericValueEval) nper = ((NumericValueEval) ve).getNumberValue(); + else { retval = ErrorEval.VALUE_INVALID; break; } + + ve = singleOperandEvaluate(operands[2], srcRow, srcCol); + if (ve instanceof NumericValueEval) pv = ((NumericValueEval) ve).getNumberValue(); + else { retval = ErrorEval.VALUE_INVALID; break; } + + ve = singleOperandEvaluate(operands[3], srcRow, srcCol); + if (ve instanceof NumericValueEval) fv = ((NumericValueEval) ve).getNumberValue(); + else { retval = ErrorEval.VALUE_INVALID; break; } + } + + if (retval == null) { + d = FinanceLib.pmt(rate, nper, pv, fv, type); + retval = (Double.isNaN(d)) + ? (ValueEval) ErrorEval.VALUE_INVALID + : (Double.isInfinite(d)) + ? (ValueEval) ErrorEval.NUM_ERROR + : new NumberEval(d); + } + return retval; + } } diff --git a/src/scratchpad/src/org/apache/poi/hssf/record/formula/functions/Pv.java b/src/scratchpad/src/org/apache/poi/hssf/record/formula/functions/Pv.java index 8ce1e18c4d..20614b1128 100644 --- a/src/scratchpad/src/org/apache/poi/hssf/record/formula/functions/Pv.java +++ b/src/scratchpad/src/org/apache/poi/hssf/record/formula/functions/Pv.java @@ -4,10 +4,59 @@ */ package org.apache.poi.hssf.record.formula.functions; +import org.apache.poi.hssf.record.formula.eval.BoolEval; +import org.apache.poi.hssf.record.formula.eval.ErrorEval; +import org.apache.poi.hssf.record.formula.eval.Eval; +import org.apache.poi.hssf.record.formula.eval.NumberEval; +import org.apache.poi.hssf.record.formula.eval.NumericValueEval; +import org.apache.poi.hssf.record.formula.eval.ValueEval; + /** * @author * */ -public class Pv extends DefaultFunctionImpl { +public class Pv extends FinanceFunction { + + public Eval evaluate(Eval[] operands, int srcRow, short srcCol) { + double rate = 0, fv = 0, nper = 0, pmt = 0, d = 0; + boolean type = false; + ValueEval retval = null; + ValueEval ve = null; + + switch (operands.length) { + default: + retval = ErrorEval.VALUE_INVALID; + break; + case 5: + ve = singleOperandNumericAsBoolean(operands[4], srcRow, srcCol); + if (ve instanceof ErrorEval) { retval = ErrorEval.VALUE_INVALID; break; } + type = ((BoolEval) ve).getBooleanValue(); + case 4: + ve = singleOperandEvaluate(operands[3], srcRow, srcCol); + if (ve instanceof NumericValueEval) fv = ((NumericValueEval) ve).getNumberValue(); + else { retval = ErrorEval.VALUE_INVALID; break; } + case 3: + ve = singleOperandEvaluate(operands[1], srcRow, srcCol); + if (ve instanceof NumericValueEval) nper = ((NumericValueEval) ve).getNumberValue(); + else { retval = ErrorEval.VALUE_INVALID; break; } + + ve = singleOperandEvaluate(operands[2], srcRow, srcCol); + if (ve instanceof NumericValueEval) pmt = ((NumericValueEval) ve).getNumberValue(); + else { retval = ErrorEval.VALUE_INVALID; break; } + ve = singleOperandEvaluate(operands[0], srcRow, srcCol); + if (ve instanceof NumericValueEval) rate = ((NumericValueEval) ve).getNumberValue(); + else { retval = ErrorEval.VALUE_INVALID; break; } + } + + if (retval == null) { + d = FinanceLib.pv(rate, nper, pmt, fv, type); + retval = (Double.isNaN(d)) + ? (ValueEval) ErrorEval.VALUE_INVALID + : (Double.isInfinite(d)) + ? (ValueEval) ErrorEval.NUM_ERROR + : new NumberEval(d); + } + return retval; + } } -- 2.39.5