Skip to content

Commit

Permalink
Merge pull request #24 from lunasaw/2.5.6
Browse files Browse the repository at this point in the history
🔖 2.5.6 AssertUtil
  • Loading branch information
lunasaw authored Mar 17, 2024
2 parents bf07dd1 + 2afc87a commit 3cbba2a
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<groupId>io.github.lunasaw</groupId>
<artifactId>luna-common</artifactId>
<name>luna-common</name>
<version>2.5.5</version>
<version>2.5.6</version>
<description>common is project which contains common utils</description>
<url>https://github.com/lunasaw/luna-common</url>

Expand Down
106 changes: 106 additions & 0 deletions src/main/java/com/luna/common/check/AssertUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package com.luna.common.check;

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

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;

import com.luna.common.exception.BaseException;

/**
* 校验辅助类
*
* @author luna
**/
public class AssertUtil {

/**
* 私有构造方法
*/
private AssertUtil() {}

/**
* 校验指定对象不能为null
*
* @param object 对象
* @param baseException 运行时异常
*/
public static void notNull(Object object, BaseException baseException, String... extendInfos) {
isTrue(object != null, baseException, extendInfos);
}

/**
* 校验指定集合不能为空
*
* @param collection 集合
* @param baseException 运行时异常
*/
public static void notEmpty(Collection collection, BaseException baseException,
String... extendInfos) {
isTrue(CollectionUtils.isNotEmpty(collection),
baseException, extendInfos);
}

/**
* 校验指定对象不能为null
*
* @param map map集合
* @param baseException 运行时异常
*/
public static void notEmpty(Map map, BaseException baseException,
String... extendInfos) {
isTrue(MapUtils.isNotEmpty(map), baseException, extendInfos);
}

/**
* 校验字符串不能为空
*
* @param str 字符串
* @param baseException 运行时异常
*/
public static void notBlank(String str, BaseException baseException,
String... extendInfos) {
if (StringUtils.isBlank(str)) {
isTrue(false, baseException, extendInfos);
}
}

/**
* 校验指定条件为true
*
* @param condition 条件
* @param baseException 运行时异常
*/
public static void isFalse(boolean condition, BaseException baseException,
String... extendInfos) {
if (condition) {
fail(baseException, extendInfos);
}
}

/**
* 校验指定条件为true
*
* @param condition 条件
* @param baseException 运行时异常
*/
public static void isTrue(boolean condition, BaseException baseException,
String... extendInfos) {
if (!condition) {
fail(baseException, extendInfos);
}
}

/**
* 抛出指定运行时异常
*
* @param baseException 运行时异常
*/
public static void fail(BaseException baseException, String... extendInfos) {
Assert.notNull(baseException);

throw new BaseException(baseException, extendInfos);
}
}
17 changes: 15 additions & 2 deletions src/main/java/com/luna/common/exception/BaseException.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,23 @@
* @author luna
*/
public class BaseException extends RuntimeException {
public static final BaseException SYSTEM_ERROR = new BaseException(100000, "系统异常");
public static final BaseException UNKNOWN = new BaseException(600000, "系统错误");
public static final BaseException PARAMETER_ERROR = new BaseException(600001, "参数错误");
public static final BaseException MISSING = new BaseException(600002, "信息不存在");
public static final BaseException PARAMETER_OVERFLOW = new BaseException(600003, "参数个数超限");
public static final BaseException REPEAT_OPERATION = new BaseException(600004, "重复操作(幂等校验失败)");
public static final BaseException UN_SUPPORT_ENCRYPT_TYPE = new BaseException(600005, "不支持该数据加密类型");

/** 错误码 {@link ResultCode} */
private int code;
private int code;
/** 错误消息 */
private String message;
private String message;

public BaseException(BaseException baseException, String... extendMessage) {
this(baseException.getCode(),
String.format(baseException.getMessage(), extendMessage));
}

public BaseException(int status, String msg, Throwable throwable) {
super(throwable);
Expand Down

0 comments on commit 3cbba2a

Please sign in to comment.