寫一個(gè)自己的斷言

寫一個(gè)自己業(yè)務(wù)里的斷言,更好的描述業(yè)務(wù),帶來的是更好的擴(kuò)展性和靈活性,讓業(yè)務(wù)斷言更加的好用,下面分享一個(gè)斷言,雖然簡(jiǎn)單,但是好用,實(shí)用。

BusinessException 是自己定義的異常,甚至你也可以在拋出異常時(shí),帶上httpCode碼,

讓業(yè)務(wù)以不同的code來區(qū)分?jǐn)嘌援惓!?br>
package com.wlc.doc.util;

import com.wlc.doc.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import java.util.Collection;
import java.util.Map;

/**
 * 描述: 老王專用斷言 </br>
 * 時(shí)間: 2021-02-25 9:28  </br>
 * 作者:老王
 */
@Slf4j
public class AssertUtil extends Assert {

    /**
     * 期望值是等于,不滿足期望,則拋異常
     * @param o1 值1
     * @param o2 值2
     * @param message 異常消息
     */
    public static void isEquals(Object o1, Object o2 ,String message) {
        if (!o1.toString().equals(o2.toString())) {
            throw new BusinessException(message);
        }
    }

    /**
     * 期望值是不等于,不滿足期望,則拋異常
     * @param o1 值1
     * @param o2 值2
     * @param message 異常消息
     */
    public static void isNotEquals(Object o1, Object o2 ,String message) {
        if (o1.toString().equals(o2.toString())) {
            throw new BusinessException(message);
        }
    }
    /**
     * 期望值等于true,不滿足期望值即expression = false,則拋異常
     * @param expression 表達(dá)式boolean值
     * @param message 異常消息
     * @param logInfo 異常日志打印對(duì)象






     */
    public static void isTrue(boolean expression, String message, Object logInfo) {
        if (!expression) {
            if (!ObjectUtils.isEmpty(logInfo)){
                log.error(logInfo.toString());
            }
            throw new BusinessException(message);
        }
    }

    /**
     * 期望值等于true,不滿足期望值即expression = false,則拋異常
     * @param expression 表達(dá)式boolean值
     * @param message 異常消息
     */
    public static void isTrue(boolean expression, String message) {
        if (!expression) {
            throw new BusinessException(message);
        }
    }
    /**
     * 期望值等于true,不滿足期望值即expression = false,則拋異常
     * @param expression 表達(dá)式boolean值
     * @param message 異常消息
     */
    public static void isNotTrue(boolean expression, String message) {
        if (expression) {
            throw new BusinessException(message);
        }
    }
    /**
     * 期望值等于true,不滿足期望值即expression = false,則拋異常
     * @param expression 表達(dá)式boolean值
     * @param message 異常消息
     * @param logInfo 異常日志打印對(duì)象
     */
    public static void isNotTrue(boolean expression, String message, Object logInfo) {
        if (expression) {
            if (!ObjectUtils.isEmpty(logInfo)){
                log.error(logInfo.toString());
            }
            throw new BusinessException(message);
        }
    }
    /**
     * 期望值object = null,若object != null,則拋異常
     * @param object 判斷對(duì)象
     * @param message 異常消息
     * @param logInfo 異常日志
     */
    public static void isNull(@Nullable Object object, String message, Object logInfo) {
        if (object != null) {
            if (!ObjectUtils.isEmpty(logInfo)){
                log.error(logInfo.toString());
            }
            throw new BusinessException(message);
        }
    }

    /**
     * 期望值object = null,若object != null,則拋異常
     * @param object 判斷對(duì)象
     * @param message 異常消息
     */
    public static void isNull(@Nullable Object object, String message) {
        if (object != null) {
            throw new BusinessException(message);
        }
    }

    /**
     * 期望值object != null,若object == null,則拋異常
     * @param object 判斷對(duì)象
     * @param message 異常消息
     * @param logInfo 異常日志
     */
    public static void notNull(@Nullable Object object, String message, Object logInfo) {
        if (object == null) {
            if (!ObjectUtils.isEmpty(logInfo)){
                log.error(logInfo.toString());
            }
            throw new BusinessException(message);
        }
    }

