package com.codvision.commoncore.utils;


import com.codvision.commoncore.common.ResponseEntity;
import com.codvision.commoncore.exception.BusinessException;
import lombok.NonNull;

/**
 * @author hxl
 */
public final class Preconditions {

    /**
     * @param expression 布尔表达式
     * @param code 自定义响应状态码
     * @param errorMsg 检查失败时使用的异常消息
     * @throws BusinessException if {@code expression} is false
     */
    public static void checkArgument(boolean expression, int code, @NonNull Object errorMsg) {
        if (!expression) {
            throw new BusinessException(code, String.valueOf(errorMsg));
        }
    }

    public static void checkArgument(boolean expression, @NonNull Object errorMsg) {
        if (!expression) {
            throw new BusinessException(String.valueOf(errorMsg));
        }
    }

    public static void checkArgument(boolean expression, int code) {
        if (!expression) {
            throw new BusinessException(code, new ResponseEntity<>().setCode(code).getMessage());
        }
    }
}
