From 6b34b1a70f4e4cd1e71ea2f42bedac55c99bfda3 Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Sun, 16 Mar 2008 15:40:17 +0000 Subject: [PATCH] Patch from Josh from bug #44608 - Support for PercentPtg in the formula evaluator git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@637600 13f79535-47bb-0310-9956-ffa450edef68 --- .../hssf/record/formula/eval/PercentEval.java | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100755 src/scratchpad/src/org/apache/poi/hssf/record/formula/eval/PercentEval.java diff --git a/src/scratchpad/src/org/apache/poi/hssf/record/formula/eval/PercentEval.java b/src/scratchpad/src/org/apache/poi/hssf/record/formula/eval/PercentEval.java new file mode 100755 index 000000000..c698a4e50 --- /dev/null +++ b/src/scratchpad/src/org/apache/poi/hssf/record/formula/eval/PercentEval.java @@ -0,0 +1,71 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ + +package org.apache.poi.hssf.record.formula.eval; + +import org.apache.poi.hssf.record.formula.PercentPtg; +import org.apache.poi.hssf.record.formula.Ptg; + +/** + * Implementation of Excel formula token '%'.

+ * @author Josh Micich + */ +public final class PercentEval extends NumericOperationEval { + + private PercentPtg _delegate; + + private static final ValueEvalToNumericXlator NUM_XLATOR = new ValueEvalToNumericXlator( + (short) (ValueEvalToNumericXlator.BOOL_IS_PARSED + | ValueEvalToNumericXlator.REF_BOOL_IS_PARSED + | ValueEvalToNumericXlator.STRING_IS_PARSED | ValueEvalToNumericXlator.REF_STRING_IS_PARSED)); + + public PercentEval(Ptg ptg) { + _delegate = (PercentPtg) ptg; + } + + protected ValueEvalToNumericXlator getXlator() { + return NUM_XLATOR; + } + + public Eval evaluate(Eval[] args, int srcRow, short srcCol) { + if (args.length != 1) { + return ErrorEval.VALUE_INVALID; + } + + ValueEval ve = singleOperandEvaluate(args[0], srcRow, srcCol); + if (ve instanceof NumericValueEval) { + double d0 = ((NumericValueEval) ve).getNumberValue(); + return new NumberEval(d0 / 100); + } + + if (ve instanceof BlankEval) { + return NumberEval.ZERO; + } + if (ve instanceof ErrorEval) { + return ve; + } + return ErrorEval.VALUE_INVALID; + } + + public int getNumberOfOperands() { + return _delegate.getNumberOfOperands(); + } + + public int getType() { + return _delegate.getType(); + } +}