    /**
     * 期望值object != null,若object == null,則拋異常
     * @param object 判斷對(duì)象
     * @param message 異常消息
     */
    public static void notNull(@Nullable Object object, String message) {
        if (ObjectUtils.isEmpty(object)) {
            throw new BusinessException(message);
        }
    }

    /**
     * textToSearch字符串是否包含 substring ,期望不包含
     * @param textToSearch 大區(qū)間str
     * @param substring  小區(qū)間str
     * @param message 異常消息
     * @param logInfo 打印日志對(duì)象
     */
    public static void doesNotContain(@Nullable String textToSearch, String substring, String message, Object logInfo) {
        if (StringUtils.hasLength(textToSearch) && StringUtils.hasLength(substring) && textToSearch.contains(substring)) {
            if (!ObjectUtils.isEmpty(logInfo)){
                log.error(logInfo.toString());
            }
            throw new BusinessException(message);
        }
    }
    /**
     * textToSearch字符串是否包含 substring ,期望不包含
     * @param textToSearch 大區(qū)間str
     * @param substring  小區(qū)間str
     * @param message 異常消息
     */
    public static void doesNotContain(@Nullable String textToSearch, String substring, String message) {
        if (StringUtils.hasLength(textToSearch) && StringUtils.hasLength(substring) && textToSearch.contains(substring)) {
            throw new BusinessException(message);
        }
    }

    /**
     * 期望值不為空
     * @param array 判斷對(duì)象數(shù)組
     * @param message 異常消息
     */
    public static void notEmpty(@Nullable Object[] array, String message) {
        if (ObjectUtils.isEmpty(array)) {
            throw new BusinessException(message);
        }
    }

    /**
     * 期望值不為空
     * @param array 判斷對(duì)象數(shù)組
     * @param message 異常消息
     * @param logInfo 輸出日志對(duì)象
     */
    public static void notEmpty(@Nullable Object[] array, String message, Object logInfo) {
        if (ObjectUtils.isEmpty(array)) {
            if (!ObjectUtils.isEmpty(logInfo)){
                log.error(logInfo.toString());
            }
            throw new BusinessException(message);
        }
    }

    /**
     * 期望值不為空
     * @param collection 判斷集合
     * @param message 異常消息提醒
     * @param logInfo 打印異常日志對(duì)象
     */
    public static void notEmpty(@Nullable Collection<?> collection, String message, Object logInfo) {
        if (CollectionUtils.isEmpty(collection)) {
            if (!ObjectUtils.isEmpty(logInfo)){
                log.error(logInfo.toString());
            }
            throw new BusinessException(message);
        }
    }

    /**
     * 期望值不為空
     * @param collection 判斷集合
     * @param message 異常消息提醒
     */
    public static void notEmpty(@Nullable Collection<?> collection, String message) {
        if (CollectionUtils.isEmpty(collection)) {
            throw new BusinessException(message);
        }
    }

    /**
     * 期望值不為空
     * @param map 判斷map
     * @param message 異常消息提醒
     * @param logInfo 打印異常日志對(duì)象
     */
    public static void notEmpty(@Nullable Map<?, ?> map, String message, Object logInfo) {
        if (CollectionUtils.isEmpty(map)) {
            if (!ObjectUtils.isEmpty(logInfo)){
                log.error(logInfo.toString());
            }
            throw new BusinessException(message);
        }
    }

    /**
     * 期望值不為空
     * @param map 異常消息提醒
     * @param message 異常消息提醒
     */
    public static void notEmpty(@Nullable Map<?, ?> map, String message) {
        if (CollectionUtils.isEmpty(map)) {
            throw new BusinessException(message);
        }
    }

}


















作者:IT學(xué)習(xí)道場(chǎng)

歡迎關(guān)注微信公眾號(hào) : IT學(xué)習(xí)道場(chǎng)