Example usage for org.apache.commons.lang.math NumberUtils createBigInteger

List of usage examples for org.apache.commons.lang.math NumberUtils createBigInteger

Introduction

In this page you can find the example usage for org.apache.commons.lang.math NumberUtils createBigInteger.

Prototype

public static BigInteger createBigInteger(String str) 

Source Link

Document

Convert a String to a BigInteger.

Returns null if the string is null.

Usage

From source file:com.mmj.app.common.core.lang.CollectionUtils.java

/**
 *  Number value = sum(new Long(0),array,"value");
 *//* w w w.j  a va2s  . c  om*/
public static <T extends Number> T sum(T rawValue, List<?> array, String property) {
    for (Object obj : array) {
        String propertyValue = null;
        try {
            propertyValue = BeanUtils.getProperty(obj, property);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (NumberUtils.isNumber(propertyValue) == false) {
            continue;
        }
        if (rawValue instanceof Integer) {
            rawValue = (T) new Integer(((Integer) rawValue) + NumberUtils.createInteger(propertyValue));
        } else if (rawValue instanceof Long) {
            rawValue = (T) new Long(((Long) rawValue) + NumberUtils.createLong(propertyValue));
        } else if (rawValue instanceof Float) {
            rawValue = (T) new Float(((Float) rawValue) + NumberUtils.createFloat(propertyValue));
        } else if (rawValue instanceof Double) {
            rawValue = (T) new Double(((Double) rawValue) + NumberUtils.createDouble(propertyValue));
        } else if (rawValue instanceof BigInteger) {
            rawValue = (T) NumberUtils.createBigInteger(propertyValue).add((BigInteger) rawValue);
        } else if (rawValue instanceof BigDecimal) {
            rawValue = (T) NumberUtils.createBigDecimal(propertyValue).add((BigDecimal) rawValue);
        }
    }
    return rawValue;
}

From source file:com.fengduo.bee.commons.core.lang.CollectionUtils.java

public static <T extends Number, E extends Object> T sum(T rawValue, List<E> array,
        ObjectConvert<E, ? extends Object> convert) {
    for (E obj : array) {
        String propertyValue = null;
        try {//from  w  ww . j a  va2  s  .c o  m
            Object value = convert.convert(obj);
            if (value == null) {
                continue;
            } else {
                propertyValue = value.toString();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (NumberUtils.isNumber(propertyValue) == false) {
            continue;
        }
        if (rawValue instanceof Integer) {
            rawValue = (T) new Integer(((Integer) rawValue) + NumberUtils.createInteger(propertyValue));
        } else if (rawValue instanceof Long) {
            rawValue = (T) new Long(((Long) rawValue) + NumberUtils.createLong(propertyValue));
        } else if (rawValue instanceof Float) {
            rawValue = (T) new Float(((Float) rawValue) + NumberUtils.createFloat(propertyValue));
        } else if (rawValue instanceof Double) {
            rawValue = (T) new Double(((Double) rawValue) + NumberUtils.createDouble(propertyValue));
        } else if (rawValue instanceof BigInteger) {
            rawValue = (T) NumberUtils.createBigInteger(propertyValue).add((BigInteger) rawValue);
        } else if (rawValue instanceof BigDecimal) {
            rawValue = (T) NumberUtils.createBigDecimal(propertyValue).add((BigDecimal) rawValue);
        }
    }
    return rawValue;
}

From source file:org.apache.tajo.engine.parser.HiveConverter.java

@Override
public LiteralValue visitConstant(HiveParser.ConstantContext ctx) {
    LiteralValue literalValue = null;/*from w  w  w  .  j  ava2 s . co  m*/

    if (ctx.StringLiteral() != null) {
        String value = ctx.StringLiteral().getText();
        String strValue = "";
        if ((value.startsWith("'") && value.endsWith("'")) || value.startsWith("\"") && value.endsWith("\"")) {
            strValue = value.substring(1, value.length() - 1);
        } else {
            strValue = value;
        }

        literalValue = new LiteralValue(strValue, LiteralValue.LiteralType.String);
    } else if (ctx.TinyintLiteral() != null) {
        literalValue = new LiteralValue(ctx.TinyintLiteral().getSymbol().getText(),
                LiteralValue.LiteralType.Unsigned_Integer);
    } else if (ctx.BigintLiteral() != null) {
        literalValue = new LiteralValue(ctx.BigintLiteral().getSymbol().getText(),
                LiteralValue.LiteralType.Unsigned_Large_Integer);
    } else if (ctx.DecimalLiteral() != null) {
        literalValue = new LiteralValue(ctx.DecimalLiteral().getSymbol().getText(),
                LiteralValue.LiteralType.Unsigned_Integer);
    } else if (ctx.Number() != null) {
        try {
            float floatValue = NumberUtils.createFloat(ctx.getText());
            literalValue = new LiteralValue(ctx.Number().getSymbol().getText(),
                    LiteralValue.LiteralType.Unsigned_Float);
        } catch (NumberFormatException nf) {
        }

        // TODO: double type

        try {
            BigInteger bigIntegerVallue = NumberUtils.createBigInteger(ctx.getText());
            literalValue = new LiteralValue(ctx.Number().getSymbol().getText(),
                    LiteralValue.LiteralType.Unsigned_Large_Integer);
        } catch (NumberFormatException nf) {
        }

        try {
            int intValue = NumberUtils.createInteger(ctx.getText());
            literalValue = new LiteralValue(ctx.Number().getSymbol().getText(),
                    LiteralValue.LiteralType.Unsigned_Integer);
        } catch (NumberFormatException nf) {
        }

    } else if (ctx.SmallintLiteral() != null) {
        literalValue = new LiteralValue(ctx.SmallintLiteral().getSymbol().getText(),
                LiteralValue.LiteralType.Unsigned_Integer);
    } else if (ctx.booleanValue() != null) {
        // TODO: boolean type
    }

    return literalValue;
}

From source file:org.apache.tajo.engine.parser.HiveQLAnalyzer.java

@Override
public LiteralValue visitConstant(HiveQLParser.ConstantContext ctx) {
    LiteralValue literalValue = null;/*from   www. j  av  a  2 s  .  c o  m*/

    if (ctx.StringLiteral() != null) {
        String value = ctx.StringLiteral().getText();
        String strValue = "";
        if ((value.startsWith("'") && value.endsWith("'")) || value.startsWith("\"") && value.endsWith("\"")) {
            strValue = value.substring(1, value.length() - 1);
        } else {
            strValue = value;
        }

        literalValue = new LiteralValue(strValue, LiteralValue.LiteralType.String);
    } else if (ctx.TinyintLiteral() != null) {
        literalValue = new LiteralValue(ctx.TinyintLiteral().getSymbol().getText(),
                LiteralValue.LiteralType.Unsigned_Integer);
    } else if (ctx.BigintLiteral() != null) {
        literalValue = new LiteralValue(ctx.BigintLiteral().getSymbol().getText(),
                LiteralValue.LiteralType.Unsigned_Large_Integer);
    } else if (ctx.DecimalLiteral() != null) {
        literalValue = new LiteralValue(ctx.DecimalLiteral().getSymbol().getText(),
                LiteralValue.LiteralType.Unsigned_Integer);
    } else if (ctx.Number() != null) {
        try {
            float floatValue = NumberUtils.createFloat(ctx.getText());
            literalValue = new LiteralValue(ctx.Number().getSymbol().getText(),
                    LiteralValue.LiteralType.Unsigned_Float);
        } catch (NumberFormatException nf) {
        }

        // TODO: double type

        try {
            BigInteger bigIntegerVallue = NumberUtils.createBigInteger(ctx.getText());
            literalValue = new LiteralValue(ctx.Number().getSymbol().getText(),
                    LiteralValue.LiteralType.Unsigned_Large_Integer);
        } catch (NumberFormatException nf) {
        }

        try {
            int intValue = NumberUtils.createInteger(ctx.getText());
            literalValue = new LiteralValue(ctx.Number().getSymbol().getText(),
                    LiteralValue.LiteralType.Unsigned_Integer);
        } catch (NumberFormatException nf) {
        }

    } else if (ctx.SmallintLiteral() != null) {
        literalValue = new LiteralValue(ctx.SmallintLiteral().getSymbol().getText(),
                LiteralValue.LiteralType.Unsigned_Integer);
    } else if (ctx.booleanValue() != null) {
        // TODO: boolean type
    }

    return literalValue;
}

From source file:org.cesecore.util.CertTools.java

/**
 * Gets a serial number in numeric form, it takes - either a hex encoded integer with length != 5 (x.509 certificate) - 5 letter numeric string
 * (cvc), will convert the number to an int - 5 letter alfanumeric string vi some numbers in it (cvc), will convert the numbers in it to a numeric
 * string (remove the letters) and convert to int - 5 letter alfanumeric string with only letters (cvc), will convert to integer from string with
 * radix 36/*from w ww.jav a  2s.c o m*/
 * 
 * @param sernoString
 * @return BigInteger
 */
public static BigInteger getSerialNumberFromString(String sernoString) {
    if (sernoString == null) {
        throw new IllegalArgumentException("getSerialNumberFromString: cert is null");
    }
    BigInteger ret;
    if (sernoString.length() != 5) {
        // This can not be a CVC certificate sequence, so it must be a hex encoded regular certificate serial number
        ret = new BigInteger(sernoString, 16);
    } else {
        // We try to handle the different cases of CVC certificate sequences, see StringTools.KEY_SEQUENCE_FORMAT
        try {
            if (NumberUtils.isNumber(sernoString)) {
                ret = NumberUtils.createBigInteger(sernoString);
            } else {
                // check if input is hexadecimal
                log.info(
                        "getSerialNumber: Sequence is not a numeric string, trying to extract numerical sequence part.");
                StringBuilder buf = new StringBuilder();
                for (int i = 0; i < sernoString.length(); i++) {
                    char c = sernoString.charAt(i);
                    if (CharUtils.isAsciiNumeric(c)) {
                        buf.append(c);
                    }
                }
                if (buf.length() > 0) {
                    ret = NumberUtils.createBigInteger(buf.toString());
                } else {
                    log.info(
                            "getSerialNumber: can not extract numeric sequence part, trying alfanumeric value (radix 36).");
                    if (sernoString.matches("[0-9A-Z]{1,5}")) {
                        int numSeq = Integer.parseInt(sernoString, 36);
                        ret = BigInteger.valueOf(numSeq);
                    } else {
                        log.info("getSerialNumber: Sequence does not contain any numeric parts, returning 0.");
                        ret = BigInteger.valueOf(0);
                    }
                }
            }
        } catch (NumberFormatException e) {
            // If we can't make the sequence into a serial number big integer, set it to 0
            log.debug("getSerialNumber: NumberFormatException for sequence: " + sernoString);
            ret = BigInteger.valueOf(0);
        }
    }
    return ret;
}

From source file:org.ejbca.util.CertTools.java

/** Gets a serial number in numeric form, it takes
 * - either a hex encoded integer with length != 5 (x.509 certificate)
 * - 5 letter numeric string (cvc), will convert the number to an int
 * - 5 letter alfanumeric string vi some numbers in it (cvc), will convert the numbers in it to a numeric string (remove the letters) and convert to int
 * - 5 letter alfanumeric string with only letters (cvc), will convert to integer from string with radix 36
 * /* ww  w.j  a v a 2  s.co  m*/
 * @param sernoString
 * @return BigInteger
 */
public static BigInteger getSerialNumberFromString(String sernoString) {
    BigInteger ret;
    if (sernoString.length() != 5) {
        // This can not be a CVC certificate sequence, so it must be a hex encoded regular certificate serial number
        ret = new BigInteger(sernoString, 16);
    } else {
        // We try to handle the different cases of CVC certificate sequences, see StringTools.KEY_SEQUENCE_FORMAT
        try {
            if (NumberUtils.isNumber(sernoString)) {
                ret = NumberUtils.createBigInteger(sernoString);
            } else {
                // check if input is hexadecimal
                log.info(
                        "getSerialNumber: Sequence is not a numeric string, trying to extract numerical sequence part.");
                final StringBuilder buf = new StringBuilder();
                for (int i = 0; i < sernoString.length(); i++) {
                    char c = sernoString.charAt(i);
                    if (CharUtils.isAsciiNumeric(c)) {
                        buf.append(c);
                    }
                }
                if (buf.length() > 0) {
                    ret = NumberUtils.createBigInteger(buf.toString());
                } else {
                    log.info(
                            "getSerialNumber: can not extract numeric sequence part, trying alfanumeric value (radix 36).");
                    if (sernoString.matches("[0-9A-Z]{1,5}")) {
                        int numSeq = Integer.parseInt(sernoString, 36);
                        ret = BigInteger.valueOf(numSeq);
                    } else {
                        log.info("getSerialNumber: Sequence does not contain any numeric parts, returning 0.");
                        ret = BigInteger.valueOf(0);
                    }
                }
            }
        } catch (NumberFormatException e) {
            // If we can't make the sequence into a serial number big integer, set it to 0
            log.debug("getSerialNumber: NumberFormatException for sequence: " + sernoString);
            ret = BigInteger.valueOf(0);
        }
    }
    return ret;
}

From source file:org.locationtech.udig.ui.NumberCellEditor.java

private Number convertToNumber() {
    if (cls == Integer.class) {
        return NumberUtils.createInteger(StringUtils.isNotEmpty(text.getText()) ? text.getText() : null);
    } else if (cls == Long.class) {
        return NumberUtils.createLong(StringUtils.isNotEmpty(text.getText()) ? text.getText() : null);
    } else if (cls == BigInteger.class) {
        return NumberUtils.createBigInteger(StringUtils.isNotEmpty(text.getText()) ? text.getText() : null);
    } else if (cls == BigDecimal.class) {
        return NumberUtils.createBigDecimal(StringUtils.isNotEmpty(text.getText()) ? text.getText() : null);
    } else if (cls == Double.class) {
        return NumberUtils.createDouble(StringUtils.isNotEmpty(text.getText()) ? text.getText() : null);
    } else if (cls == Float.class) {
        return NumberUtils.createFloat(StringUtils.isNotEmpty(text.getText()) ? text.getText() : null);
    } else if (cls == Short.class) {
        if (StringUtils.isNotEmpty(text.getText())) {
            return Short.valueOf(text.getText());
        } else {/*from   w  w  w  . j  a v  a  2  s  .  c om*/
            return null;
        }
    }
    return null;
}