Java Code Examples for org.springframework.util.NumberUtils#parseNumber()

The following examples show how to use org.springframework.util.NumberUtils#parseNumber() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: QueryGenerator.java    From jeecg-boot with Apache License 2.0 6 votes vote down vote up
private static void addRuleToQueryWrapper(SysPermissionDataRuleModel dataRule, String name, Class propertyType, QueryWrapper<?> queryWrapper) {
	QueryRuleEnum rule = QueryRuleEnum.getByValue(dataRule.getRuleConditions());
	if(rule.equals(QueryRuleEnum.IN) && ! propertyType.equals(String.class)) {
		String[] values = dataRule.getRuleValue().split(",");
		Object[] objs = new Object[values.length];
		for (int i = 0; i < values.length; i++) {
			objs[i] = NumberUtils.parseNumber(values[i], propertyType);
		}
		addEasyQuery(queryWrapper, name, rule, objs);
	}else {
		if (propertyType.equals(String.class)) {
			addEasyQuery(queryWrapper, name, rule, converRuleValue(dataRule.getRuleValue()));
		}else if (propertyType.equals(Date.class)) {
			String dateStr =converRuleValue(dataRule.getRuleValue());
			if(dateStr.length()==10){
				addEasyQuery(queryWrapper, name, rule, DateUtils.str2Date(dateStr,DateUtils.date_sdf.get()));
			}else{
				addEasyQuery(queryWrapper, name, rule, DateUtils.str2Date(dateStr,DateUtils.datetimeFormat.get()));
			}
		}else {
			addEasyQuery(queryWrapper, name, rule, NumberUtils.parseNumber(dataRule.getRuleValue(), propertyType));
		}
	}
}
 
