Java 类org.springframework.util.NumberUtils 实例源码

项目:lams    文件:SingleColumnRowMapper.java   
/**
 * Convert the given column value to the specified required type.
 * Only called if the extracted column value does not match already.
 * <p>If the required type is String, the value will simply get stringified
 * via {@code toString()}. In case of a Number, the value will be
 * converted into a Number, either through number conversion or through
 * String parsing (depending on the value type).
 * @param value the column value as extracted from {@code getColumnValue()}
 * (never {@code null})
 * @param requiredType the type that each result object is expected to match
 * (never {@code null})
 * @return the converted value
 * @see #getColumnValue(java.sql.ResultSet, int, Class)
 */
@SuppressWarnings("unchecked")
protected Object convertValueToRequiredType(Object value, Class<?> requiredType) {
    if (String.class.equals(requiredType)) {
        return value.toString();
    }
    else if (Number.class.isAssignableFrom(requiredType)) {
        if (value instanceof Number) {
            // Convert original Number to target Number class.
            return NumberUtils.convertNumberToTargetClass(((Number) value), (Class<Number>) requiredType);
        }
        else {
            // Convert stringified value to target Number class.
            return NumberUtils.parseNumber(value.toString(),(Class<Number>) requiredType);
        }
    }
    else {
        throw new IllegalArgumentException(
                "Value [" + value + "] is of type [" + value.getClass().getName() +
                "] and cannot be converted to required type [" + requiredType.getName() + "]");
    }
}
项目:lams    文件:CustomNumberEditor.java   
/**
 * Parse the Number from the given text, using the specified NumberFormat.
 */
@Override
public void setAsText(String text) throws IllegalArgumentException {
    if (this.allowEmpty && !StringUtils.hasText(text)) {
        // Treat empty String as null value.
        setValue(null);
    }
    else if (this.numberFormat != null) {
        // Use given NumberFormat for parsing text.
        setValue(NumberUtils.parseNumber(text, this.numberClass, this.numberFormat));
    }
    else {
        // Use default valueOf methods for parsing text.
        setValue(NumberUtils.parseNumber(text, this.numberClass));
    }
}
项目:spring4-understanding    文件:SingleColumnRowMapper.java   
/**
 * Convert the given column value to the specified required type.
 * Only called if the extracted column value does not match already.
 * <p>If the required type is String, the value will simply get stringified
 * via {@code toString()}. In case of a Number, the value will be
 * converted into a Number, either through number conversion or through
 * String parsing (depending on the value type).
 * @param value the column value as extracted from {@code getColumnValue()}
 * (never {@code null})
 * @param requiredType the type that each result object is expected to match
 * (never {@code null})
 * @return the converted value
 * @see #getColumnValue(java.sql.ResultSet, int, Class)
 */
@SuppressWarnings("unchecked")
protected Object convertValueToRequiredType(Object value, Class<?> requiredType) {
    if (String.class == requiredType) {
        return value.toString();
    }
    else if (Number.class.isAssignableFrom(requiredType)) {
        if (value instanceof Number) {
            // Convert original Number to target Number class.
            return NumberUtils.convertNumberToTargetClass(((Number) value), (Class<Number>) requiredType);
        }
        else {
            // Convert stringified value to target Number class.
            return NumberUtils.parseNumber(value.toString(),(Class<Number>) requiredType);
        }
    }
    else {
        throw new IllegalArgumentException(
                "Value [" + value + "] is of type [" + value.getClass().getName() +
                "] and cannot be converted to required type [" + requiredType.getName() + "]");
    }
}
项目:spring4-understanding    文件:CustomNumberEditor.java   
/**
 * Parse the Number from the given text, using the specified NumberFormat.
 */
@Override
public void setAsText(String text) throws IllegalArgumentException {
    if (this.allowEmpty && !StringUtils.hasText(text)) {
        // Treat empty String as null value.
        setValue(null);
    }
    else if (this.numberFormat != null) {
        // Use given NumberFormat for parsing text.
        setValue(NumberUtils.parseNumber(text, this.numberClass, this.numberFormat));
    }
    else {
        // Use default valueOf methods for parsing text.
        setValue(NumberUtils.parseNumber(text, this.numberClass));
    }
}
项目:meazza    文件:CustomStringToNumberConverterFactory.java   
@Override
public T convert(String source) {
    if (StringUtils.isBlank(source)) {
        return null;
    }

    String trimmed = StringUtils.trim(source);

    T result = null;
    try {
        result = NumberUtils.parseNumber(trimmed, targetType);
    } catch (Exception e) {
        // Just ignore convert error
    }
    return result;
}
项目:spring    文件:CustomNumberEditor.java   
/**
 * Parse the Number from the given text, using the specified NumberFormat.
 */
