FileDocCategorySizeDatePackage
XYNumericFunction.javaAPI DocApache Poi 3.0.17420Sun Mar 11 12:59:30 GMT 2007org.apache.poi.hssf.record.formula.functions

XYNumericFunction

public abstract class XYNumericFunction extends NumericFunction
author
Amol S. Deshmukh < amolweb at ya hoo dot com >

Fields Summary
protected static final int
X
protected static final int
Y
private static final org.apache.poi.hssf.record.formula.eval.ValueEvalToNumericXlator
DEFAULT_NUM_XLATOR
Constructors Summary
Methods Summary
protected static double[]ensureCapacity(double[] arr, int pos)

        double[] temp = arr;
        while (pos >= arr.length) {
            arr = new double[arr.length << 2];
        }
        if (temp.length != arr.length)
            System.arraycopy(temp, 0, arr, 0, temp.length);
        return arr;
    
protected static doublegetDoubleValue(org.apache.poi.hssf.record.formula.eval.Eval eval)

        double retval = 0;
        if (eval instanceof NumberEval) {
            NumberEval ne = (NumberEval) eval;
            retval = ne.getNumberValue();
        }
        else if (eval instanceof RefEval) {
            RefEval re = (RefEval) eval;
            ValueEval ve = re.getInnerValueEval();
                retval = (ve instanceof NumberEval)
                    ? ((NumberEval) ve).getNumberValue()
                    : Double.NaN;
        }
        else if (eval instanceof ErrorEval) {
            retval = Double.NaN;
        }
        return retval;
    
protected intgetMaxNumOperands()

        return 30;
    
protected double[][]getNumberArray(org.apache.poi.hssf.record.formula.eval.Eval[] xops, org.apache.poi.hssf.record.formula.eval.Eval[] yops, int srcRow, short srcCol)
Returns a double array that contains values for the numeric cells from among the list of operands. Blanks and Blank equivalent cells are ignored. Error operands or cells containing operands of type that are considered invalid and would result in #VALUE! error in excel cause this function to return null.

param
operands
param
srcRow
param
srcCol
return

        double[][] retval = new double[2][30];
        int count = 0;
        
        if (xops.length > getMaxNumOperands() 
                || yops.length > getMaxNumOperands()
                || xops.length != yops.length) {
            retval = null;
        }
        else {
            
            for (int i=0, iSize=xops.length; i<iSize; i++) {
                Eval xEval = xops[i];
                Eval yEval = yops[i];
                
                if (isNumberEval(xEval) && isNumberEval(yEval)) {
                    retval[X] = ensureCapacity(retval[X], count);
                    retval[Y] = ensureCapacity(retval[Y], count);
                    retval[X][count] = getDoubleValue(xEval);
                    retval[Y][count] = getDoubleValue(yEval);
                    if (Double.isNaN(retval[X][count]) || Double.isNaN(retval[Y][count])) {
                        retval = null;
                        break;
                    }
                    count++;
                }
            }
        }
        
        if (retval != null) {
            double[][] temp = retval;
            retval[X] = trimToSize(retval[X], count);
            retval[Y] = trimToSize(retval[Y], count);
        }
        
        return retval;
    
protected double[][]getValues(org.apache.poi.hssf.record.formula.eval.Eval[] operands, int srcCellRow, short srcCellCol)

        double[][] retval = null;
        
        outer: do {
            if (operands.length == 2) {
                Eval[] xEvals = new Eval[1];
                Eval[] yEvals = new Eval[1];
                if (operands[X] instanceof AreaEval) {
                    AreaEval ae = (AreaEval) operands[0];
                    xEvals = ae.getValues();
                }
                else if (operands[X] instanceof ErrorEval) {
                    break outer;
                }
                else {
                    xEvals[0] = operands[X];
                }
                
                if (operands[Y] instanceof AreaEval) {
                    AreaEval ae = (AreaEval) operands[Y];
                    yEvals = ae.getValues();
                }
                else if (operands[Y] instanceof ErrorEval) {
                    break outer;
                }
                else {
                    yEvals[0] = operands[Y];
                }
                
                retval = getNumberArray(xEvals, yEvals, srcCellRow, srcCellCol);
            }
        } while (false);
        
        return retval;
    
protected org.apache.poi.hssf.record.formula.eval.ValueEvalToNumericXlatorgetXlator()
this is the default impl for the factory method getXlator of the super class NumericFunction. Subclasses can override this method if they desire to return a different ValueEvalToNumericXlator instance than the default.


                                         
       
        return DEFAULT_NUM_XLATOR;
    
protected static booleanisNumberEval(org.apache.poi.hssf.record.formula.eval.Eval eval)

        boolean retval = false;
        
        if (eval instanceof NumberEval) {
            retval = true;
        }
        else if (eval instanceof RefEval) {
            RefEval re = (RefEval) eval;
            ValueEval ve = re.getInnerValueEval();
            retval = (ve instanceof NumberEval);
        }
        
        return retval;
    
protected static double[]trimToSize(double[] arr, int len)

        double[] tarr = arr;
        if (arr.length > len) {
            tarr = new double[len];
            System.arraycopy(arr, 0, tarr, 0, len);
        }
        return tarr;