Search in sources :

Example 1 with NumberPtg

use of org.apache.poi.ss.formula.ptg.NumberPtg in project poi by apache.

the class FormulaParser method getNumberPtgFromString.

/**
     * Get a PTG for an integer from its string representation.
     * return Int or Number Ptg based on size of input
     */
private static Ptg getNumberPtgFromString(String number1, String number2, String exponent) {
    StringBuilder number = new StringBuilder();
    if (number2 == null) {
        number.append(number1);
        if (exponent != null) {
            number.append('E');
            number.append(exponent);
        }
        String numberStr = number.toString();
        int intVal;
        try {
            intVal = Integer.parseInt(numberStr);
        } catch (NumberFormatException e) {
            return new NumberPtg(numberStr);
        }
        if (IntPtg.isInRange(intVal)) {
            return new IntPtg(intVal);
        }
        return new NumberPtg(numberStr);
    }
    if (number1 != null) {
        number.append(number1);
    }
    number.append('.');
    number.append(number2);
    if (exponent != null) {
        number.append('E');
        number.append(exponent);
    }
    return new NumberPtg(number.toString());
}
Also used : IntPtg(org.apache.poi.ss.formula.ptg.IntPtg) NumberPtg(org.apache.poi.ss.formula.ptg.NumberPtg)

Example 2 with NumberPtg

use of org.apache.poi.ss.formula.ptg.NumberPtg in project poi by apache.

the class FormulaParser method parseUnary.

private ParseNode parseUnary(boolean isPlus) {
    boolean numberFollows = IsDigit(look) || look == '.';
    ParseNode factor = powerFactor();
    if (numberFollows) {
        // + or - directly next to a number is parsed with the number
        Ptg token = factor.getToken();
        if (token instanceof NumberPtg) {
            if (isPlus) {
                return factor;
            }
            token = new NumberPtg(-((NumberPtg) token).getValue());
            return new ParseNode(token);
        }
        if (token instanceof IntPtg) {
            if (isPlus) {
                return factor;
            }
            int intVal = ((IntPtg) token).getValue();
            // note - cannot use IntPtg for negatives
            token = new NumberPtg(-intVal);
            return new ParseNode(token);
        }
    }
    return new ParseNode(isPlus ? UnaryPlusPtg.instance : UnaryMinusPtg.instance, factor);
}
Also used : IntPtg(org.apache.poi.ss.formula.ptg.IntPtg) NumberPtg(org.apache.poi.ss.formula.ptg.NumberPtg) ArrayPtg(org.apache.poi.ss.formula.ptg.ArrayPtg) AttrPtg(org.apache.poi.ss.formula.ptg.AttrPtg) PercentPtg(org.apache.poi.ss.formula.ptg.PercentPtg) RangePtg(org.apache.poi.ss.formula.ptg.RangePtg) AddPtg(org.apache.poi.ss.formula.ptg.AddPtg) EqualPtg(org.apache.poi.ss.formula.ptg.EqualPtg) UnaryMinusPtg(org.apache.poi.ss.formula.ptg.UnaryMinusPtg) NameXPtg(org.apache.poi.ss.formula.ptg.NameXPtg) RefPtg(org.apache.poi.ss.formula.ptg.RefPtg) DividePtg(org.apache.poi.ss.formula.ptg.DividePtg) GreaterThanPtg(org.apache.poi.ss.formula.ptg.GreaterThanPtg) MultiplyPtg(org.apache.poi.ss.formula.ptg.MultiplyPtg) StringPtg(org.apache.poi.ss.formula.ptg.StringPtg) ErrPtg(org.apache.poi.ss.formula.ptg.ErrPtg) Ptg(org.apache.poi.ss.formula.ptg.Ptg) NamePtg(org.apache.poi.ss.formula.ptg.NamePtg) MemAreaPtg(org.apache.poi.ss.formula.ptg.MemAreaPtg) NotEqualPtg(org.apache.poi.ss.formula.ptg.NotEqualPtg) ValueOperatorPtg(org.apache.poi.ss.formula.ptg.ValueOperatorPtg) ConcatPtg(org.apache.poi.ss.formula.ptg.ConcatPtg) UnaryPlusPtg(org.apache.poi.ss.formula.ptg.UnaryPlusPtg) GreaterEqualPtg(org.apache.poi.ss.formula.ptg.GreaterEqualPtg) LessThanPtg(org.apache.poi.ss.formula.ptg.LessThanPtg) BoolPtg(org.apache.poi.ss.formula.ptg.BoolPtg) IntersectionPtg(org.apache.poi.ss.formula.ptg.IntersectionPtg) AbstractFunctionPtg(org.apache.poi.ss.formula.ptg.AbstractFunctionPtg) IntPtg(org.apache.poi.ss.formula.ptg.IntPtg) LessEqualPtg(org.apache.poi.ss.formula.ptg.LessEqualPtg) UnionPtg(org.apache.poi.ss.formula.ptg.UnionPtg) FuncVarPtg(org.apache.poi.ss.formula.ptg.FuncVarPtg) SubtractPtg(org.apache.poi.ss.formula.ptg.SubtractPtg) FuncPtg(org.apache.poi.ss.formula.ptg.FuncPtg) OperandPtg(org.apache.poi.ss.formula.ptg.OperandPtg) MissingArgPtg(org.apache.poi.ss.formula.ptg.MissingArgPtg) MemFuncPtg(org.apache.poi.ss.formula.ptg.MemFuncPtg) OperationPtg(org.apache.poi.ss.formula.ptg.OperationPtg) PowerPtg(org.apache.poi.ss.formula.ptg.PowerPtg) AreaPtg(org.apache.poi.ss.formula.ptg.AreaPtg) ParenthesisPtg(org.apache.poi.ss.formula.ptg.ParenthesisPtg) NumberPtg(org.apache.poi.ss.formula.ptg.NumberPtg)

