Move the FormulaEvaluator code out of scratchpad
[poi.git] / src / java / org / apache / poi / hssf / record / formula / functions / NumericFunction.java
blobfd96f1495a9c47978102c43caf1ad250eca49e77
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 14, 2005
21 package org.apache.poi.hssf.record.formula.functions;
23 import org.apache.poi.hssf.record.formula.eval.AreaEval;
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.ValueEval;
27 import org.apache.poi.hssf.record.formula.eval.ValueEvalToNumericXlator;
29 /**
30 * @author Amol S. Deshmukh < amolweb at ya hoo dot com >
33 public abstract class NumericFunction implements Function {
35 protected static final double E = Math.E;
36 protected static final double PI = Math.PI;
38 private static final ValueEvalToNumericXlator DEFAULT_NUM_XLATOR =
39 new ValueEvalToNumericXlator((short) (
40 ValueEvalToNumericXlator.BOOL_IS_PARSED
41 | ValueEvalToNumericXlator.REF_BOOL_IS_PARSED
42 | ValueEvalToNumericXlator.STRING_IS_PARSED
43 | ValueEvalToNumericXlator.REF_STRING_IS_PARSED
44 //| ValueEvalToNumericXlator.STRING_TO_BOOL_IS_PARSED
45 //| ValueEvalToNumericXlator.REF_STRING_TO_BOOL_IS_PARSED
46 //| ValueEvalToNumericXlator.STRING_IS_INVALID_VALUE
47 //| ValueEvalToNumericXlator.REF_STRING_IS_INVALID_VALUE
48 ));
50 private static final int DEFAULT_MAX_NUM_OPERANDS = 30;
52 /**
53 * this is the default impl of the factory(ish) method getXlator.
54 * Subclasses can override this method
55 * if they desire to return a different ValueEvalToNumericXlator instance
56 * than the default.
58 protected ValueEvalToNumericXlator getXlator() {
59 return DEFAULT_NUM_XLATOR;
62 protected ValueEval singleOperandEvaluate(Eval eval, int srcRow, short srcCol) {
63 ValueEval retval;
64 if (eval instanceof AreaEval) {
65 AreaEval ae = (AreaEval) eval;
66 if (ae.contains(srcRow, srcCol)) { // circular ref!
67 retval = ErrorEval.CIRCULAR_REF_ERROR;
69 else if (ae.isRow()) {
70 if (ae.containsColumn(srcCol)) {
71 ValueEval ve = ae.getValueAt(ae.getFirstRow(), srcCol);
72 ve = getXlator().attemptXlateToNumeric(ve);
73 retval = getXlator().attemptXlateToNumeric(ve);
75 else {
76 retval = ErrorEval.VALUE_INVALID;
79 else if (ae.isColumn()) {
80 if (ae.containsRow(srcRow)) {
81 ValueEval ve = ae.getValueAt(srcRow, ae.getFirstColumn());
82 retval = getXlator().attemptXlateToNumeric(ve);
84 else {
85 retval = ErrorEval.VALUE_INVALID;
88 else {
89 retval = ErrorEval.VALUE_INVALID;
92 else {
93 retval = getXlator().attemptXlateToNumeric((ValueEval) eval);
95 return retval;