@Override
public void setAsText(String text) throws IllegalArgumentException {
    if (this.allowEmpty && !StringUtils.hasText(text)) {
        // Treat empty String as null value.
        setValue(null);
    }
    else if (this.numberFormat != null) {
        // Use given NumberFormat for parsing text.
        setValue(NumberUtils.parseNumber(text, this.numberClass, this.numberFormat));
    }
    else {
        // Use default valueOf methods for parsing text.
        setValue(NumberUtils.parseNumber(text, this.numberClass));
    }
}
项目:spring-data-keyvalue-redis    文件:BinaryConverters.java   
@Override
public Date convert(byte[] source) {

    if (source == null || source.length == 0) {
        return null;
    }

    String value = toString(source);
    try {
        return new Date(NumberUtils.parseNumber(value, Long.class));
    } catch (NumberFormatException nfe) {
        // ignore
    }

    try {
        return DateFormat.getInstance().parse(value);
    } catch (ParseException e) {
        // ignore
    }

    throw new IllegalArgumentException("Cannot parse date out of " + source);
}
项目:eHMP    文件:SimpleLineMapper.java   
/**
 * Convert the given line value to the specified required type.
 * Only called if the extracted line value does not match already.
 * <p>If the required type is String, the value will simply get stringified
 * via <code>toString()</code>. In case of a Boolean, the value will be converted into a
 * Boolean using String parsing.  In case of a Number, the value will be
 * converted into a Number, either through number conversion or through
 * String parsing (depending on the value type).
 *
 * @param value        the column value as extracted from <code>getColumnValue()</code>
 *                     (never <code>null</code>)
 * @param requiredType the type that each result object is expected to match
 *                     (never <code>null</code>)
 * @return the converted value
 */
@SuppressWarnings("unchecked")
protected Object convertValueToRequiredType(Object value, Class requiredType) {
    if (String.class.equals(requiredType)) {
        return value.toString();
    } else if (Number.class.isAssignableFrom(requiredType)) {
        if (value instanceof Number) {
            // Convert original Number to target Number class.
            return NumberUtils.convertNumberToTargetClass(((Number) value), requiredType);
        } else {
            // Convert stringified value to target Number class.
            return NumberUtils.parseNumber(value.toString(), requiredType);
        }
    } else if (Boolean.class.isAssignableFrom(requiredType)) {
        return Boolean.valueOf(value.toString());
    } else {
        throw new IllegalArgumentException(
                "Value [" + value + "] is of type [" + value.getClass().getName() +
                        "] and cannot be converted to required type [" + requiredType.getName() + "]");
    }
}
项目:npjt-extra    文件:QueryType.java   
@SuppressWarnings("unchecked")
private static <T> AffectedRowCountAndKey<T> executeUpdateAndKeepKeys(
        String template, Method method,
        NamedParameterJdbcTemplate jdbc, SqlParameterSource parameters) {

    Class<T> keyClass = (Class<T>) ((ParameterizedType) method.getGenericReturnType()).getActualTypeArguments()[0];

    KeyHolder keyHolder = new GeneratedKeyHolder();

    int result = jdbc.update(template, parameters, keyHolder);
    Map<String, Object> keys = keyHolder.getKeys();
    Object key;
    if (keys.size() > 1) {
        AutoGeneratedKey spec = Objects.requireNonNull(withType(method.getDeclaredAnnotations(), AutoGeneratedKey.class), "more than one key for query " + template + ": annotation @AutoGeneratedKey required");
        key = Objects.requireNonNull(keys.get(spec.value()), "the key with name " + spec.value() + " has returned null for query " + template + ": required a non null key");
    } else if (Number.class.isAssignableFrom(keyClass)) {
        Class<? extends Number> c = (Class<? extends Number>) keyClass;
        return new AffectedRowCountAndKey<>(result, (T) NumberUtils.convertNumberToTargetClass(keyHolder.getKey(), c));
    } else {
        key = keys.values().iterator().next();
    }
    return new AffectedRowCountAndKey<>(result, keyClass.cast(key));
}
项目:class-guard    文件:DataAccessUtils.java   
/**
 * Return a unique result object from the given Collection.
 * Throws an exception if 0 or more than 1 result objects found,
 * of if the unique result object is not convertable to the
 * specified required type.
 * @param results the result Collection (can be {@code null})
 * @return the unique result object
 * @throws IncorrectResultSizeDataAccessException if more than one
 * result object has been found in the given Collection
 * @throws EmptyResultDataAccessException if no result object
 * at all has been found in the given Collection
 * @throws TypeMismatchDataAccessException if the unique object does
 * not match the specified required type
 */
