package com.kyexpress.erp.tms.bill.provider.utils; import com.kyexpress.erp.tms.bill.provider.constant.ResponseCode; import com.kyexpress.framework.exception.ApplicationException; import com.kyexpress.framework.utils.StringUtils; import com.kyexpress.framework.validator.util.ValidatorUtil; import org.springframework.util.CollectionUtils; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Collection; import java.util.function.Consumer; import java.util.function.Function; import java.util.function.Supplier; import java.util.stream.Stream; public final class ValidateUtils { private ValidateUtils() { } public static boolean isValidDate(String str,String formatStr) { boolean convertSuccess = true; SimpleDateFormat format = new SimpleDateFormat(formatStr); try { // 设置lenient为false. // 否则SimpleDateFormat会比较宽松地验证日期,比如2007/02/29会被接受,并转换成2007/03/01 format.setLenient(false); format.parse(str); } catch (ParseException e) { // e.printStackTrace(); // 如果throw java.text.ParseException或者NullPointerException,就说明格式不对 convertSuccess = false; } return convertSuccess; } public static void valid(Supplier<Boolean> supplier, ResponseCode code) { if (supplier.get()) { throw new ApplicationException(code); } } public static void valid(Supplier<Boolean> supplier, ResponseCode code, ResponseCode... paramCodes) { if (supplier.get()) { if (ArrayUtils.isEmpty(paramCodes)) { throw new ApplicationException(code); } else { String[] params = Stream.of(paramCodes).map(ResponseCode::getMessage).toArray(String[]::new); throw new ApplicationException(code.getCode(), String.format(code.getMessage(), params)); } } } public static void valid(Supplier<Boolean> supplier, ResponseCode code, String... params) { if (supplier.get()) { if (ArrayUtils.isEmpty(params)) { throw new ApplicationException(code); } else { throw new ApplicationException(code.getCode(), String.format(code.getMessage(), params)); } } } public static <T> void valid(T param, Consumer<T> consumer, ResponseCode code) { try { consumer.accept(param); } catch (Exception e) { String message = e.getMessage(); throw new ApplicationException(code.getCode(), message); } } public static <T> void validNull(T value, ResponseCode code) { valid(() -> value == null, code); } public static <T> void validNull(T value, ResponseCode code, String... params) { valid(() -> value == null, code, params); } public static <T> void validNull(T value, ResponseCode code, ResponseCode... params) { valid(() -> value == null, code, params); } public static <T> void validEmpty(String value, ResponseCode code) { valid(() -> StringUtils.isEmpty(value), code); } public static <T> void validEmpty(String value, ResponseCode code, String... params) { valid(() -> StringUtils.isEmpty(value), code, params); } public static <T> void validEmpty(String value, ResponseCode code, ResponseCode... params) { valid(() -> StringUtils.isEmpty(value), code, params); } public static <T> void validEmpty(Collection<T> value, ResponseCode code) { valid(() -> CollectionUtils.isEmpty(value), code); } public static <T> void validEmpty(Collection<T> value, ResponseCode code, String... params) { valid(() -> CollectionUtils.isEmpty(value), code, params); } public static <T> void validEmpty(Collection<T> value, ResponseCode code, ResponseCode... params) { valid(() -> CollectionUtils.isEmpty(value), code, params); } }