本文整理汇总了Java中org.springframework.util.NumberUtils类的典型用法代码示例。如果您正苦于以下问题:Java NumberUtils类的具体用法?Java NumberUtils怎么用?Java NumberUtils使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
NumberUtils类属于org.springframework.util包,在下文中一共展示了NumberUtils类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: convertValueToRequiredType
import org.springframework.util.NumberUtils; //导入依赖的package包/类
/**
* 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() + "]");
}
}
示例2: setAsText
import org.springframework.util.NumberUtils; //导入依赖的package包/类
/**
* 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));
}
}
示例3: convertValueToRequiredType
import org.springframework.util.NumberUtils; //导入依赖的package包/类
/**
* 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() + "]");
}
}
示例4: convert
import org.springframework.util.NumberUtils; //导入依赖的package包/类
@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;
}
示例5: convert
import org.springframework.util.NumberUtils; //导入依赖的package包/类
@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);
}
示例6: convertValueToRequiredType
import org.springframework.util.NumberUtils; //导入依赖的package包/类
/**
* 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() + "]");
}
}
示例7: executeUpdateAndKeepKeys
import org.springframework.util.NumberUtils; //导入依赖的package包/类
@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));
}
示例8: objectResult
import org.springframework.util.NumberUtils; //导入依赖的package包/类
/**
* 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;
}
示例9: convertValueToRequiredType
import org.springframework.util.NumberUtils; //导入依赖的package包/类
/**
* 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() + "]");
}
}
示例10: newSupportCommandList
import org.springframework.util.NumberUtils; //导入依赖的package包/类
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;
}
示例11: conversionType
import org.springframework.util.NumberUtils; //导入依赖的package包/类
/**
* 将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;
}
示例12: convertValueToRequiredType
import org.springframework.util.NumberUtils; //导入依赖的package包/类
@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() + "]");
}
}
示例13: setUp
import org.springframework.util.NumberUtils; //导入依赖的package包/类
@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();
}
示例14: convertIfNeccesary
import org.springframework.util.NumberUtils; //导入依赖的package包/类
@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;
}
示例15: setValue
import org.springframework.util.NumberUtils; //导入依赖的package包/类
/**
* 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);
}
}