package com.engine.matfron.util;

import com.engine.matfron.exception.CustomizeRunTimeException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.Collection;
import java.util.Map;

/**
 * @Author weaver_cl
 * @description:
 * @Date 2022/4/26
 * @Version V1.0
 **/
public abstract class CommonAssert {
    /**
     * 判断入参不为null
     *
     * @param object  待检查参数
     * @param message 检查失败返回的异常信息
     */
    public static void notNull(Object object, String message) {
        if (object == null) {
            throw new CustomizeRunTimeException(message);
        }
    }

    /**
     * 判断入参不为null
     *
     * @param string  待检查参数
     * @param message 检查失败返回的异常信息
     */
    public static void notNull(String string, String message) {
        if (StringUtils.isBlank(string)) {
            throw new CustomizeRunTimeException(message);
        }
    }

    /**
     * 判断多个入参不为null
     *
     * @param message 检查失败返回的异常信息
     * @param objects 待检查参数
     */
    public static void notNull(String message, Object... objects) {
        for (Object obj : objects) {
            if (obj == null) {
                throw new CustomizeRunTimeException(message);
            }
        }
    }

    /**
     * 判断入参为null
     *
     * @param object  待检查参数
     * @param message 检查失败返回的异常信息
     */
    public static void isNull(Object object, String message) {
        if (object != null) {
            throw new CustomizeRunTimeException(message);
        }
    }

    /**
     * 判断集合是否为空
     *
     * @param collection 待检查参数
     * @param message    检查失败返回的异常信息
     */
    public static void isEmpty(Collection<?> collection, String message) {
        if (!CollectionUtils.isEmpty(collection)) {
            throw new CustomizeRunTimeException(message);
        }
    }

    /**
     * 判断集合不为空
     *
     * @param collection 待检查参数
     * @param message    检查失败返回的异常信息
     */
    public static void notEmpty(Collection<?> collection, String message) {
        if (CollectionUtils.isEmpty(collection)) {
            throw new CustomizeRunTimeException(message);
        }
    }

    /**
     * 判断数组是否为空
     *
     * @param arr     待检查参数
     * @param message 检查失败返回的异常信息
     */
    public static void notEmpty(Object[] arr, String message) {
        if (ObjectUtils.isEmpty(arr)) {
            throw new CustomizeRunTimeException(message);
        }
    }

    /**
     * 判断map是否为空
     *
     * @param map     待检查参数
     * @param message 检查失败返回的异常信息
     */
    public static void notEmpty(Map<?, ?> map, String message) {
        if (CollectionUtils.isEmpty(map)) {
            throw new CustomizeRunTimeException(message);
        }
    }

    /**
     * 判断数组元素是否为空
     *
     * @param arr     待检查参数
     * @param message 检查失败返回的异常信息
     */
    public static void notNullElement(Object[] arr, String message) {
        if (arr != null) {
            for (Object obj : arr) {
                if (obj == null) {
                    throw new CustomizeRunTimeException(message);
                }
            }
        }
    }

    /**
     * 判断boolean
     *
     * @param expression 待检查参数
     * @param message    检查失败返回的异常信息
     */
    public static void isTrue(boolean expression, String message) {
        if (!expression) {
            throw new CustomizeRunTimeException(message);
        }
    }

    public static void isFalse(boolean expression, String message) {
        if (expression) {
            throw new CustomizeRunTimeException(message);
        }
    }

    public static void isBlank(CharSequence cs, String message) {
        int strLen;
        if (cs != null && (strLen = cs.length()) != 0) {
            for (int i = 0; i < strLen; ++i) {
                if (!Character.isWhitespace(cs.charAt(i))) {
                    throw new CustomizeRunTimeException(message);
                }
            }
        }
    }

    public static void notBlank(CharSequence cs, String message) {
        int strLen;
        if (cs != null && (strLen = cs.length()) != 0) {
            for (int i = 0; i < strLen; ++i) {
                if (Character.isWhitespace(cs.charAt(i))) {
                    throw new CustomizeRunTimeException(message);
                }
            }
        }
        if (cs == null || cs.length() == 0) {
            throw new CustomizeRunTimeException(message);
        }

    }
}
