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 9, 2005
019: *
020: */
021: package org.apache.poi.hssf.record.formula.functions;
022:
023: import org.apache.poi.hssf.record.formula.eval.AreaEval;
024: import org.apache.poi.hssf.record.formula.eval.BlankEval;
025: import org.apache.poi.hssf.record.formula.eval.BoolEval;
026: import org.apache.poi.hssf.record.formula.eval.ErrorEval;
027: import org.apache.poi.hssf.record.formula.eval.Eval;
028: import org.apache.poi.hssf.record.formula.eval.NumberEval;
029: import org.apache.poi.hssf.record.formula.eval.RefEval;
030: import org.apache.poi.hssf.record.formula.eval.StringEval;
031: import org.apache.poi.hssf.record.formula.eval.ValueEval;
032:
033: /**
034: * @author Amol S. Deshmukh < amol at apache dot org >
035: * The NOT boolean function. Returns negation of specified value
036: * (treated as a boolean). If the specified arg is a number,
037: * then it is true <=> 'number is non-zero'
038: */
039: public class Not extends BooleanFunction {
040:
041: public Eval evaluate(Eval[] operands, int srcRow, short srcCol) {
042: ValueEval retval = null;
043: boolean b = true;
044: ValueEval tempVe = null;
045:
046: switch (operands.length) {
047: default:
048: retval = ErrorEval.VALUE_INVALID;
049: break;
050: case 1:
051: if (operands[0] instanceof AreaEval) {
052: AreaEval ae = (AreaEval) operands[0];
053: if (ae.isRow() && ae.containsColumn(srcCol)) {
054: ValueEval ve = ae.getValueAt(ae.getFirstRow(),
055: srcCol);
056: tempVe = singleOperandEvaluate(ve);
057: } else if (ae.isColumn() && ae.containsRow(srcRow)) {
058: ValueEval ve = ae.getValueAt(srcRow, ae
059: .getFirstColumn());
060: tempVe = singleOperandEvaluate(ve);
061: } else {
062: retval = ErrorEval.VALUE_INVALID;
063: }
064: } else {
065: tempVe = singleOperandEvaluate(operands[0]);
066: if (tempVe instanceof StringEval) {
067: retval = ErrorEval.VALUE_INVALID;
068: } else if (tempVe instanceof ErrorEval) {
069: retval = tempVe;
070: }
071: }
072: }
073:
074: if (retval == null) { // if no error
075: if (tempVe instanceof BoolEval) {
076: b = b && ((BoolEval) tempVe).getBooleanValue();
077: } else if (tempVe instanceof StringEval) {
078: retval = ErrorEval.VALUE_INVALID;
079: } else if (tempVe instanceof ErrorEval) {
080: retval = tempVe;
081: }
082: retval = b ? BoolEval.FALSE : BoolEval.TRUE;
083: }
084:
085: return retval;
086: }
087:
088: protected ValueEval singleOperandEvaluate(Eval ve) {
089: ValueEval retval = ErrorEval.VALUE_INVALID;
090: if (ve instanceof RefEval) {
091: RefEval re = (RefEval) ve;
092: retval = singleOperandEvaluate(re.getInnerValueEval());
093: } else if (ve instanceof BoolEval) {
094: retval = (BoolEval) ve;
095: } else if (ve instanceof NumberEval) {
096: NumberEval ne = (NumberEval) ve;
097: retval = ne.getNumberValue() != 0 ? BoolEval.TRUE
098: : BoolEval.FALSE;
099: } else if (ve instanceof StringEval) {
100: StringEval se = (StringEval) ve;
101: String str = se.getStringValue();
102: retval = str.equalsIgnoreCase("true") ? BoolEval.TRUE : str
103: .equalsIgnoreCase("false") ? BoolEval.FALSE
104: : (ValueEval) ErrorEval.VALUE_INVALID;
105: } else if (ve instanceof BlankEval) {
106: retval = BoolEval.FALSE;
107: } else {
108: retval = ErrorEval.VALUE_INVALID;
109: }
110: return retval;
111: }
112: }
|