Example 3 with NumberPtg

use of org.apache.poi.ss.formula.ptg.NumberPtg in project poi by apache.

the class TestFormulaParser method testParseNumber.

@Test
public void testParseNumber() {
    IntPtg ip;
    // bug 33160
    ip = (IntPtg) parseSingleToken("40", IntPtg.class);
    assertEquals(40, ip.getValue());
    ip = (IntPtg) parseSingleToken("40000", IntPtg.class);
    assertEquals(40000, ip.getValue());
    // check the upper edge of the IntPtg range:
    ip = (IntPtg) parseSingleToken("65535", IntPtg.class);
    assertEquals(65535, ip.getValue());
    NumberPtg np = (NumberPtg) parseSingleToken("65536", NumberPtg.class);
    assertEquals(65536, np.getValue(), 0);
    np = (NumberPtg) parseSingleToken("65534.6", NumberPtg.class);
    assertEquals(65534.6, np.getValue(), 0);
}
Also used : IntPtg(org.apache.poi.ss.formula.ptg.IntPtg) NumberPtg(org.apache.poi.ss.formula.ptg.NumberPtg) Test(org.junit.Test)

Example 4 with NumberPtg

use of org.apache.poi.ss.formula.ptg.NumberPtg in project poi by apache.

the class TestFormulaParser method confirmUnary.