Example 2
Source File: SingleColumnRowMapper.java    From effectivejava with Apache License 2.0 6 votes vote down vote up
/**
 * 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() + "]");
	}
}
 
Example 3
Source File: QueueMessageUtils.java    From spring-cloud-aws with Apache License 2.0 6 votes vote down vote up
public static Object parseNumber(String value, String type)
		throws ClassNotFoundException {
	if (MessageAttributeDataTypes.NUMBER.equals(type)) {
		return NumberUtils.parseNumber(value, Number.class);
	}
	else {
		String javaType = type
				.substring(MessageAttributeDataTypes.NUMBER.length() + 1);

		if (PRIMITIVE_TO_WRAPPED.containsKey(javaType.toLowerCase())) {
			return NumberUtils.parseNumber(value,
					PRIMITIVE_TO_WRAPPED.get(javaType.toLowerCase()));
		}
		else {
			Class<? extends Number> numberTypeClass = Class.forName(javaType)
					.asSubclass(Number.class);
			return NumberUtils.parseNumber(value, numberTypeClass);
		}
	}
}
 
Example 4
Source File: SingleColumnRowMapper.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * 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() + "]");
	}
}
 
Example 5
Source File: SingleColumnRowMapper.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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() + "]");
	}
}
 
Example 6
Source File: HqlGenerateUtil.java    From jeecg with Apache License 2.0 6 votes vote down vote up
private static void addRuleToCriteria(TSDataRule tsDataRule,
		String aliasName, Class propertyType, CriteriaQuery cq) {
	HqlRuleEnum rule = HqlRuleEnum.getByValue(tsDataRule.getRuleConditions());
	if (rule.equals(HqlRuleEnum.IN)) {
		String[] values = tsDataRule.getRuleValue().split(",");
		Object[] objs = new Object[values.length];
		if (! propertyType.equals(String.class)) {
			for (int i = 0; i < values.length; i++) {
				objs[i] = NumberUtils.parseNumber(values[i], propertyType);
			}
		}else {
			objs = values;
		}
		ObjectParseUtil.addCriteria(cq, aliasName, rule, objs);
	} else {
		if (propertyType.equals(String.class)) {
			ObjectParseUtil.addCriteria(cq, aliasName, rule,converRuleValue(tsDataRule.getRuleValue()));
		} else {
			ObjectParseUtil.addCriteria(cq, aliasName, rule, NumberUtils.parseNumber(tsDataRule.getRuleValue(), propertyType));
		}
	}
}
 
Example 7
Source File: QueryGenerator.java    From teaching with Apache License 2.0 6 votes vote down vote up
private static void addRuleToQueryWrapper(SysPermissionDataRuleModel dataRule, String name, Class propertyType, QueryWrapper<?> queryWrapper) {
	QueryRuleEnum rule = QueryRuleEnum.getByValue(dataRule.getRuleConditions());
	if(rule.equals(QueryRuleEnum.IN) && ! propertyType.equals(String.class)) {
		String[] values = dataRule.getRuleValue().split(",");
		Object[] objs = new Object[values.length];
		for (int i = 0; i < values.length; i++) {
			objs[i] = NumberUtils.parseNumber(values[i], propertyType);
		}
		addEasyQuery(queryWrapper, name, rule, objs);
	}else {
		if (propertyType.equals(String.class)) {
			addEasyQuery(queryWrapper, name, rule, converRuleValue(dataRule.getRuleValue()));
		} else {
			addEasyQuery(queryWrapper, name, rule, NumberUtils.parseNumber(dataRule.getRuleValue(), propertyType));
		}
	}
}
 
Example 8
Source File: SingleColumnRowMapper.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * 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). Otherwise, the value will
 * be converted to a required type using the {@link ConversionService}.
 * @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")
@Nullable
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 if (this.conversionService != null && this.conversionService.canConvert(value.getClass(), requiredType)) {
		return this.conversionService.convert(value, requiredType);
	}
	else {
		throw new IllegalArgumentException(
				"Value [" + value + "] is of type [" + value.getClass().getName() +
				"] and cannot be converted to required type [" + requiredType.getName() + "]");
	}
}
 
Example 9
Source File: QueryGenerator.java    From jeecg-boot-with-activiti with MIT License 6 votes vote down vote up
private static void addRuleToQueryWrapper(SysPermissionDataRule dataRule,String name, Class propertyType, QueryWrapper<?> queryWrapper) {
	QueryRuleEnum rule = QueryRuleEnum.getByValue(dataRule.getRuleConditions());
	if(rule.equals(QueryRuleEnum.IN) && ! propertyType.equals(String.class)) {
		String[] values = dataRule.getRuleValue().split(",");
		Object[] objs = new Object[values.length];
		for (int i = 0; i < values.length; i++) {
			objs[i] = NumberUtils.parseNumber(values[i], propertyType);
		}
		addEasyQuery(queryWrapper, name, rule, objs);
	}else {
		if (propertyType.equals(String.class)) {
			addEasyQuery(queryWrapper, name, rule, converRuleValue(dataRule.getRuleValue()));
		} else {
			addEasyQuery(queryWrapper, name, rule, NumberUtils.parseNumber(dataRule.getRuleValue(), propertyType));
		}
	}
}
 
Example 10
Source File: QueryGenerator.java    From jeecg-cloud with Apache License 2.0 6 votes vote down vote up
private static void addRuleToQueryWrapper(SysPermissionDataRuleModel dataRule, String name, Class propertyType, QueryWrapper<?> queryWrapper) {
	QueryRuleEnum rule = QueryRuleEnum.getByValue(dataRule.getRuleConditions());
	if(rule.equals(QueryRuleEnum.IN) && ! propertyType.equals(String.class)) {
		String[] values = dataRule.getRuleValue().split(",");
		Object[] objs = new Object[values.length];
		for (int i = 0; i < values.length; i++) {
			objs[i] = NumberUtils.parseNumber(values[i], propertyType);
		}
		addEasyQuery(queryWrapper, name, rule, objs);
	}else {
		if (propertyType.equals(String.class)) {
			addEasyQuery(queryWrapper, name, rule, converRuleValue(dataRule.getRuleValue()));
		}else if (propertyType.equals(Date.class)) {
			String dateStr =converRuleValue(dataRule.getRuleValue());
			if(dateStr.length()==10){
				addEasyQuery(queryWrapper, name, rule, DateUtils.str2Date(dateStr,DateUtils.date_sdf.get()));
			}else{
				addEasyQuery(queryWrapper, name, rule, DateUtils.str2Date(dateStr,DateUtils.datetimeFormat.get()));
			}
		}else {
			addEasyQuery(queryWrapper, name, rule, NumberUtils.parseNumber(dataRule.getRuleValue(), propertyType));
		}
	}
}
 
Example 11
Source File: SingleColumnRowMapper.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * 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). Otherwise, the value will
 * be converted to a required type using the {@link ConversionService}.
 * @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")
@Nullable
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 if (this.conversionService != null && this.conversionService.canConvert(value.getClass(), requiredType)) {
		return this.conversionService.convert(value, requiredType);
	}
	else {
		throw new IllegalArgumentException(
				"Value [" + value + "] is of type [" + value.getClass().getName() +
				"] and cannot be converted to required type [" + requiredType.getName() + "]");
	}
}
 
Example 12
Source File: StringToNumberConverterFactory.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public T convert(String source) {
	if (source.isEmpty()) {
		return null;
	}
	return NumberUtils.parseNumber(source, this.targetType);
}
 
Example 13
Source File: QueryGenerator.java    From teaching with Apache License 2.0 5 votes vote down vote up
/**
 *   根据权限相关配置生成相关的SQL 语句
 * @param searchObj
 * @param parameterMap
 * @return
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public static String installAuthJdbc(Class<?> clazz) {
	StringBuffer sb = new StringBuffer();
	//权限查询
	Map<String,SysPermissionDataRuleModel> ruleMap = getRuleMap();
	PropertyDescriptor origDescriptors[] = PropertyUtils.getPropertyDescriptors(clazz);
	String sql_and = " and ";
	for (String c : ruleMap.keySet()) {
		if(oConvertUtils.isNotEmpty(c) && c.startsWith(SQL_RULES_COLUMN)){
			sb.append(sql_and+getSqlRuleValue(ruleMap.get(c).getRuleValue()));
		}
	}
	String name;
	for (int i = 0; i < origDescriptors.length; i++) {
		name = origDescriptors[i].getName();
		if (judgedIsUselessField(name)) {
			continue;
		}
		if(ruleMap.containsKey(name)) {
			SysPermissionDataRuleModel dataRule = ruleMap.get(name);
			QueryRuleEnum rule = QueryRuleEnum.getByValue(dataRule.getRuleConditions());
			Class propType = origDescriptors[i].getPropertyType();
			boolean isString = propType.equals(String.class);
			Object value;
			if(isString) {
				value = converRuleValue(dataRule.getRuleValue());
			}else {
				value = NumberUtils.parseNumber(dataRule.getRuleValue(),propType);
			}
			String filedSql = getSingleSqlByRule(rule, oConvertUtils.camelToUnderline(name), value,isString);
			sb.append(sql_and+filedSql);
		}
	}
	log.info("query auth sql is:"+sb.toString());
	return sb.toString();
}
 
Example 14
Source File: StringToNumberConverterFactory.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public T convert(String source) {
	if (source.isEmpty()) {
		return null;
	}
	return NumberUtils.parseNumber(source, this.targetType);
}
 
Example 15
Source File: QueryGenerator.java    From jeecg-boot with Apache License 2.0 5 votes vote down vote up
/**
 *   根据权限相关配置生成相关的SQL 语句
 * @param searchObj
 * @param parameterMap
 * @return
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public static String installAuthJdbc(Class<?> clazz) {
	StringBuffer sb = new StringBuffer();
	//权限查询
	Map<String,SysPermissionDataRuleModel> ruleMap = getRuleMap();
	PropertyDescriptor origDescriptors[] = PropertyUtils.getPropertyDescriptors(clazz);
	String sql_and = " and ";
	for (String c : ruleMap.keySet()) {
		if(oConvertUtils.isNotEmpty(c) && c.startsWith(SQL_RULES_COLUMN)){
			sb.append(sql_and+getSqlRuleValue(ruleMap.get(c).getRuleValue()));
		}
	}
	String name;
	for (int i = 0; i < origDescriptors.length; i++) {
		name = origDescriptors[i].getName();
		if (judgedIsUselessField(name)) {
			continue;
		}
		if(ruleMap.containsKey(name)) {
			SysPermissionDataRuleModel dataRule = ruleMap.get(name);
			QueryRuleEnum rule = QueryRuleEnum.getByValue(dataRule.getRuleConditions());
			Class propType = origDescriptors[i].getPropertyType();
			boolean isString = propType.equals(String.class);
			Object value;
			if(isString) {
				value = converRuleValue(dataRule.getRuleValue());
			}else {
				value = NumberUtils.parseNumber(dataRule.getRuleValue(),propType);
			}
			String filedSql = getSingleSqlByRule(rule, oConvertUtils.camelToUnderline(name), value,isString);
			sb.append(sql_and+filedSql);
		}
	}
	log.info("query auth sql is:"+sb.toString());
	return sb.toString();
}
 
Example 16
Source File: QueryGenerator.java    From jeecg-boot-with-activiti with MIT License 5 votes vote down vote up
/**
 *   根据权限相关配置生成相关的SQL 语句
 * @param searchObj
 * @param parameterMap
 * @return
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public static String installAuthJdbc(Class<?> clazz) {
	StringBuffer sb = new StringBuffer();
	//权限查询
	Map<String,SysPermissionDataRule> ruleMap = getRuleMap();
	PropertyDescriptor origDescriptors[] = PropertyUtils.getPropertyDescriptors(clazz);
	String sql_and = " and ";
	for (String c : ruleMap.keySet()) {
		if(oConvertUtils.isNotEmpty(c) && c.startsWith(SQL_RULES_COLUMN)){
			sb.append(sql_and+getSqlRuleValue(ruleMap.get(c).getRuleValue()));
		}
	}
	String name;
	for (int i = 0; i < origDescriptors.length; i++) {
		name = origDescriptors[i].getName();
		if (judgedIsUselessField(name)) {
			continue;
		}
		if(ruleMap.containsKey(name)) {
			SysPermissionDataRule dataRule = ruleMap.get(name);
			QueryRuleEnum rule = QueryRuleEnum.getByValue(dataRule.getRuleConditions());
			Class propType = origDescriptors[i].getPropertyType();
			boolean isString = propType.equals(String.class);
			Object value;
			if(isString) {
				value = converRuleValue(dataRule.getRuleValue());
			}else {
				value = NumberUtils.parseNumber(dataRule.getRuleValue(),propType);
			}
			String filedSql = getSingleSqlByRule(rule, oConvertUtils.camelToUnderline(name), value,isString);
			sb.append(sql_and+filedSql);
		}
	}
	log.info("query auth sql is:"+sb.toString());
	return sb.toString();
}
 
Example 17
Source File: ProjectController.java    From DBus with Apache License 2.0 5 votes vote down vote up
@ApiOperation(value = "search projects", notes = "search projects by userId, roleType")
@ApiResponses({
        @ApiResponse(code = 200, message = "OK", response = Map[].class)
})
@GetMapping("")
public ResultEntity queryProjects(HttpServletRequest req) {
    Integer userId = null;
    if (StringUtils.isNotBlank(req.getParameter("__user_id")))
        userId = NumberUtils.parseNumber(req.getParameter("__user_id"), Integer.class);
    String roleType = req.getParameter("__role_type");
    return service.queryProjects(userId, roleType);
}
 
Example 18
Source File: StringToNumberConverterFactory.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public T convert(String source) {
	if (source.isEmpty()) {
		return null;
	}
	return NumberUtils.parseNumber(source, this.targetType);
}
 
Example 19
Source File: StringToNumberConverterFactory.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public T convert(String source) {
	if (source.length() == 0) {
		return null;
	}
	return NumberUtils.parseNumber(source, this.targetType);
}
 
Example 20
Source File: QueryGenerator.java    From jeecg-cloud with Apache License 2.0 5 votes vote down vote up
/**
 *   根据权限相关配置生成相关的SQL 语句
 * @param clazz
 * @return
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public static String installAuthJdbc(Class<?> clazz) {
	StringBuffer sb = new StringBuffer();
	//权限查询
	Map<String,SysPermissionDataRuleModel> ruleMap = getRuleMap();
	PropertyDescriptor origDescriptors[] = PropertyUtils.getPropertyDescriptors(clazz);
	String sql_and = " and ";
	for (String c : ruleMap.keySet()) {
		if(oConvertUtils.isNotEmpty(c) && c.startsWith(SQL_RULES_COLUMN)){
			sb.append(sql_and+getSqlRuleValue(ruleMap.get(c).getRuleValue()));
		}
	}
	String name;
	for (int i = 0; i < origDescriptors.length; i++) {
		name = origDescriptors[i].getName();
		if (judgedIsUselessField(name)) {
			continue;
		}
		if(ruleMap.containsKey(name)) {
			SysPermissionDataRuleModel dataRule = ruleMap.get(name);
			QueryRuleEnum rule = QueryRuleEnum.getByValue(dataRule.getRuleConditions());
			Class propType = origDescriptors[i].getPropertyType();
			boolean isString = propType.equals(String.class);
			Object value;
			if(isString) {
				value = converRuleValue(dataRule.getRuleValue());
			}else {
				value = NumberUtils.parseNumber(dataRule.getRuleValue(),propType);
			}
			String filedSql = getSingleSqlByRule(rule, oConvertUtils.camelToUnderline(name), value,isString);
			sb.append(sql_and+filedSql);
		}
	}
	log.info("query auth sql is:"+sb.toString());
	return sb.toString();
}