package com.ld.igds.util;
|
|
import com.ld.igds.exception.BaseException;
|
import org.springframework.util.Assert;
|
import org.springframework.util.CollectionUtils;
|
import org.springframework.util.StringUtils;
|
|
import java.util.Collection;
|
import java.util.Map;
|
|
/**
|
* @author: andy.jia
|
* @description: 自定义工具类调整抛出异常信息,在异常中支持添加outId
|
* @date:2019.03.06
|
**/
|
public abstract class AssertUtil extends Assert {
|
|
|
/**
|
* @param object
|
* @param message
|
* @param outId
|
* @throws BaseException
|
*/
|
public static void isNull(Object object, String message, String outId) {
|
if (object != null) {
|
throw new BaseException(message, outId);
|
}
|
}
|
|
/**
|
* Assert that an object is not {@code null}.
|
* <pre class="code">Assert.notNull(clazz, "The class must not be null");</pre>
|
*
|
* @param object the object to check
|
* @param message the exception message to use if the assertion fails
|
* @throws IllegalArgumentException if the object is {@code null}
|
*/
|
public static void notNull(Object object, String message, String outId) {
|
if (object == null) {
|
throw new BaseException(message, outId);
|
}
|
}
|
|
/**
|
* Assert that the given String contains valid text content; that is, it must not
|
* be {@code null} and must contain at least one non-whitespace character.
|
* <pre class="code">Assert.hasText(name, "'name' must not be empty");</pre>
|
*
|
* @param text the String to check
|
* @param message the exception message to use if the assertion fails
|
* @throws IllegalArgumentException if the text does not contain valid text content
|
* @see StringUtils#hasText
|
*/
|
public static void hasText(String text, String message, String outId) {
|
if (!StringUtils.hasText(text)) {
|
throw new BaseException(message, outId);
|
}
|
}
|
|
/**
|
* Assert that a collection contains elements; that is, it must not be
|
* {@code null} and must contain at least one element.
|
* <pre class="code">Assert.notEmpty(collection, "Collection must contain elements");</pre>
|
*
|
* @param collection the collection to check
|
* @param message the exception message to use if the assertion fails
|
* @throws IllegalArgumentException if the collection is {@code null} or
|
* contains no elements
|
*/
|
public static void notEmpty(Collection<?> collection, String message, String outId) {
|
if (CollectionUtils.isEmpty(collection)) {
|
throw new BaseException(message, outId);
|
}
|
}
|
|
/**
|
* Assert that a Map contains entries; that is, it must not be {@code null}
|
* and must contain at least one entry.
|
* <pre class="code">Assert.notEmpty(map, "Map must contain entries");</pre>
|
*
|
* @param map the map to check
|
* @param message the exception message to use if the assertion fails
|
* @throws IllegalArgumentException if the map is {@code null} or contains no entries
|
*/
|
public static void notEmpty(Map<?, ?> map, String message, String outId) {
|
if (CollectionUtils.isEmpty(map)) {
|
throw new BaseException(message, outId);
|
}
|
}
|
}
|