package com.cf.imes.framework.common.util.Assert;


import cn.hutool.core.util.ObjectUtil;
import com.cf.imes.framework.common.exception.ErrorCode;
import com.cf.imes.framework.common.exception.util.ServiceExceptionUtil;
import org.springframework.util.ObjectUtils;

import java.util.List;

import static com.cf.imes.framework.common.exception.util.ServiceExceptionUtil.exception;



/**
 * 断言工具类：用于代码抛出异常，提升代码的简洁度
 *
 * @author 晨丰科技
 */
public class AssertUtils {


    private AssertUtils() {
        throw new IllegalStateException("AssertUtils class");
    }


    /*
    * 判断是否为空，可以不用if语句，简化代码
    * */
    public static void notEmpty(List<?> array, ErrorCode errorCode) {
        if (ObjectUtils.isEmpty(array)) {
            throw exception(errorCode);
        }
    }

    public static void empty(List<?> array, ErrorCode errorCode) {
        if (!ObjectUtils.isEmpty(array)) {
            throw exception(errorCode);
        }
    }

    public static void empty(List<?> array, ErrorCode errorCode, Object... params) {
        if (!ObjectUtils.isEmpty(array)) {
            throw ServiceExceptionUtil.exception(errorCode, params);
        }
    }

    public static void empty(Object object, ErrorCode errorCode) {
        if (object != null) {
            throw exception(errorCode);
        }
    }

    public static void notEmpty(Object object, ErrorCode errorCode) {
        if (object == null) {
            throw exception(errorCode);
        }
    }


    public static void notEmpty(List<?> array ,Object object, ErrorCode errorCode) {
        if (object == null || ObjectUtils.isEmpty(array)) {
            throw exception(errorCode);
        }
    }


    public static void notEmpty(List<?> array, ErrorCode errorCode,Object object) {
        if (ObjectUtils.isEmpty(array)) {
            throw exception(errorCode,object);
        }
    }

    public static void notEquals(Object obj1, Object obj2, ErrorCode errorCode) {
        if (ObjectUtil.notEqual(obj1, obj2)) {
            throw exception(errorCode);
        }
    }
}