@SuppressWarnings("unchecked")
public static <T> T objectResult(Collection<?> results, Class<T> requiredType)
        throws IncorrectResultSizeDataAccessException, TypeMismatchDataAccessException {

    Object result = requiredUniqueResult(results);
    if (requiredType != null && !requiredType.isInstance(result)) {
        if (String.class.equals(requiredType)) {
            result = result.toString();
        }
        else if (Number.class.isAssignableFrom(requiredType) && Number.class.isInstance(result)) {
            try {
                result = NumberUtils.convertNumberToTargetClass(((Number) result), (Class<? extends Number>) requiredType);
            }
            catch (IllegalArgumentException ex) {
                throw new TypeMismatchDataAccessException(ex.getMessage());
            }
        }
        else {
            throw new TypeMismatchDataAccessException(
                    "Result object is of type [" + result.getClass().getName() +
                    "] and could not be converted to required type [" + requiredType.getName() + "]");
        }
    }
    return (T) result;
}
项目:class-guard    文件:SingleColumnRowMapper.java   
/**
 * Convert the given column value to the specified required type.
 * Only called if the extracted column value does not match already.
 * <p>If the required type is String, the value will simply get stringified
 * via {@code toString()}. In case of a Number, the value will be
 * converted into a Number, either through number conversion or through
 * String parsing (depending on the value type).
 * @param value the column value as extracted from {@code getColumnValue()}
 * (never {@code null})
 * @param requiredType the type that each result object is expected to match
 * (never {@code null})
 * @return the converted value
 * @see #getColumnValue(java.sql.ResultSet, int, Class)
 */
@SuppressWarnings("unchecked")
protected Object convertValueToRequiredType(Object value, Class requiredType) {
    if (String.class.equals(requiredType)) {
        return value.toString();
    }
    else if (Number.class.isAssignableFrom(requiredType)) {
        if (value instanceof Number) {
            // Convert original Number to target Number class.
            return NumberUtils.convertNumberToTargetClass(((Number) value), requiredType);
        }
        else {
            // Convert stringified value to target Number class.
            return NumberUtils.parseNumber(value.toString(), requiredType);
        }
    }
    else {
        throw new IllegalArgumentException(
                "Value [" + value + "] is of type [" + value.getClass().getName() +
                "] and cannot be converted to required type [" + requiredType.getName() + "]");
    }
}
项目:class-guard    文件:CustomNumberEditor.java   
/**
 * Parse the Number from the given text, using the specified NumberFormat.
 */
@Override
public void setAsText(String text) throws IllegalArgumentException {
    if (this.allowEmpty && !StringUtils.hasText(text)) {
        // Treat empty String as null value.
        setValue(null);
    }
    else if (this.numberFormat != null) {
        // Use given NumberFormat for parsing text.
        setValue(NumberUtils.parseNumber(text, this.numberClass, this.numberFormat));
    }
    else {
        // Use default valueOf methods for parsing text.
        setValue(NumberUtils.parseNumber(text, this.numberClass));
    }
}
项目:pinpoint    文件:PinpointServerClusterPoint.java   
private List<TCommandType> newSupportCommandList(Map<Object, Object> properties) {
    final Object supportCommandCodeList = properties.get(HandshakePropertyType.SUPPORT_COMMAND_LIST.getName());
    if (!(supportCommandCodeList instanceof List)) {
        return Collections.emptyList();
    }

    final List<TCommandType> result = new ArrayList<>();
    for (Object supportCommandCode : (List)supportCommandCodeList) {
        if (supportCommandCode instanceof Number) {
            TCommandType commandType = TCommandType.getType(NumberUtils.convertNumberToTargetClass((Number) supportCommandCode, Short.class));
            if (commandType != null) {
                result.add(commandType);
            }
        }
    }
    return result;

}
项目:CommonDao    文件:ValueUtils.java   
/**
 * 将value转换为指定类型的值,转换不成功则返回原始的value
 * @param value             value
 * @param targetClazz       target class
 * @return                  re value
 * @throws ParseException   解析异常
 */
