001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /*
018: * Created on May 8, 2005
019: *
020: */
021: package org.apache.poi.hssf.record.formula.eval;
022:
023: import org.apache.poi.hssf.record.formula.Ptg;
024: import org.apache.poi.hssf.record.formula.UnaryPlusPtg;
025:
026: /**
027: * @author Amol S. Deshmukh < amolweb at ya hoo dot com >
028: *
029: */
030: public class UnaryPlusEval implements OperationEval /*extends NumericOperationEval*/{
031:
032: private UnaryPlusPtg delegate;
033:
034: /*
035: * COMMENT FOR COMMENTED CODE IN THIS FILE
036: *
037: * In excel the programmer seems to not have cared to
038: * think about how strings were handled in other numeric
039: * operations when he/she was implementing this operation :P
040: *
041: * Here's what I mean:
042: *
043: * Q. If the formula -"hello" evaluates to #VALUE! in excel, what should
044: * the formula +"hello" evaluate to?
045: *
046: * A. +"hello" evaluates to "hello" (what the...?)
047: *
048: */
049:
050: // private static final ValueEvalToNumericXlator NUM_XLATOR =
051: // new ValueEvalToNumericXlator((short)
052: // ( ValueEvalToNumericXlator.BOOL_IS_PARSED
053: // | ValueEvalToNumericXlator.EVALUATED_REF_BOOL_IS_PARSED
054: // | ValueEvalToNumericXlator.EVALUATED_REF_STRING_IS_PARSED
055: // | ValueEvalToNumericXlator.REF_BOOL_IS_PARSED
056: // | ValueEvalToNumericXlator.STRING_IS_PARSED
057: // ));
058:
059: public UnaryPlusEval(Ptg ptg) {
060: this .delegate = (UnaryPlusPtg) ptg;
061: }
062:
063: // protected ValueEvalToNumericXlator getXlator() {
064: // return NUM_XLATOR;
065: // }
066:
067: public Eval evaluate(Eval[] operands, int srcRow, short srcCol) {
068: ValueEval retval = null;
069:
070: switch (operands.length) {
071: default:
072: retval = ErrorEval.UNKNOWN_ERROR;
073: break;
074: case 1:
075:
076: // ValueEval ve = singleOperandEvaluate(operands[0], srcRow, srcCol);
077: // if (ve instanceof NumericValueEval) {
078: // d = ((NumericValueEval) ve).getNumberValue();
079: // }
080: // else if (ve instanceof BlankEval) {
081: // // do nothing
082: // }
083: // else if (ve instanceof ErrorEval) {
084: // retval = ve;
085: // }
086: if (operands[0] instanceof RefEval) {
087: RefEval re = (RefEval) operands[0];
088: retval = re.getInnerValueEval();
089: } else if (operands[0] instanceof AreaEval) {
090: AreaEval ae = (AreaEval) operands[0];
091: if (ae.contains(srcRow, srcCol)) { // circular ref!
092: retval = ErrorEval.CIRCULAR_REF_ERROR;
093: } else if (ae.isRow()) {
094: if (ae.containsColumn(srcCol)) {
095: ValueEval ve = ae.getValueAt(ae.getFirstRow(),
096: srcCol);
097: if (ve instanceof RefEval) {
098: ve = ((RefEval) ve).getInnerValueEval();
099: }
100: retval = ve;
101: } else {
102: retval = ErrorEval.VALUE_INVALID;
103: }
104: } else if (ae.isColumn()) {
105: if (ae.containsRow(srcRow)) {
106: ValueEval ve = ae.getValueAt(ae.getFirstRow(),
107: srcCol);
108: if (ve instanceof RefEval) {
109: ve = ((RefEval) ve).getInnerValueEval();
110: }
111: retval = ve;
112: } else {
113: retval = ErrorEval.VALUE_INVALID;
114: }
115: } else {
116: retval = ErrorEval.VALUE_INVALID;
117: }
118: } else {
119: retval = (ValueEval) operands[0];
120: }
121: }
122:
123: if (retval instanceof BlankEval) {
124: retval = new NumberEval(0);
125: }
126:
127: return retval;
128: }
129:
130: public int getNumberOfOperands() {
131: return delegate.getNumberOfOperands();
132: }
133:
134: public int getType() {
135: return delegate.getType();
136: }
137:
138: }
|