Assert.java 3.8 KB
Newer Older
苗卫卫 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
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");
    }
}