@SuppressWarnings("unchecked")
public static Object conversionType(Object value, Class<?> targetClazz) throws ParseException {
    if (value == null) {
        return null;
    }
    if (String.class.equals(targetClazz)) {
        // 字符串
        return String.valueOf(value);
    } else if (Date.class.equals(targetClazz)) {
        if (value instanceof Date) {
            return value;
        } else {
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return df.parse(String.valueOf(value));
        }
    } else if (Number.class.isAssignableFrom(targetClazz)) {
        // 数值
        return NumberUtils.parseNumber(String.valueOf(value), (Class<Number>)targetClazz);
    }
    return value;
}
项目:CommonDao    文件:DefaultCommonDao.java   
@SuppressWarnings("unchecked")
protected Object convertValueToRequiredType(Object value, Class<?> requiredType) {
    if (value == null) {
        return null;
    }
    if (String.class.equals(requiredType)) {
        return String.valueOf(value);
    }
    else if (Number.class.isAssignableFrom(requiredType)) {
        if (value instanceof Number) {
            // Convert original Number to target Number class.
            return NumberUtils.convertNumberToTargetClass(((Number) value), (Class<Number>)requiredType);
        }
        else {
            // Convert stringified value to target Number class.
            return NumberUtils.parseNumber(value.toString(), (Class<Number>)requiredType);
        }
    }
    else {
        throw new IllegalArgumentException(
                "Value [" + value + "] is of type [" + value.getClass().getName() +
                "] and cannot be converted to required type [" + requiredType.getName() + "]");
    }
}
项目:CommonDao    文件:AbstractPaginatedRepairer.java   
@SuppressWarnings("unchecked")
protected Object convertValueToRequiredType(Object value, Class<?> requiredType) {
    if (value == null) {
        return null;
    }
    if (String.class.equals(requiredType)) {
        return String.valueOf(value);
    }
    else if (Number.class.isAssignableFrom(requiredType)) {
        if (value instanceof Number) {
            // Convert original Number to target Number class.
            return NumberUtils.convertNumberToTargetClass(((Number) value), (Class<Number>)requiredType);
        }
        else {
            // Convert stringified value to target Number class.
            return NumberUtils.parseNumber(value.toString(), (Class<Number>)requiredType);
        }
    }
    else {
        throw new IllegalArgumentException(
                "Value [" + value + "] is of type [" + value.getClass().getName() +
                "] and cannot be converted to required type [" + requiredType.getName() + "]");
    }
}
项目:jixture    文件:TestFileFixtureTransformer.java   
@Before
public void setUp() throws NoSuchFieldException {
    GenericConversionService genericConversionService = new GenericConversionService();

    genericConversionService.addConverter(new Converter<String, Integer>() {
        @Override
        public Integer convert(String source) {
            if (0 == source.length()) {
                return null;
            }
            return NumberUtils.parseNumber(source, Integer.class);
        }
    });

    Mockito.when(conversionServiceFactoryBean.getObject()).thenReturn(genericConversionService);

    mockCartEntry();
    mockProduct();
}
项目:excel-utils    文件:ConvertUtils.java   
@SuppressWarnings("unchecked")
public static Object convertIfNeccesary(Object obj, Class<?> type,String dateFormat) throws ParseException {
    if(type.equals(obj.getClass())){
        return obj;
    }
    if(String.class.equals(type)){
        if(Date.class.isAssignableFrom(obj.getClass())){
            SimpleDateFormat format = getDateFormat(dateFormat);
            return format.format((Date)obj);
        }
        return obj.toString();
    }
    if(Number.class.isAssignableFrom(type)){
        if(Number.class.isAssignableFrom(obj.getClass())){
            return NumberUtils.convertNumberToTargetClass((Number)obj, (Class<Number>)type);
        }else if(String.class.equals(obj.getClass())){
            return NumberUtils.parseNumber(obj.toString(), (Class<Number>)type);
        }
    }
    if(ClassUtils.isBaseNumberType(type)){//基本类型之中的数字类型
        return NumberUtils.convertNumberToTargetClass((Number)obj, ClassUtils.getNumberWrapClass(type));
    }
    if(char.class.equals(type) || Character.class.equals(type)){
        if(obj.toString().length() > 0 ){
            return new Character(obj.toString().charAt(0));             
        }
        return null;
    }
    if(String.class.equals(obj.getClass())){
        if(Date.class.equals(type)){    
            SimpleDateFormat simpleDateFormat = getDateFormat(dateFormat);
            return simpleDateFormat.parse(obj.toString());
        }
        if(Boolean.class.equals(type)){
            return Boolean.parseBoolean(obj.toString());
        }
    }
    return null;
}
项目:lams    文件:DataAccessUtils.java   
/**
 * Return a unique result object from the given Collection.
 * Throws an exception if 0 or more than 1 result objects found,
 * of if the unique result object is not convertable to the
 * specified required type.
 * @param results the result Collection (can be {@code null})
 * @return the unique result object
 * @throws IncorrectResultSizeDataAccessException if more than one
 * result object has been found in the given Collection
 * @throws EmptyResultDataAccessException if no result object
 * at all has been found in the given Collection
 * @throws TypeMismatchDataAccessException if the unique object does
 * not match the specified required type
 */
@SuppressWarnings("unchecked")
public static <T> T objectResult(Collection<?> results, Class<T> requiredType)
        throws IncorrectResultSizeDataAccessException, TypeMismatchDataAccessException {

    Object result = requiredUniqueResult(results);
    if (requiredType != null && !requiredType.isInstance(result)) {
        if (String.class.equals(requiredType)) {
            result = result.toString();
        }
        else if (Number.class.isAssignableFrom(requiredType) && Number.class.isInstance(result)) {
            try {
                result = NumberUtils.convertNumberToTargetClass(((Number) result), (Class<? extends Number>) requiredType);
            }
            catch (IllegalArgumentException ex) {
                throw new TypeMismatchDataAccessException(ex.getMessage());
            }
        }
        else {
            throw new TypeMismatchDataAccessException(
                    "Result object is of type [" + result.getClass().getName() +
                    "] and could not be converted to required type [" + requiredType.getName() + "]");
        }
    }
    return (T) result;
}
项目:lams    文件:CustomNumberEditor.java   
/**
 * Coerce a Number value into the required target class, if necessary.
 */
