package com.zd.common.core.utils;

import cn.hutool.core.collection.CollUtil;
import com.zd.common.core.exception.ServiceException;
import org.springframework.lang.Nullable;

import java.util.Collection;
import java.util.function.Supplier;

/**
 * 断言 抛出业务异常
 *
 * @Author: zhoupan
 * @Date: 2021/09/09/9:10
 * @Description:
 */
public class Assert {

    /**
     * 集合空断言
     *
     * @param collection
     * @param messageSupplier
     */
    public static void isEmpty(@Nullable Collection<?> collection, Supplier<String> messageSupplier) {
        if (!CollUtil.isEmpty(collection)) {
            throw new ServiceException(messageSupplier.get());
        }
    }

    public static void isEmpty(@Nullable Collection<?> collection, String message) {
        if (!CollUtil.isEmpty(collection)) {
            throw new ServiceException(message);
        }
    }

    /**
     * 空断言
     *
     * @param o
     * @param messageSupplier
     */
    public static void isNull(Object o, Supplier<String> messageSupplier) {
        if (o != null) {
            throw new ServiceException(messageSupplier.get());
        }
    }

    public static void isNull(Object o, String message) {
        if (o != null) {
            throw new ServiceException(message);
        }
    }

    /**
     * 空断言
     *
     * @param o
     * @param messageSupplier
     */
    public static void isNotNull(Object o, Supplier<String> messageSupplier) {
        if (o == null) {
            throw new ServiceException(messageSupplier.get());
        }
    }

    public static void isNotNull(Object o, String message) {
        if (o == null) {
            throw new ServiceException(message);
        }
    }

    public static void notEmpty(@Nullable Collection<?> collection, Supplier<String> messageSupplier) {
        if (CollUtil.isEmpty(collection)) {
            throw new ServiceException(messageSupplier.get());
        }
    }

    public static void notEmpty(@Nullable Collection<?> collection, String message) {
        if (CollUtil.isEmpty(collection)) {
            throw new ServiceException(message);
        }
    }

    public static void isTrue(Boolean b, Supplier<String> messageSupplier) {
        if (b == null || !b) {
            throw new ServiceException(messageSupplier.get());
        }
    }

    public static void isTrue(Boolean b, String message) {
        if (b == null || !b) {
            throw new ServiceException(message);
        }
    }

    public static void isFalse(Boolean b, String message) {
        if (b == null || b) {
            throw new ServiceException(message);
        }
    }

    public static void isFalse(Boolean b, Supplier<String> messageSupplier) {
        if (b == null || b) {
            throw new ServiceException(messageSupplier.get());
        }
    }
}
