package com.starcharge.base.util;

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

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;

import com.ihidea.core.support.exception.ServiceException;

/**
 * 断言工具类
 * 
 * @author lilin
 * @version [4.0.0, 2017年6月28日]
 */
public final class Assert {
    /**
     * 判断是否为真
     * 
     * @param expression 表达式
     * @param message errorMessage
     */
    public static void isTrue(boolean expression, String message) {
        if (!expression) {
            throw new ServiceException(message);
        }
    }
    
    /**
     * 判断是否为真
     * 
     * @param expression 表达式
     */
    public static void isTrue(boolean expression) {
        isTrue(expression, "[Assertion failed] - this expression must be true");
    }
    
    /**
     * 判断对象是否为空
     * 
     * @param object 对象
     * @param message errorMessage
     */
    public static void isNull(Object object, String message) {
        if (object != null) {
            throw new ServiceException(message);
        }
    }
    
    /**
     * 判断对象是否为空
     * 
     * @param object 对象
     */
    public static void isNull(Object object) {
        isNull(object, "[Assertion failed] - the object argument must be null");
    }

    /**
     * 判断字符串是否为空
     *
     * @param str 对象
     * @param message errorMessage
     */
    public static void notBlank(String str, String message) {
        if (StringUtils.isBlank(str)) {
            throw new ServiceException(message);
        }
    }
    
    /**
     * 判断对象是否不为空
     * 
     * @param object 对象
     * @param message errorMessage
     */
    public static void notNull(Object object, String message) {
        if (object == null) {
            throw new ServiceException(message);
        }
    }
    
    /**
     * 判断对象是否不为空
     * 
     * @param object 对象
     */
    public static void notNull(Object object) {
        notNull(object, "[Assertion failed] - this argument is required; it must not be null");
    }
    
    /**
     * 判断字符串是否为空
     * 
     * @param str 字符串
     * @param message errorMessage
     */
    public static void notEmpty(String str, String message) {
        if (StringUtils.isEmpty(str)) {
            throw new ServiceException(message);
        }
    }
    
    /**
     * 判断字符串是否为空
     * 
     * @param str 字符串
     */
    public static void notEmpty(String str) {
        notEmpty(str, "[Assertion failed] - this string must not be empty");
    }
    
    /**
     * 判断集合是否为空
     * 
     * @param collection 集合
     * @param message errorMessage
     */
    public static void notEmpty(Collection<?> collection, String message) {
        if (CollectionUtils.isEmpty(collection)) {
            throw new ServiceException(message);
        }
    }
    
    /**
     * 判断集合是否为空
     * 
     * @param collection 集合
     */
    public static void notEmpty(Collection<?> collection) {
        notEmpty(collection, "[Assertion failed] - this collection must not be empty: it must contain at least 1 element");
    }
    
    /**
     * 判断map是否为空
     * 
     * @param map map集合
     * @param message errorMessage
     */
    public static void notEmpty(Map<?, ?> map, String message) {
        if (MapUtils.isEmpty(map)) {
            throw new ServiceException(message);
        }
    }
    
    /**
     * 判断map是否为空
     * 
     * @param map map集合
     */
    public static void notEmpty(Map<?, ?> map) {
        notEmpty(map, "[Assertion failed] - this map must not be empty: it must contain at least 1 element");
    }
}