private static void confirmUnary(String formulaText, double val, Class<?>... expectedTokenTypes) {
    Ptg[] ptgs = parseFormula(formulaText);
    confirmTokenClasses(ptgs, expectedTokenTypes);
    Ptg ptg0 = ptgs[0];
    if (ptg0 instanceof IntPtg) {
        IntPtg intPtg = (IntPtg) ptg0;
        assertEquals((int) val, intPtg.getValue());
    } else if (ptg0 instanceof NumberPtg) {
        NumberPtg numberPtg = (NumberPtg) ptg0;
        assertEquals(val, numberPtg.getValue(), 0.0);
    } else {
        fail("bad ptg0 " + ptg0);
    }
}
Also used : IntPtg(org.apache.poi.ss.formula.ptg.IntPtg) NumberPtg(org.apache.poi.ss.formula.ptg.NumberPtg) ArrayPtg(org.apache.poi.ss.formula.ptg.ArrayPtg) AttrPtg(org.apache.poi.ss.formula.ptg.AttrPtg) PercentPtg(org.apache.poi.ss.formula.ptg.PercentPtg) RangePtg(org.apache.poi.ss.formula.ptg.RangePtg) AddPtg(org.apache.poi.ss.formula.ptg.AddPtg) EqualPtg(org.apache.poi.ss.formula.ptg.EqualPtg) UnaryMinusPtg(org.apache.poi.ss.formula.ptg.UnaryMinusPtg) NameXPtg(org.apache.poi.ss.formula.ptg.NameXPtg) RefPtg(org.apache.poi.ss.formula.ptg.RefPtg) DividePtg(org.apache.poi.ss.formula.ptg.DividePtg) GreaterThanPtg(org.apache.poi.ss.formula.ptg.GreaterThanPtg) MultiplyPtg(org.apache.poi.ss.formula.ptg.MultiplyPtg) Ref3DPtg(org.apache.poi.ss.formula.ptg.Ref3DPtg) StringPtg(org.apache.poi.ss.formula.ptg.StringPtg) ErrPtg(org.apache.poi.ss.formula.ptg.ErrPtg) Ptg(org.apache.poi.ss.formula.ptg.Ptg) Area3DPtg(org.apache.poi.ss.formula.ptg.Area3DPtg) NamePtg(org.apache.poi.ss.formula.ptg.NamePtg) MemAreaPtg(org.apache.poi.ss.formula.ptg.MemAreaPtg) ConcatPtg(org.apache.poi.ss.formula.ptg.ConcatPtg) UnaryPlusPtg(org.apache.poi.ss.formula.ptg.UnaryPlusPtg) BoolPtg(org.apache.poi.ss.formula.ptg.BoolPtg) IntersectionPtg(org.apache.poi.ss.formula.ptg.IntersectionPtg) AbstractFunctionPtg(org.apache.poi.ss.formula.ptg.AbstractFunctionPtg) IntPtg(org.apache.poi.ss.formula.ptg.IntPtg) UnionPtg(org.apache.poi.ss.formula.ptg.UnionPtg) FuncVarPtg(org.apache.poi.ss.formula.ptg.FuncVarPtg) SubtractPtg(org.apache.poi.ss.formula.ptg.SubtractPtg) FuncPtg(org.apache.poi.ss.formula.ptg.FuncPtg) MissingArgPtg(org.apache.poi.ss.formula.ptg.MissingArgPtg) MemFuncPtg(org.apache.poi.ss.formula.ptg.MemFuncPtg) PowerPtg(org.apache.poi.ss.formula.ptg.PowerPtg) AreaPtg(org.apache.poi.ss.formula.ptg.AreaPtg) ParenthesisPtg(org.apache.poi.ss.formula.ptg.ParenthesisPtg) NumberPtg(org.apache.poi.ss.formula.ptg.NumberPtg)

Aggregations

IntPtg (org.apache.poi.ss.formula.ptg.IntPtg)4 NumberPtg (org.apache.poi.ss.formula.ptg.NumberPtg)4 AbstractFunctionPtg (org.apache.poi.ss.formula.ptg.AbstractFunctionPtg)2 AddPtg (org.apache.poi.ss.formula.ptg.AddPtg)2 AreaPtg (org.apache.poi.ss.formula.ptg.AreaPtg)2 ArrayPtg (org.apache.poi.ss.formula.ptg.ArrayPtg)2 AttrPtg (org.apache.poi.ss.formula.ptg.AttrPtg)2 BoolPtg (org.apache.poi.ss.formula.ptg.BoolPtg)2 ConcatPtg (org.apache.poi.ss.formula.ptg.ConcatPtg)2 DividePtg (org.apache.poi.ss.formula.ptg.DividePtg)2 EqualPtg (org.apache.poi.ss.formula.ptg.EqualPtg)2 ErrPtg (org.apache.poi.ss.formula.ptg.ErrPtg)2 FuncPtg (org.apache.poi.ss.formula.ptg.FuncPtg)2 FuncVarPtg (org.apache.poi.ss.formula.ptg.FuncVarPtg)2 GreaterThanPtg (org.apache.poi.ss.formula.ptg.GreaterThanPtg)2 IntersectionPtg (org.apache.poi.ss.formula.ptg.IntersectionPtg)2 MemAreaPtg (org.apache.poi.ss.formula.ptg.MemAreaPtg)2 MemFuncPtg (org.apache.poi.ss.formula.ptg.MemFuncPtg)2 MissingArgPtg (org.apache.poi.ss.formula.ptg.MissingArgPtg)2 MultiplyPtg (org.apache.poi.ss.formula.ptg.MultiplyPtg)2