@Override
public void setValue(Object value) {
    if (value instanceof Number) {
        super.setValue(NumberUtils.convertNumberToTargetClass((Number) value, this.numberClass));
    }
    else {
        super.setValue(value);
    }
}
项目:lams    文件:OpDivide.java   
@Override
public TypedValue getValueInternal(ExpressionState state) throws EvaluationException {
    Object leftOperand = getLeftOperand().getValueInternal(state).getValue();
    Object rightOperand = getRightOperand().getValueInternal(state).getValue();

    if (leftOperand instanceof Number && rightOperand instanceof Number) {
        Number leftNumber = (Number) leftOperand;
        Number rightNumber = (Number) rightOperand;

        if (leftNumber instanceof BigDecimal || rightNumber instanceof BigDecimal) {
            BigDecimal leftBigDecimal = NumberUtils.convertNumberToTargetClass(leftNumber, BigDecimal.class);
            BigDecimal rightBigDecimal = NumberUtils.convertNumberToTargetClass(rightNumber, BigDecimal.class);
            int scale = Math.max(leftBigDecimal.scale(), rightBigDecimal.scale());
            return new TypedValue(leftBigDecimal.divide(rightBigDecimal, scale, RoundingMode.HALF_EVEN));
        }

        if (leftNumber instanceof Double || rightNumber instanceof Double) {
            return new TypedValue(leftNumber.doubleValue() / rightNumber.doubleValue());
        }
        if (leftNumber instanceof Float || rightNumber instanceof Float) {
            return new TypedValue(leftNumber.floatValue() / rightNumber.floatValue());
        }
        if (leftNumber instanceof Long || rightNumber instanceof Long) {
            return new TypedValue(leftNumber.longValue() / rightNumber.longValue());
        }

        // TODO what about non-int result of the division?
        return new TypedValue(leftNumber.intValue() / rightNumber.intValue());
    }

    return state.operate(Operation.DIVIDE, leftOperand, rightOperand);
}
项目:lams    文件:OpModulus.java   
@Override
public TypedValue getValueInternal(ExpressionState state) throws EvaluationException {
    Object leftOperand = getLeftOperand().getValueInternal(state).getValue();
    Object rightOperand = getRightOperand().getValueInternal(state).getValue();
    if (leftOperand instanceof Number && rightOperand instanceof Number) {
        Number leftNumber = (Number) leftOperand;
        Number rightNumber = (Number) rightOperand;

        if (leftNumber instanceof BigDecimal || rightNumber instanceof BigDecimal) {
            BigDecimal leftBigDecimal = NumberUtils.convertNumberToTargetClass(leftNumber, BigDecimal.class);
            BigDecimal rightBigDecimal = NumberUtils.convertNumberToTargetClass(rightNumber, BigDecimal.class);
            return new TypedValue(leftBigDecimal.remainder(rightBigDecimal));
        }

        if (leftNumber instanceof Double || rightNumber instanceof Double) {
            return new TypedValue(leftNumber.doubleValue() % rightNumber.doubleValue());
        }

        if (leftNumber instanceof Float || rightNumber instanceof Float) {
            return new TypedValue(leftNumber.floatValue() % rightNumber.floatValue());
        }

        if (leftNumber instanceof Long || rightNumber instanceof Long) {
            return new TypedValue(leftNumber.longValue() % rightNumber.longValue());
        }

        return new TypedValue(leftNumber.intValue() % rightNumber.intValue());
    }

    return state.operate(Operation.MODULUS, leftOperand, rightOperand);
}
项目:lams    文件:OpGE.java   
@Override
public BooleanTypedValue getValueInternal(ExpressionState state) throws EvaluationException {
    Object left = getLeftOperand().getValueInternal(state).getValue();
    Object right = getRightOperand().getValueInternal(state).getValue();
    if (left instanceof Number && right instanceof Number) {
        Number leftNumber = (Number) left;
        Number rightNumber = (Number) right;

        if (leftNumber instanceof BigDecimal || rightNumber instanceof BigDecimal) {
            BigDecimal leftBigDecimal = NumberUtils.convertNumberToTargetClass(leftNumber, BigDecimal.class);
            BigDecimal rightBigDecimal = NumberUtils.convertNumberToTargetClass(rightNumber, BigDecimal.class);
            return BooleanTypedValue.forValue(leftBigDecimal.compareTo(rightBigDecimal) >= 0);
        }

        if (leftNumber instanceof Double || rightNumber instanceof Double) {
            return BooleanTypedValue.forValue(leftNumber.doubleValue() >= rightNumber.doubleValue());
        }

        if (leftNumber instanceof Float || rightNumber instanceof Float) {
            return BooleanTypedValue.forValue(leftNumber.floatValue() >= rightNumber.floatValue());
        }

        if (leftNumber instanceof Long || rightNumber instanceof Long) {
            return BooleanTypedValue.forValue(leftNumber.longValue() >= rightNumber.longValue());
        }
        return BooleanTypedValue.forValue(leftNumber.intValue() >= rightNumber.intValue());
    }
    return BooleanTypedValue.forValue(state.getTypeComparator().compare(left, right) >= 0);
}
项目:lams    文件:OpLT.java   
@Override
public BooleanTypedValue getValueInternal(ExpressionState state) throws EvaluationException {
    Object left = getLeftOperand().getValueInternal(state).getValue();
    Object right = getRightOperand().getValueInternal(state).getValue();

    if (left instanceof Number && right instanceof Number) {
        Number leftNumber = (Number) left;
        Number rightNumber = (Number) right;

        if (leftNumber instanceof BigDecimal || rightNumber instanceof BigDecimal) {
            BigDecimal leftBigDecimal = NumberUtils.convertNumberToTargetClass(leftNumber, BigDecimal.class);
            BigDecimal rightBigDecimal = NumberUtils.convertNumberToTargetClass(rightNumber, BigDecimal.class);
            return BooleanTypedValue.forValue(leftBigDecimal.compareTo(rightBigDecimal) < 0);
        }

        if (leftNumber instanceof Double || rightNumber instanceof Double) {
            return BooleanTypedValue.forValue(leftNumber.doubleValue() < rightNumber.doubleValue());
        }

        if (leftNumber instanceof Float || rightNumber instanceof Float) {
            return BooleanTypedValue.forValue(leftNumber.floatValue() < rightNumber.floatValue());
        }

        if (leftNumber instanceof Long || rightNumber instanceof Long) {
            return BooleanTypedValue.forValue(leftNumber.longValue() < rightNumber.longValue());
        }

        return BooleanTypedValue.forValue(leftNumber.intValue() < rightNumber.intValue());
    }

    if (left instanceof CharSequence && right instanceof CharSequence) {
        left = left.toString();
        right = right.toString();
    }

    return BooleanTypedValue.forValue(state.getTypeComparator().compare(left, right) < 0);
}
项目:lams    文件:OpLE.java   
@Override
public BooleanTypedValue getValueInternal(ExpressionState state)
        throws EvaluationException {
    Object left = getLeftOperand().getValueInternal(state).getValue();
    Object right = getRightOperand().getValueInternal(state).getValue();
    if (left instanceof Number && right instanceof Number) {
        Number leftNumber = (Number) left;
        Number rightNumber = (Number) right;

        if (leftNumber instanceof BigDecimal || rightNumber instanceof BigDecimal) {
            BigDecimal leftBigDecimal = NumberUtils.convertNumberToTargetClass(leftNumber, BigDecimal.class);
            BigDecimal rightBigDecimal = NumberUtils.convertNumberToTargetClass(rightNumber, BigDecimal.class);
            return BooleanTypedValue.forValue(leftBigDecimal.compareTo(rightBigDecimal) <= 0);
        }

        if (leftNumber instanceof Double || rightNumber instanceof Double) {
            return BooleanTypedValue.forValue(leftNumber.doubleValue() <= rightNumber.doubleValue());
        }

        if (leftNumber instanceof Float || rightNumber instanceof Float) {
            return BooleanTypedValue.forValue(leftNumber.floatValue() <= rightNumber.floatValue());
        }

        if (leftNumber instanceof Long || rightNumber instanceof Long) {
            return BooleanTypedValue.forValue(leftNumber.longValue() <= rightNumber.longValue());
        }

        return BooleanTypedValue.forValue(leftNumber.intValue() <= rightNumber.intValue());
    }

    return BooleanTypedValue.forValue(state.getTypeComparator().compare(left, right) <= 0);
}
项目:lams    文件:OpGT.java   
@Override
public BooleanTypedValue getValueInternal(ExpressionState state) throws EvaluationException {
    Object left = getLeftOperand().getValueInternal(state).getValue();
    Object right = getRightOperand().getValueInternal(state).getValue();

    if (left instanceof Number && right instanceof Number) {
        Number leftNumber = (Number) left;
        Number rightNumber = (Number) right;

        if (leftNumber instanceof BigDecimal || rightNumber instanceof BigDecimal) {
            BigDecimal leftBigDecimal = NumberUtils.convertNumberToTargetClass(leftNumber, BigDecimal.class);
            BigDecimal rightBigDecimal = NumberUtils.convertNumberToTargetClass(rightNumber, BigDecimal.class);
            return BooleanTypedValue.forValue(leftBigDecimal.compareTo(rightBigDecimal) > 0);
        }

        if (leftNumber instanceof Double || rightNumber instanceof Double) {
            return BooleanTypedValue.forValue(leftNumber.doubleValue() > rightNumber.doubleValue());
        }

        if (leftNumber instanceof Float || rightNumber instanceof Float) {
            return BooleanTypedValue.forValue(leftNumber.floatValue() > rightNumber.floatValue());
        }

        if (leftNumber instanceof Long || rightNumber instanceof Long) {
            return BooleanTypedValue.forValue(leftNumber.longValue() > rightNumber.longValue());
        }

        return BooleanTypedValue.forValue(leftNumber.intValue() > rightNumber.intValue());
    }

    if (left instanceof CharSequence && right instanceof CharSequence) {
        left = left.toString();
        right = right.toString();
    }

    return BooleanTypedValue.forValue(state.getTypeComparator().compare(left, right) > 0);
}
项目:spring4-understanding    文件:PeriodicTriggerTests.java   
private static Date asDate(Object o) {
    if (o == null) {
        return null;
    }
    if (o instanceof Date) {
        return (Date) o;
    }
    if (o instanceof Number) {
        return new Date(System.currentTimeMillis() +
                NumberUtils.convertNumberToTargetClass((Number) o, Long.class));
    }
    throw new IllegalArgumentException(
            "expected Date or Number, but actual type was: " + o.getClass());
}
项目:spring4-understanding    文件:StringToNumberConverterFactory.java   
@Override
public T convert(String source) {
    if (source.length() == 0) {
        return null;
    }
    return NumberUtils.parseNumber(source, this.targetType);
}
项目:spring4-understanding    文件:DataAccessUtils.java   
/**
 * Return a unique result object from the given Collection.
 * Throws an exception if 0 or more than 1 result objects found,
 * of if the unique result object is not convertable to the
 * specified required type.
 * @param results the result Collection (can be {@code null})
 * @return the unique result object
 * @throws IncorrectResultSizeDataAccessException if more than one
 * result object has been found in the given Collection
 * @throws EmptyResultDataAccessException if no result object
 * at all has been found in the given Collection
 * @throws TypeMismatchDataAccessException if the unique object does
 * not match the specified required type
 */
