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 8, 2005
19: *
20: */
21: package org.apache.poi.hssf.record.formula.eval;
22:
23: import org.apache.poi.hssf.record.formula.AddPtg;
24: import org.apache.poi.hssf.record.formula.Ptg;
25:
26: /**
27: * @author Amol S. Deshmukh < amolweb at ya hoo dot com >
28: * This is a documentation of the observed behaviour of
29: * the '+' operator in Excel:
30: * <ol>
31: * <li> 1+TRUE = 2
32: * <li> 1+FALSE = 1
33: * <li> 1+"true" = #VALUE!
34: * <li> 1+"1" = 2
35: * <li> 1+A1 = #VALUE if A1 contains "1"
36: * <li> 1+A1 = 2 if A1 contains ="1"
37: * <li> 1+A1 = 2 if A1 contains TRUE or =TRUE
38: * <li> 1+A1 = #VALUE! if A1 contains "TRUE" or ="TRUE"
39: */
40: public class AddEval extends NumericOperationEval {
41:
42: private AddPtg delegate;
43: private static final ValueEvalToNumericXlator NUM_XLATOR = new ValueEvalToNumericXlator(
44: (short) (ValueEvalToNumericXlator.BOOL_IS_PARSED
45: | ValueEvalToNumericXlator.EVALUATED_REF_BOOL_IS_PARSED
46: | ValueEvalToNumericXlator.EVALUATED_REF_STRING_IS_PARSED
47: | ValueEvalToNumericXlator.REF_BOOL_IS_PARSED
48: | ValueEvalToNumericXlator.STRING_IS_PARSED | ValueEvalToNumericXlator.REF_STRING_IS_PARSED));
49:
50: public AddEval(Ptg ptg) {
51: delegate = (AddPtg) ptg;
52: }
53:
54: public ValueEvalToNumericXlator getXlator() {
55: return NUM_XLATOR;
56: }
57:
58: public Eval evaluate(Eval[] operands, int srcRow, short srcCol) {
59: Eval retval = null;
60: double d = 0;
61: switch (operands.length) {
62: default: // will rarely happen. currently the parser itself fails.
63: retval = ErrorEval.UNKNOWN_ERROR;
64: break;
65: case 2:
66: for (int i = 0, iSize = 2; retval == null && i < iSize; i++) {
67: ValueEval ve = singleOperandEvaluate(operands[i],
68: srcRow, srcCol);
69: if (ve instanceof NumericValueEval) {
70: d += ((NumericValueEval) ve).getNumberValue();
71: } else if (ve instanceof BlankEval) {
72: // do nothing
73: } else {
74: retval = ErrorEval.VALUE_INVALID;
75: }
76: } // end for inside case
77: } // end switch
78:
79: if (retval == null) {
80: retval = Double.isNaN(d) ? (ValueEval) ErrorEval.VALUE_INVALID
81: : new NumberEval(d);
82: }
83:
84: return retval;
85: }
86:
87: public int getNumberOfOperands() {
88: return delegate.getNumberOfOperands();
89: }
90:
91: public int getType() {
92: return delegate.getType();
93: }
94: }
|