Move the FormulaEvaluator code out of scratchpad
[poi.git] / src / java / org / apache / poi / hssf / record / formula / functions / Nper.java
blob95411edb7777a1bc9b489252960e245f0b90eb6d
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * Created on May 15, 2005
21 package org.apache.poi.hssf.record.formula.functions;
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;
30 public class Nper extends FinanceFunction {
32 public Eval evaluate(Eval[] operands, int srcRow, short srcCol) {
33 double rate = 0, fv = 0, pmt = 0, pv = 0, d = 0;
34 boolean type = false;
35 ValueEval retval = null;
36 ValueEval ve = null;
38 switch (operands.length) {
39 default:
40 retval = ErrorEval.VALUE_INVALID;
41 break;
42 case 5:
43 ve = singleOperandNumericAsBoolean(operands[4], srcRow, srcCol);
44 if (ve instanceof ErrorEval) { retval = ErrorEval.VALUE_INVALID; break; }
45 type = ((BoolEval) ve).getBooleanValue();
46 case 4:
47 ve = singleOperandEvaluate(operands[0], srcRow, srcCol);
48 if (ve instanceof NumericValueEval) rate = ((NumericValueEval) ve).getNumberValue();
49 else { retval = ErrorEval.VALUE_INVALID; break; }
51 ve = singleOperandEvaluate(operands[1], srcRow, srcCol);
52 if (ve instanceof NumericValueEval) pmt = ((NumericValueEval) ve).getNumberValue();
53 else { retval = ErrorEval.VALUE_INVALID; break; }
55 ve = singleOperandEvaluate(operands[2], srcRow, srcCol);
56 if (ve instanceof NumericValueEval) pv = ((NumericValueEval) ve).getNumberValue();
57 else { retval = ErrorEval.VALUE_INVALID; break; }
59 ve = singleOperandEvaluate(operands[3], srcRow, srcCol);
60 if (ve instanceof NumericValueEval) fv = ((NumericValueEval) ve).getNumberValue();
61 else { retval = ErrorEval.VALUE_INVALID; break; }
64 if (retval == null) {
65 d = FinanceLib.nper(rate, pmt, pv, fv, type);
66 retval = (Double.isNaN(d))
67 ? (ValueEval) ErrorEval.VALUE_INVALID
68 : (Double.isInfinite(d))
69 ? (ValueEval) ErrorEval.NUM_ERROR
70 : new NumberEval(d);
72 return retval;