@SuppressWarnings("unchecked")
public static <T> T objectResult(Collection<?> results, Class<T> requiredType)
        throws IncorrectResultSizeDataAccessException, TypeMismatchDataAccessException {

    Object result = requiredUniqueResult(results);
    if (requiredType != null && !requiredType.isInstance(result)) {
        if (String.class == requiredType) {
            result = result.toString();
        }
        else if (Number.class.isAssignableFrom(requiredType) && Number.class.isInstance(result)) {
            try {
                result = NumberUtils.convertNumberToTargetClass(((Number) result), (Class<? extends Number>) requiredType);
            }
            catch (IllegalArgumentException ex) {
                throw new TypeMismatchDataAccessException(ex.getMessage());
            }
        }
        else {
            throw new TypeMismatchDataAccessException(
                    "Result object is of type [" + result.getClass().getName() +
                    "] and could not be converted to required type [" + requiredType.getName() + "]");
        }
    }
    return (T) result;
}
项目:spring4-understanding    文件:OperatorPower.java   
@Override
public TypedValue getValueInternal(ExpressionState state) throws EvaluationException {
    SpelNodeImpl leftOp = getLeftOperand();
    SpelNodeImpl rightOp = getRightOperand();

    Object leftOperand = leftOp.getValueInternal(state).getValue();
    Object rightOperand = rightOp.getValueInternal(state).getValue();

    if (leftOperand instanceof Number && rightOperand instanceof Number) {
        Number leftNumber = (Number) leftOperand;
        Number rightNumber = (Number) rightOperand;

        if (leftNumber instanceof BigDecimal) {
            BigDecimal leftBigDecimal = NumberUtils.convertNumberToTargetClass(leftNumber, BigDecimal.class);
            return new TypedValue(leftBigDecimal.pow(rightNumber.intValue()));
        }
        else if (leftNumber instanceof BigInteger) {
            BigInteger leftBigInteger = NumberUtils.convertNumberToTargetClass(leftNumber, BigInteger.class);
            return new TypedValue(leftBigInteger.pow(rightNumber.intValue()));
        }
        else if (leftNumber instanceof Double || rightNumber instanceof Double) {
            return new TypedValue(Math.pow(leftNumber.doubleValue(), rightNumber.doubleValue()));
        }
        else if (leftNumber instanceof Float || rightNumber instanceof Float) {
            return new TypedValue(Math.pow(leftNumber.floatValue(), rightNumber.floatValue()));
        }

        double d = Math.pow(leftNumber.doubleValue(), rightNumber.doubleValue());
        if (d > Integer.MAX_VALUE || leftNumber instanceof Long || rightNumber instanceof Long) {
            return new TypedValue((long) d);
        }
        else {
            return new TypedValue((int) d);
        }
    }

    return state.operate(Operation.POWER, leftOperand, rightOperand);
}
项目:spring4-understanding    文件:CustomNumberEditor.java   
/**
 * Coerce a Number value into the required target class, if necessary.
 */
