01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: /*
18: * Created on May 15, 2005
19: *
20: */
21: package org.apache.poi.hssf.record.formula.functions;
22:
23: import org.apache.poi.hssf.record.formula.eval.BoolEval;
24: import org.apache.poi.hssf.record.formula.eval.ErrorEval;
25: import org.apache.poi.hssf.record.formula.eval.Eval;
26: import org.apache.poi.hssf.record.formula.eval.NumberEval;
27: import org.apache.poi.hssf.record.formula.eval.NumericValueEval;
28: import org.apache.poi.hssf.record.formula.eval.ValueEval;
29:
30: public class Pv extends FinanceFunction {
31:
32: public Eval evaluate(Eval[] operands, int srcRow, short srcCol) {
33: double rate = 0, fv = 0, nper = 0, pmt = 0, d = 0;
34: boolean type = false;
35: ValueEval retval = null;
36: ValueEval ve = null;
37:
38: switch (operands.length) {
39: default:
40: retval = ErrorEval.VALUE_INVALID;
41: break;
42: case 5:
43: ve = singleOperandNumericAsBoolean(operands[4], srcRow,
44: srcCol);
45: if (ve instanceof ErrorEval) {
46: retval = ErrorEval.VALUE_INVALID;
47: break;
48: }
49: type = ((BoolEval) ve).getBooleanValue();
50: case 4:
51: ve = singleOperandEvaluate(operands[3], srcRow, srcCol);
52: if (ve instanceof NumericValueEval)
53: fv = ((NumericValueEval) ve).getNumberValue();
54: else {
55: retval = ErrorEval.VALUE_INVALID;
56: break;
57: }
58: case 3:
59: ve = singleOperandEvaluate(operands[1], srcRow, srcCol);
60: if (ve instanceof NumericValueEval)
61: nper = ((NumericValueEval) ve).getNumberValue();
62: else {
63: retval = ErrorEval.VALUE_INVALID;
64: break;
65: }
66:
67: ve = singleOperandEvaluate(operands[2], srcRow, srcCol);
68: if (ve instanceof NumericValueEval)
69: pmt = ((NumericValueEval) ve).getNumberValue();
70: else {
71: retval = ErrorEval.VALUE_INVALID;
72: break;
73: }
74:
75: ve = singleOperandEvaluate(operands[0], srcRow, srcCol);
76: if (ve instanceof NumericValueEval)
77: rate = ((NumericValueEval) ve).getNumberValue();
78: else {
79: retval = ErrorEval.VALUE_INVALID;
80: break;
81: }
82: }
83:
84: if (retval == null) {
85: d = FinanceLib.pv(rate, nper, pmt, fv, type);
86: retval = (Double.isNaN(d)) ? (ValueEval) ErrorEval.VALUE_INVALID
87: : (Double.isInfinite(d)) ? (ValueEval) ErrorEval.NUM_ERROR
88: : new NumberEval(d);
89: }
90: return retval;
91: }
92: }
|