From 0d06fa008e733754d128425da064b9909967c057 Mon Sep 17 00:00:00 2001 From: Josh Micich Date: Thu, 11 Sep 2008 07:16:20 +0000 Subject: [PATCH] Refactoring MultiOperandNumericFunction - removed Ref2DEval. git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@694153 13f79535-47bb-0310-9956-ffa450edef68 --- .../record/formula/eval/FunctionEval.java | 4 +- .../hssf/record/formula/eval/Ref2DEval.java | 53 ------- .../eval/ValueEvalToNumericXlator.java | 143 ------------------ .../formula/functions/AggregateFunction.java | 9 +- .../hssf/record/formula/functions/Maxa.java | 41 ----- .../functions/{Mina.java => MinaMaxa.java} | 33 ++-- .../MultiOperandNumericFunction.java | 107 +++++++------ 7 files changed, 81 insertions(+), 309 deletions(-) delete mode 100644 src/java/org/apache/poi/hssf/record/formula/eval/Ref2DEval.java delete mode 100644 src/java/org/apache/poi/hssf/record/formula/eval/ValueEvalToNumericXlator.java delete mode 100644 src/java/org/apache/poi/hssf/record/formula/functions/Maxa.java rename src/java/org/apache/poi/hssf/record/formula/functions/{Mina.java => MinaMaxa.java} (57%) diff --git a/src/java/org/apache/poi/hssf/record/formula/eval/FunctionEval.java b/src/java/org/apache/poi/hssf/record/formula/eval/FunctionEval.java index 6f14a1e856..719349a1c8 100644 --- a/src/java/org/apache/poi/hssf/record/formula/eval/FunctionEval.java +++ b/src/java/org/apache/poi/hssf/record/formula/eval/FunctionEval.java @@ -420,8 +420,8 @@ public abstract class FunctionEval implements OperationEval { retval[359] = new Hyperlink(); // HYPERLINK retval[360] = new NotImplementedFunction(); // PHONETIC retval[361] = new Averagea(); // AVERAGEA - retval[362] = new Maxa(); // MAXA - retval[363] = new Mina(); // MINA + retval[362] = MinaMaxa.MAXA; + retval[363] = MinaMaxa.MINA; retval[364] = new Stdevpa(); // STDEVPA retval[365] = new Varpa(); // VARPA retval[366] = new Stdeva(); // STDEVA diff --git a/src/java/org/apache/poi/hssf/record/formula/eval/Ref2DEval.java b/src/java/org/apache/poi/hssf/record/formula/eval/Ref2DEval.java deleted file mode 100644 index e4bb47a43d..0000000000 --- a/src/java/org/apache/poi/hssf/record/formula/eval/Ref2DEval.java +++ /dev/null @@ -1,53 +0,0 @@ -/* -* 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.RefPtg; - -/** - * @author adeshmukh - * - */ -public final class Ref2DEval implements RefEval { - - private final ValueEval value; - private final RefPtg delegate; - - public Ref2DEval(RefPtg ptg, ValueEval ve) { - if(ve == null) { - throw new IllegalArgumentException("ve must not be null"); - } - if(false && ptg == null) { // TODO - fix dodgy code in MultiOperandNumericFunction - throw new IllegalArgumentException("ptg must not be null"); - } - value = ve; - delegate = ptg; - } - public ValueEval getInnerValueEval() { - return value; - } - public int getRow() { - return delegate.getRow(); - } - public int getColumn() { - return delegate.getColumn(); - } - public AreaEval offset(int relFirstRowIx, int relLastRowIx, int relFirstColIx, int relLastColIx) { - throw new RuntimeException("should not be called"); // TODO - delete this whole class - } -} diff --git a/src/java/org/apache/poi/hssf/record/formula/eval/ValueEvalToNumericXlator.java b/src/java/org/apache/poi/hssf/record/formula/eval/ValueEvalToNumericXlator.java deleted file mode 100644 index bb04e411b1..0000000000 --- a/src/java/org/apache/poi/hssf/record/formula/eval/ValueEvalToNumericXlator.java +++ /dev/null @@ -1,143 +0,0 @@ -/* ==================================================================== - 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; - -/** - * @author Amol S. Deshmukh < amolweb at ya hoo dot com > - * - */ -public final class ValueEvalToNumericXlator { - - public static final int BLANK_IS_PARSED = 0x0001; // => blanks are not ignored, converted to 0 - - public static final int REF_BOOL_IS_PARSED = 0x0002; - - private final int flags; - - - public ValueEvalToNumericXlator(int flags) { - - if (false) { // uncomment to see who is using this class - System.err.println(new Throwable().getStackTrace()[1].getClassName() + "\t0x" - + Integer.toHexString(flags).toUpperCase()); - } - this.flags = flags; - } - - /** - * returned value can be either A NumericValueEval, BlankEval or ErrorEval. - * The params can be either NumberEval, BoolEval, StringEval, or - * RefEval - * @param eval - */ - public ValueEval attemptXlateToNumeric(ValueEval eval) { - - if (eval == null) { - throw new IllegalArgumentException("eval must not be null"); - } - - // most common case - least worries :) - if (eval instanceof NumberEval) { - return eval; - } - - if (eval instanceof BoolEval) { - return eval; - } - - if (eval instanceof StringEval) { - return xlateStringEval((StringEval) eval); - } - - if (eval instanceof RefEval) { - return xlateRefEval((RefEval) eval); - } - - if (eval instanceof ErrorEval) { - return eval; - } - - if (eval instanceof BlankEval) { - return xlateBlankEval(); - } - - // probably AreaEval? then not acceptable. - throw new RuntimeException("Invalid ValueEval type passed for conversion: " + eval.getClass()); - } - - /** - * no args are required since BlankEval has only one - * instance. If flag is set, a zero - * valued numbereval is returned, else BlankEval.INSTANCE - * is returned. - */ - private ValueEval xlateBlankEval() { - return ((flags & BLANK_IS_PARSED) > 0) - ? (ValueEval) NumberEval.ZERO - : BlankEval.INSTANCE; - } - - /** - * uses the relevant flags to decode the supplied RefVal - * @param eval - */ - private ValueEval xlateRefEval(RefEval reval) { - ValueEval eval = reval.getInnerValueEval(); - - // most common case - least worries :) - if (eval instanceof NumberEval) { - return eval; - } - - if (eval instanceof BoolEval) { - return ((flags & REF_BOOL_IS_PARSED) > 0) - ? (ValueEval) eval - : BlankEval.INSTANCE; - } - - if (eval instanceof StringEval) { - // all ref strings are blanks - return BlankEval.INSTANCE; - } - - if (eval instanceof ErrorEval) { - return eval; - } - - if (eval instanceof BlankEval) { - return xlateBlankEval(); - } - - throw new RuntimeException("Invalid ValueEval type passed for conversion: (" - + eval.getClass().getName() + ")"); - } - - /** - * uses the relevant flags to decode the StringEval - * @param eval - */ - private static ValueEval xlateStringEval(StringEval eval) { - - String s = eval.getStringValue(); - Double d = OperandResolver.parseDouble(s); - if(d == null) { - return ErrorEval.VALUE_INVALID; - } - return new NumberEval(d.doubleValue()); - } -} diff --git a/src/java/org/apache/poi/hssf/record/formula/functions/AggregateFunction.java b/src/java/org/apache/poi/hssf/record/formula/functions/AggregateFunction.java index 0758686228..0495ee3fc4 100644 --- a/src/java/org/apache/poi/hssf/record/formula/functions/AggregateFunction.java +++ b/src/java/org/apache/poi/hssf/record/formula/functions/AggregateFunction.java @@ -19,22 +19,17 @@ package org.apache.poi.hssf.record.formula.functions; import org.apache.poi.hssf.record.formula.eval.ErrorEval; import org.apache.poi.hssf.record.formula.eval.EvaluationException; -import org.apache.poi.hssf.record.formula.eval.ValueEval; -import org.apache.poi.hssf.record.formula.eval.ValueEvalToNumericXlator; /** * @author Amol S. Deshmukh < amolweb at ya hoo dot com > * */ public abstract class AggregateFunction extends MultiOperandNumericFunction { - private static final ValueEvalToNumericXlator DEFAULT_NUM_XLATOR = - new ValueEvalToNumericXlator(0); - protected ValueEval attemptXlateToNumeric(ValueEval ve) { - return DEFAULT_NUM_XLATOR.attemptXlateToNumeric(ve); + protected AggregateFunction() { + super(false, false); } - /* ---------------------------------------------------------------------- */ public static final Function AVEDEV = new AggregateFunction() { diff --git a/src/java/org/apache/poi/hssf/record/formula/functions/Maxa.java b/src/java/org/apache/poi/hssf/record/formula/functions/Maxa.java deleted file mode 100644 index 95c4c8c911..0000000000 --- a/src/java/org/apache/poi/hssf/record/formula/functions/Maxa.java +++ /dev/null @@ -1,41 +0,0 @@ -/* ==================================================================== - 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.functions; - -import org.apache.poi.hssf.record.formula.eval.ValueEval; -import org.apache.poi.hssf.record.formula.eval.ValueEvalToNumericXlator; - -/** - * @author Amol S. Deshmukh < amolweb at ya hoo dot com > - * - */ -public final class Maxa extends MultiOperandNumericFunction { - private static final ValueEvalToNumericXlator DEFAULT_NUM_XLATOR = - new ValueEvalToNumericXlator((short) ( - ValueEvalToNumericXlator.REF_BOOL_IS_PARSED - | ValueEvalToNumericXlator.BLANK_IS_PARSED - )); - - protected ValueEval attemptXlateToNumeric(ValueEval ve) { - return DEFAULT_NUM_XLATOR.attemptXlateToNumeric(ve); - } - - public double evaluate(double[] values) { - return values.length > 0 ? MathX.max(values) : 0; - } -} diff --git a/src/java/org/apache/poi/hssf/record/formula/functions/Mina.java b/src/java/org/apache/poi/hssf/record/formula/functions/MinaMaxa.java similarity index 57% rename from src/java/org/apache/poi/hssf/record/formula/functions/Mina.java rename to src/java/org/apache/poi/hssf/record/formula/functions/MinaMaxa.java index 7344936310..70e5e7ff1b 100644 --- a/src/java/org/apache/poi/hssf/record/formula/functions/Mina.java +++ b/src/java/org/apache/poi/hssf/record/formula/functions/MinaMaxa.java @@ -17,25 +17,24 @@ package org.apache.poi.hssf.record.formula.functions; -import org.apache.poi.hssf.record.formula.eval.ValueEval; -import org.apache.poi.hssf.record.formula.eval.ValueEvalToNumericXlator; - /** - * @author Amol S. Deshmukh < amolweb at ya hoo dot com > - * + * @author Amol S. Deshmukh < amolweb at ya hoo dot com > + * */ -public final class Mina extends MultiOperandNumericFunction { - private static final ValueEvalToNumericXlator DEFAULT_NUM_XLATOR = - new ValueEvalToNumericXlator((short) ( - ValueEvalToNumericXlator.REF_BOOL_IS_PARSED - | ValueEvalToNumericXlator.BLANK_IS_PARSED - )); - - protected ValueEval attemptXlateToNumeric(ValueEval ve) { - return DEFAULT_NUM_XLATOR.attemptXlateToNumeric(ve); +public abstract class MinaMaxa extends MultiOperandNumericFunction { + + protected MinaMaxa() { + super(true, true); } - public double evaluate(double[] values) { - return values.length > 0 ? MathX.min(values) : 0; - } + public static final Function MAXA = new MinaMaxa() { + protected double evaluate(double[] values) { + return values.length > 0 ? MathX.max(values) : 0; + } + }; + public static final Function MINA = new MinaMaxa() { + protected double evaluate(double[] values) { + return values.length > 0 ? MathX.min(values) : 0; + } + }; } diff --git a/src/java/org/apache/poi/hssf/record/formula/functions/MultiOperandNumericFunction.java b/src/java/org/apache/poi/hssf/record/formula/functions/MultiOperandNumericFunction.java index ec65d0f127..b3feac6a07 100644 --- a/src/java/org/apache/poi/hssf/record/formula/functions/MultiOperandNumericFunction.java +++ b/src/java/org/apache/poi/hssf/record/formula/functions/MultiOperandNumericFunction.java @@ -19,13 +19,14 @@ package org.apache.poi.hssf.record.formula.functions; import org.apache.poi.hssf.record.formula.eval.AreaEval; import org.apache.poi.hssf.record.formula.eval.BlankEval; +import org.apache.poi.hssf.record.formula.eval.BoolEval; import org.apache.poi.hssf.record.formula.eval.ErrorEval; import org.apache.poi.hssf.record.formula.eval.Eval; import org.apache.poi.hssf.record.formula.eval.EvaluationException; import org.apache.poi.hssf.record.formula.eval.NumberEval; -import org.apache.poi.hssf.record.formula.eval.NumericValueEval; -import org.apache.poi.hssf.record.formula.eval.Ref2DEval; +import org.apache.poi.hssf.record.formula.eval.OperandResolver; import org.apache.poi.hssf.record.formula.eval.RefEval; +import org.apache.poi.hssf.record.formula.eval.StringEval; import org.apache.poi.hssf.record.formula.eval.ValueEval; /** @@ -35,6 +36,16 @@ import org.apache.poi.hssf.record.formula.eval.ValueEval; * where the order of operands does not matter */ public abstract class MultiOperandNumericFunction implements Function { + + private final boolean _isReferenceBoolCounted; + private final boolean _isBlankCounted; + + protected MultiOperandNumericFunction(boolean isReferenceBoolCounted, boolean isBlankCounted) { + _isReferenceBoolCounted = isReferenceBoolCounted; + _isBlankCounted = isBlankCounted; + } + + static final double[] EMPTY_DOUBLE_ARRAY = { }; private static class DoubleList { @@ -74,7 +85,7 @@ public abstract class MultiOperandNumericFunction implements Function { private static final int DEFAULT_MAX_NUM_OPERANDS = 30; public final Eval evaluate(Eval[] args, int srcCellRow, short srcCellCol) { - + double d; try { double[] values = getNumberArray(args); @@ -82,16 +93,16 @@ public abstract class MultiOperandNumericFunction implements Function { } catch (EvaluationException e) { return e.getErrorEval(); } - + if (Double.isNaN(d) || Double.isInfinite(d)) return ErrorEval.NUM_ERROR; - + return new NumberEval(d); } protected abstract double evaluate(double[] values) throws EvaluationException; - - + + /** * Maximum number of operands accepted by this function. * Subclasses may override to change default value. @@ -132,54 +143,58 @@ public abstract class MultiOperandNumericFunction implements Function { int height = ae.getHeight(); for (int rrIx=0; rrIx