@Override
public void setValue(Object value) {
    if (value instanceof Number) {
        super.setValue(NumberUtils.convertNumberToTargetClass((Number) value, this.numberClass));
    }
    else {
        super.setValue(value);
    }
}
项目:nbone    文件:StringHelper.java   
public static <T extends Number> List<T> delimitedListToListWithNumber(String str, String delimiter,Class<T> targetClass) {
    List<String> temp = delimitedListToList(str, delimiter);
    List<T> result  = new ArrayList<T>(temp.size());
    for (String string : temp) {
     T number = NumberUtils.parseNumber(string,targetClass);
     result.add(number);
    }
    return result;
}
项目:spring    文件:CustomNumberEditor.java   
/**
 * Coerce a Number value into the required target class, if necessary.
 */
@Override
public void setValue(Object value) {
    if (value instanceof Number) {
        super.setValue(NumberUtils.convertNumberToTargetClass((Number) value, this.numberClass));
    }
    else {
        super.setValue(value);
    }
}
项目:spring    文件:OperatorPower.java   
@Override
public TypedValue getValueInternal(ExpressionState state) throws EvaluationException {
    SpelNodeImpl leftOp = getLeftOperand();
    SpelNodeImpl rightOp = getRightOperand();

    Object leftOperand = leftOp.getValueInternal(state).getValue();
    Object rightOperand = rightOp.getValueInternal(state).getValue();

    if (leftOperand instanceof Number && rightOperand instanceof Number) {
        Number leftNumber = (Number) leftOperand;
        Number rightNumber = (Number) rightOperand;

        if (leftNumber instanceof BigDecimal) {
            BigDecimal leftBigDecimal = NumberUtils.convertNumberToTargetClass(leftNumber, BigDecimal.class);
            return new TypedValue(leftBigDecimal.pow(rightNumber.intValue()));
        }
        else if (leftNumber instanceof BigInteger) {
            BigInteger leftBigInteger = NumberUtils.convertNumberToTargetClass(leftNumber, BigInteger.class);
            return new TypedValue(leftBigInteger.pow(rightNumber.intValue()));
        }
        else if (leftNumber instanceof Double || rightNumber instanceof Double) {
            return new TypedValue(Math.pow(leftNumber.doubleValue(), rightNumber.doubleValue()));
        }
        else if (leftNumber instanceof Float || rightNumber instanceof Float) {
            return new TypedValue(Math.pow(leftNumber.floatValue(), rightNumber.floatValue()));
        }

        double d = Math.pow(leftNumber.doubleValue(), rightNumber.doubleValue());
        if (d > Integer.MAX_VALUE || leftNumber instanceof Long || rightNumber instanceof Long) {
            return new TypedValue((long) d);
        }
        else {
            return new TypedValue((int) d);
        }
    }

    return state.operate(Operation.POWER, leftOperand, rightOperand);
}
项目:spring    文件:StringToNumberConverterFactory.java   
@Override
public T convert(String source) {
    if (source.length() == 0) {
        return null;
    }
    return NumberUtils.parseNumber(source, this.targetType);
}
项目:spring-data-keyvalue-redis    文件:BinaryConverters.java   
@Override
public T convert(byte[] source) {

    if (source == null || source.length == 0) {
        return null;
    }

    return NumberUtils.parseNumber(toString(source), targetType);
}
项目:class-guard    文件:PeriodicTriggerTests.java   
private static Date asDate(Object o) {
    if (o == null) {
        return null;
    }
    if (o instanceof Date) {
        return (Date) o;
    }
    if (o instanceof Number) {
        return new Date(System.currentTimeMillis() +
                NumberUtils.convertNumberToTargetClass((Number) o, Long.class));
    }
    throw new IllegalArgumentException(
            "expected Date or Number, but actual type was: " + o.getClass());
}
项目:class-guard    文件:CustomNumberEditor.java   
/**
 * Coerce a Number value into the required target class, if necessary.
 */
