Java Code Examples for org.springframework.util.NumberUtils

The following examples show how to use org.springframework.util.NumberUtils. These examples are extracted from open source projects. 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 Project: spring-analysis-note   Source File: DataAccessUtils.java    License: MIT License 6 votes vote down vote up
/**
 * 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 convertible to the
 * specified required type.
 * @param results the result Collection (can be {@code null}
 * but is not expected to contain {@code null} elements)
 * @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(@Nullable Collection<?> results, @Nullable 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;
}
 
Example 2
Source Project: spring-analysis-note   Source File: SingleColumnRowMapper.java    License: 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 3
Source Project: effectivejava   Source File: SingleColumnRowMapper.java    License: 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 4
Source Project: spring-analysis-note   Source File: CustomNumberEditor.java    License: MIT License 6 votes vote down vote up
/**
 * 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));
	}
}
 
Example 5
Source Project: jeecg-cloud   Source File: QueryGenerator.java    License: 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 6
Source Project: jeecg-boot-with-activiti   Source File: QueryGenerator.java    License: 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 7
Source Project: java-technology-stack   Source File: DataAccessUtils.java    License: MIT License 6 votes vote down vote up
/**
 * 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 convertible to the
 * specified required type.
 * @param results the result Collection (can be {@code null}
 * but is not expected to contain {@code null} elements)
 * @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(@Nullable Collection<?> results, @Nullable 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;
}
 
Example 8
Source Project: java-technology-stack   Source File: SingleColumnRowMapper.java    License: 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 Project: jeecg   Source File: HqlGenerateUtil.java    License: 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 10
Source Project: java-technology-stack   Source File: CustomNumberEditor.java    License: MIT License 6 votes vote down vote up
/**
 * 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));
	}
}
 
Example 11
Source Project: spring-cloud-aws   Source File: QueueMessageUtils.java    License: 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 12
Source Project: teaching   Source File: QueryGenerator.java    License: 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 13
Source Project: jeecg-boot   Source File: QueryGenerator.java    License: 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 14
Source Project: lams   Source File: SingleColumnRowMapper.java    License: 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 15
Source Project: lams   Source File: DataAccessUtils.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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 convertible 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;
}
 
Example 16
Source Project: lams   Source File: CustomNumberEditor.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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));
	}
}
 
Example 17
Source Project: spring4-understanding   Source File: CustomNumberEditor.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 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));
	}
}
 
Example 18
Source Project: blog_demos   Source File: CustomNumberEditor.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 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));
	}
}
 
Example 19
Source Project: spring4-understanding   Source File: DataAccessUtils.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 20
Source Project: spring-analysis-note   Source File: CustomNumberEditor.java    License: MIT License 5 votes vote down vote up
/**
 * Coerce a Number value into the required target class, if necessary.
 */
@Override
public void setValue(@Nullable Object value) {
	if (value instanceof Number) {
		super.setValue(NumberUtils.convertNumberToTargetClass((Number) value, this.numberClass));
	}
	else {
		super.setValue(value);
	}
}
 
Example 21
Source Project: jeecg-cloud   Source File: QueryGenerator.java    License: 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();
}
 
Example 22
Source Project: jeecg-boot-with-activiti   Source File: QueryGenerator.java    License: 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 23
Source Project: java-technology-stack   Source File: PeriodicTriggerTests.java    License: MIT License 5 votes vote down vote up
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());
}
 
Example 24
@Override
public T convert(String source) {
	if (source.isEmpty()) {
		return null;
	}
	return NumberUtils.parseNumber(source, this.targetType);
}
 
Example 25
Source Project: java-technology-stack   Source File: OperatorPower.java    License: MIT License 5 votes vote down vote up
@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);
}
 
Example 26
Source Project: spring4-understanding   Source File: OperatorPower.java    License: Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 27
Source Project: cuba   Source File: WebSlider.java    License: Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
protected V convertFromDouble(Double componentValue) throws ConversionException {
    if (componentValue == null) {
        return null;
    }

    Datatype<V> datatype = getDatatype();
    return (V) NumberUtils.convertNumberToTargetClass(componentValue, datatype.getJavaClass());
}
 
Example 28
Source Project: teaching   Source File: QueryGenerator.java    License: 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 29
Source Project: jeecg-boot   Source File: QueryGenerator.java    License: 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 30
Source Project: DBus   Source File: ProjectController.java    License: 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);
}