【java8】如何优雅的参数格式校验工具类

package com.fanghui.common.util;

import com.fanghui.common.constant.ResultEnum;
import com.fanghui.common.constant.ValidConstant;
import com.fanghui.common.exception.BusinessException;

import cn.hutool.core.text.CharSequenceUtil;

import java.util.function.Consumer;
import java.util.function.Predicate;

/**
 * 参数格式校验工具类
 *
 * @author fanghui
 * @date 2021-04-26 17:06
 */
public class PreconditionUtil {

    private PreconditionUtil() {
    }

    public static void checkEcuPartNum(String ecuPartNum) {
        paramCheck(ecuPartNum, CharSequenceUtil::isNotBlank, ValidConstant.MSG_NON_NULL_ECU_PART_NUM);
        paramCheck(ecuPartNum, p -> p.length() >= ValidConstant.MIN_ECU_PART_NUM
                && p.length() <= ValidConstant.MAX_ECU_PART_NUM,
            ValidConstant.MSG_LEN_ECU_PART_NUM);
    }

    public static void checkEcuSVer(String ecuSVer) {
        paramCheck(ecuSVer, CharSequenceUtil::isNotBlank, ValidConstant.MSG_NON_NULL_ECU_SVER);
        paramCheck(ecuSVer, v -> v.length() >= ValidConstant.MIN_ECU_SVER
                && v.length() <= ValidConstant.MAX_ECU_SVER,
            ValidConstant.MSG_LEN_ECU_SVER);
    }

    public static void checkEcuHsn(String ecuHsn) {
        if (CharSequenceUtil.isNotEmpty(ecuHsn)) {
            paramCheck(ecuHsn, h -> h.length() >= ValidConstant.MIN_ECU_HSN
                    && h.length() <= ValidConstant.MAX_ECU_HSN,
                ValidConstant.MSG_LEN_ECU_HSN);
        }
    }

    public static void checkSupplierCode(String supplierCode) {
        if (CharSequenceUtil.isNotEmpty(supplierCode)) {
            paramCheck(supplierCode, s -> s.length() >= ValidConstant.MIN_ECU_SUPPLIER_CODE
                    && s.length() <= ValidConstant.MAX_ECU_SUPPLIER_CODE,
                ValidConstant.MSG_LEN_ECU_SUPPLIER_CODE);
        }
    }

    public static void checkEcuHVer(String ecuHVer) {
        if (CharSequenceUtil.isNotEmpty(ecuHVer)) {
            paramCheck(ecuHVer, h -> h.length() >= ValidConstant.MIN_ECU_HVER
                    && h.length() <= ValidConstant.MAX_ECU_HVER,
                ValidConstant.MSG_LEN_ECU_HVER);
        }
    }

    public static void checkEcuProductDate(String productDate) {
        if (CharSequenceUtil.isNotEmpty(productDate)) {
            paramCheck(productDate, p -> p.length() >= ValidConstant.MIN_ECU_PRODUCT_DATE
                    && p.length() <= ValidConstant.MAX_ECU_PRODUCT_DATE,
                ValidConstant.MSG_LEN_ECU_PRODUCT_DATE);
        }
    }

    /**
     * 判断参数是否满足条件 如果条件不满足,将抛出 {@link BusinessException} {@link ResultEnum#PARAM_VERIFICATION_FAIL}
     *
     * @param param     被判断的参数
     * @param predicate 条件
     * @param remark    备注
     * @param        参数类型
     */
    public static  void paramCheck(T param, Predicate predicate, String remark) {
        if (!predicate.test(param)) {
            throw new BusinessException(ResultEnum.PARAM_VERIFICATION_FAIL, remark);
        }
    }

    /**
     * 判断条件是否满足,如果满足则执行下一步动作,否则什么也不做
     *
     * @param param     被判断的参数
     * @param predicate 条件
     * @param consumer  下一步动作
     * @param        参数类型
     */
    public static  void consumer(T param, Predicate predicate, Consumer consumer) {
        if (predicate.test(param)) {
            consumer.accept(param);
        }
    }
}

你可能感兴趣的:(#,工具类封装,#,java核心知识点,工具类,java8)