@Override
public void setValue(Object value) {
    if (value instanceof Number) {
        super.setValue(NumberUtils.convertNumberToTargetClass((Number) value, this.numberClass));
    }
    else {
        super.setValue(value);
    }
}
项目:sharding    文件:DefaultScriptEngine.java   
@Override
public String eval(String script, ScriptContext context) throws ScriptException {
    try {
        Object val = engine.eval(script, context);
        if(Number.class.isInstance(val)) {
            return String.valueOf(NumberUtils.convertNumberToTargetClass((Number) val, Long.class));
        }
        return String.valueOf(val);
    } catch (javax.script.ScriptException e) {
        throw new ScriptException(e);
    }
}
项目:spring-cloud-aws    文件:NotificationRequestConverter.java   
private static Object getNumberValue(String attributeType, String attributeValue) {
    String numberType = attributeType.substring(MessageAttributeDataTypes.NUMBER.length() + 1);
    try {
        Class<? extends Number> numberTypeClass = Class.forName(numberType).asSubclass(Number.class);
        return NumberUtils.parseNumber(attributeValue, numberTypeClass);
    } catch (ClassNotFoundException e) {
        throw new MessagingException(String.format("Message attribute with value '%s' and data type '%s' could not be converted " +
                "into a Number because target class was not found.", attributeValue, attributeType), e);
    }
}