refactor(util): 重构工具类代码以改进资源管理和验证逻辑
This commit is contained in:
+6
-4
@@ -110,10 +110,12 @@ public class KitRedisHelper {
|
||||
ScanOptions options = ScanOptions.scanOptions().match(pattern).build();
|
||||
RedisConnectionFactory factory = redisTemplate.getConnectionFactory();
|
||||
RedisConnection rc = Objects.requireNonNull(factory).getConnection();
|
||||
Cursor<byte[]> cursor = rc.scan(options);
|
||||
List<String> result = new ArrayList<>();
|
||||
while (cursor.hasNext()) {
|
||||
result.add(new String(cursor.next()));
|
||||
List<String> result;
|
||||
try (Cursor<byte[]> cursor = rc.scan(options)) {
|
||||
result = new ArrayList<>();
|
||||
while (cursor.hasNext()) {
|
||||
result.add(new String(cursor.next()));
|
||||
}
|
||||
}
|
||||
try {
|
||||
RedisConnectionUtils.releaseConnection(rc, factory);
|
||||
|
||||
+3
-1
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
package cn.odboy.util;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.odboy.base.KitSelectOptionVo;
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import java.lang.reflect.Field;
|
||||
@@ -38,7 +39,7 @@ import lombok.experimental.UtilityClass;
|
||||
* @date 2022-11-09
|
||||
*/
|
||||
@UtilityClass
|
||||
public final class KitCollUtil extends cn.hutool.core.collection.CollUtil {
|
||||
public final class KitCollUtil extends CollUtil {
|
||||
|
||||
private static <T, K> Predicate<T> distinctPredicate(Function<? super T, ? extends K> keyMapper) {
|
||||
Map<Object, Boolean> map = new HashMap<>(3);
|
||||
@@ -192,6 +193,7 @@ public final class KitCollUtil extends cn.hutool.core.collection.CollUtil {
|
||||
* @return /
|
||||
*/
|
||||
public static BigDecimal streamBigDecimalAvg(List<BigDecimal> data) {
|
||||
KitValidUtil.notEmpty(data, "data");
|
||||
return data.stream().filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add)
|
||||
.divide(BigDecimal.valueOf(data.size()), 6, RoundingMode.FLOOR);
|
||||
}
|
||||
|
||||
+3
-7
@@ -81,10 +81,6 @@ public final class KitFileUtil extends FileUtil {
|
||||
* 定义KB的计算常量
|
||||
*/
|
||||
private static final int KB = 1024;
|
||||
/**
|
||||
* 格式化小数
|
||||
*/
|
||||
private static final DecimalFormat DF = new DecimalFormat("0.00");
|
||||
|
||||
/**
|
||||
* MultipartFile转File
|
||||
@@ -113,13 +109,13 @@ public final class KitFileUtil extends FileUtil {
|
||||
String resultSize;
|
||||
if (size / GB >= 1) {
|
||||
//如果当前Byte的值大于等于1GB
|
||||
resultSize = DF.format(size / (float) GB) + " GB";
|
||||
resultSize = new DecimalFormat("0.00").format(size / (float) GB) + " GB";
|
||||
} else if (size / MB >= 1) {
|
||||
//如果当前Byte的值大于等于1MB
|
||||
resultSize = DF.format(size / (float) MB) + " MB";
|
||||
resultSize = new DecimalFormat("0.00").format(size / (float) MB) + " MB";
|
||||
} else if (size / KB >= 1) {
|
||||
//如果当前Byte的值大于等于1KB
|
||||
resultSize = DF.format(size / (float) KB) + " KB";
|
||||
resultSize = new DecimalFormat("0.00").format(size / (float) KB) + " KB";
|
||||
} else {
|
||||
resultSize = size + " B";
|
||||
}
|
||||
|
||||
+15
-7
@@ -15,16 +15,18 @@
|
||||
*/
|
||||
package cn.odboy.util;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.odboy.framework.exception.BadRequestException;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
import jakarta.validation.ConstraintViolation;
|
||||
import jakarta.validation.Validation;
|
||||
import jakarta.validation.Validator;
|
||||
import jakarta.validation.ValidatorFactory;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import jakarta.validation.groups.Default;
|
||||
import javax.validation.ConstraintViolation;
|
||||
import javax.validation.Validation;
|
||||
import javax.validation.Validator;
|
||||
import javax.validation.ValidatorFactory;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import javax.validation.groups.Default;
|
||||
import lombok.experimental.UtilityClass;
|
||||
|
||||
/**
|
||||
@@ -65,7 +67,7 @@ public final class KitValidUtil {
|
||||
}
|
||||
|
||||
public static void notNull(Object value, @NotNull String name, @NotNull String code) {
|
||||
if (value != null) {
|
||||
if (value == null) {
|
||||
throw new BadRequestException(String.format("参数 %s(%s) 必填", name, code));
|
||||
}
|
||||
}
|
||||
@@ -81,4 +83,10 @@ public final class KitValidUtil {
|
||||
throw new BadRequestException(String.format("参数 %s(%s) 必填", name, code));
|
||||
}
|
||||
}
|
||||
|
||||
public static void notEmpty(List<?> data, @NotNull String code) {
|
||||
if (CollUtil.isEmpty(data)) {
|
||||
throw new BadRequestException(String.format("参数 %s 必填", code));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user