perf: 简化类型校验代码
This commit is contained in:
@@ -271,48 +271,42 @@ public abstract class SimpleServiceSupport<ENTITY extends SimpleEntity> implemen
|
||||
if (ObjectHelper.isNotEmpty(queryable.getLike())) {
|
||||
queryable.getLike().forEach((column, value) -> {
|
||||
var path = column(root, column);
|
||||
checkString(path, column);
|
||||
checkString(value, column);
|
||||
checkString(path, value, column);
|
||||
predicates.add(builder.like(column(root, column), value));
|
||||
});
|
||||
}
|
||||
if (ObjectHelper.isNotEmpty(queryable.getNotLike())) {
|
||||
queryable.getNotLike().forEach((column, value) -> {
|
||||
var path = column(root, column);
|
||||
checkString(path, column);
|
||||
checkString(value, column);
|
||||
checkString(path, value, column);
|
||||
predicates.add(builder.notLike(column(root, column), value));
|
||||
});
|
||||
}
|
||||
if (ObjectHelper.isNotEmpty(queryable.getGreat())) {
|
||||
queryable.getGreat().forEach((column, value) -> {
|
||||
var path = this.<Comparable<Object>>column(root, column);
|
||||
checkComparable(path, column);
|
||||
checkComparable(value, column);
|
||||
checkComparable(path, value, column);
|
||||
predicates.add(builder.greaterThan(path, (Comparable<Object>) value));
|
||||
});
|
||||
}
|
||||
if (ObjectHelper.isNotEmpty(queryable.getLess())) {
|
||||
queryable.getLess().forEach((column, value) -> {
|
||||
var path = this.<Comparable<Object>>column(root, column);
|
||||
checkComparable(path, column);
|
||||
checkComparable(value, column);
|
||||
checkComparable(path, value, column);
|
||||
predicates.add(builder.lessThan(path, (Comparable<Object>) value));
|
||||
});
|
||||
}
|
||||
if (ObjectHelper.isNotEmpty(queryable.getGreatEqual())) {
|
||||
queryable.getGreatEqual().forEach((column, value) -> {
|
||||
var path = this.<Comparable<Object>>column(root, column);
|
||||
checkComparable(path, column);
|
||||
checkComparable(value, column);
|
||||
checkComparable(path, value, column);
|
||||
predicates.add(builder.greaterThanOrEqualTo(path, (Comparable<Object>) value));
|
||||
});
|
||||
}
|
||||
if (ObjectHelper.isNotEmpty(queryable.getLessEqual())) {
|
||||
queryable.getLessEqual().forEach((column, value) -> {
|
||||
var path = this.<Comparable<Object>>column(root, column);
|
||||
checkComparable(path, column);
|
||||
checkComparable(value, column);
|
||||
checkComparable(path, value, column);
|
||||
predicates.add(builder.lessThanOrEqualTo(path, (Comparable<Object>) value));
|
||||
});
|
||||
}
|
||||
@@ -325,18 +319,14 @@ public abstract class SimpleServiceSupport<ENTITY extends SimpleEntity> implemen
|
||||
if (ObjectHelper.isNotEmpty(queryable.getBetween())) {
|
||||
queryable.getBetween().forEach((column, value) -> {
|
||||
var path = this.<Comparable<Object>>column(root, column);
|
||||
checkComparable(path, column);
|
||||
checkComparable(value.getStart(), column);
|
||||
checkComparable(value.getEnd(), column);
|
||||
checkComparable(path, value, column);
|
||||
predicates.add(builder.between(column(root, column), (Comparable<Object>) value.getStart(), (Comparable<Object>) value.getEnd()));
|
||||
});
|
||||
}
|
||||
if (ObjectHelper.isNotEmpty(queryable.getNotBetween())) {
|
||||
queryable.getNotBetween().forEach((column, value) -> {
|
||||
var path = this.<Comparable<Object>>column(root, column);
|
||||
checkComparable(path, column);
|
||||
checkComparable(value.getStart(), column);
|
||||
checkComparable(value.getEnd(), column);
|
||||
checkComparable(path, value, column);
|
||||
predicates.add(builder.between(column(root, column), (Comparable<Object>) value.getStart(), (Comparable<Object>) value.getEnd()).not());
|
||||
});
|
||||
}
|
||||
@@ -353,21 +343,8 @@ public abstract class SimpleServiceSupport<ENTITY extends SimpleEntity> implemen
|
||||
* @param column 字段名称
|
||||
* @throws NotComparableException 当字段类型不可比较时抛出
|
||||
*/
|
||||
private void checkComparable(Path<?> path, String column) {
|
||||
if (!ObjectHelper.isComparable(path.getJavaType())) {
|
||||
throw new NotComparableException(column);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查值是否可比较
|
||||
*
|
||||
* @param value 值对象
|
||||
* @param column 字段名称
|
||||
* @throws NotComparableException 当值不可比较时抛出
|
||||
*/
|
||||
private void checkComparable(Object value, String column) {
|
||||
if (!ObjectHelper.isComparable(value)) {
|
||||
private void checkComparable(Path<?> path, Object value, String column) {
|
||||
if (!ObjectHelper.isComparable(path.getJavaType()) || !ObjectHelper.isComparable(value)) {
|
||||
throw new NotComparableException(column);
|
||||
}
|
||||
}
|
||||
@@ -379,9 +356,9 @@ public abstract class SimpleServiceSupport<ENTITY extends SimpleEntity> implemen
|
||||
* @param column 字段名称
|
||||
* @throws NotComparableException 当区间值不可比较时抛出
|
||||
*/
|
||||
private void checkComparable(Query.Queryable.Between value, String column) {
|
||||
checkComparable(value.getStart(), column);
|
||||
checkComparable(value.getEnd(), column);
|
||||
private void checkComparable(Path<?> path, Query.Queryable.Between value, String column) {
|
||||
checkComparable(path, value.getStart(), column);
|
||||
checkComparable(path, value.getEnd(), column);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -417,21 +394,8 @@ public abstract class SimpleServiceSupport<ENTITY extends SimpleEntity> implemen
|
||||
* @param column 字段名称
|
||||
* @throws NotStringException 当字段类型不是字符串时抛出
|
||||
*/
|
||||
private void checkString(Path<?> path, String column) {
|
||||
if (!ObjectHelper.isString(path.getJavaType())) {
|
||||
throw new NotStringException(column);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查值是否为字符串
|
||||
*
|
||||
* @param value 值对象
|
||||
* @param column 字段名称
|
||||
* @throws NotStringException 当值不是字符串时抛出
|
||||
*/
|
||||
private void checkString(Object value, String column) {
|
||||
if (!ObjectHelper.isString(value)) {
|
||||
private void checkString(Path<?> path, Object value, String column) {
|
||||
if (!ObjectHelper.isString(path.getJavaType()) || !ObjectHelper.isString(value)) {
|
||||
throw new NotStringException(column);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user