每次抛异常之前都要if下,太麻烦了,整个工具类,简化代码,少点if代码也好看了。。。。
ExceptionEnum 是封装的异常枚举。
RRException是一个可以抛出的RuntimeException异常类。
public interface ExceptionEnum {
int getCode();
String getDesc();
}
public enum BizError implements ExceptionEnum {
PARAM_NULL_ERROR(8000001, "参数不能为空"),
PARAM_FAIL_CODE(8000002, "参数异常"),
;
private static final Map<Integer, BizError> VALUE_LOOK_UP = new ConcurrentHashMap<>(values().length);
static {
for (BizError type : EnumSet.allOf(BizError.class)) {
VALUE_LOOK_UP.put(type.code, type);
}
}
private final int code;
private final String msg;
BizError(int value, String name) {
this.code = value;
this.msg = name;
}
public static BizError resolve(Integer value) {
return (value != null ? VALUE_LOOK_UP.get(value) : null);
}
public static BizError fromValue(Integer value) {
BizError data = VALUE_LOOK_UP.get(value);
if (data == null) {
throw new IllegalArgumentException("参数[" + value + "]不正确,没有找到对应的 BizError");
}
return data;
}
@Override
public int getCode() {
return code;
}
@Override
public String getDesc() {
return msg;
}
public String getMsg() {
return msg;
}
}
public class RRException extends RuntimeException {
private static final long serialVersionUID = 1L;
private String msg;
private int code = 500000;
public RRException(String msg) {
super(msg);
this.msg = msg;
}
public RRException(String msg, Throwable e) {
super(msg, e);
this.msg = msg;
}
public RRException(int code, String msg) {
super(msg);
this.msg = msg;
this.code = code;
}
public RRException(int code, String msg, Throwable e) {
super(msg, e);
this.msg = msg;
this.code = code;
}
public RRException(ExceptionEnum e) {
super(e.getDesc());
this.code = e.getCode();
this.msg = e.getDesc();
}
public String getMsg() {
return this.msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public int getCode() {
return this.code;
}
public void setCode(int code) {
this.code = code;
}
}
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import java.util.Collection;
import java.util.Map;
import java.util.Objects;
public class CheckUtils {
public static void isEquals(Object obj1, Object obj2, ExceptionEnum error) {
if (Objects.equals(obj1, obj2)) {
throw new RRException(error);
}
}
public static void noEquals(Object obj1, Object obj2, ExceptionEnum error) {
if (!Objects.equals(obj1, obj2)) {
throw new RRException(error);
}
}
public static void isTrue(boolean b, ExceptionEnum error) {
if (b) {
throw new RRException(error);
}
}
public static void isFalse(boolean b, ExceptionEnum error) {
isTrue(!b, error);
}
public static void isNull(Object b, ExceptionEnum error) {
if (Objects.isNull(b)) {
throw new RRException(error);
}
}
public static void noNull(Object b, ExceptionEnum error) {
if (Objects.nonNull(b)) {
throw new RRException(error);
}
}
public static void isEmpty(Collection collection, ExceptionEnum error) {
if (CollectionUtils.isEmpty(collection)) {
throw new RRException(error);
}
}
public static void noEmpty(Collection collection, ExceptionEnum error) {
if (!CollectionUtils.isEmpty(collection)) {
throw new RRException(error);
}
}
public static void isEmpty(Map map, ExceptionEnum error) {
if (CollectionUtils.isEmpty(map)) {
throw new RRException(error);
}
}
public static void noEmpty(Map map, ExceptionEnum error) {
if (!CollectionUtils.isEmpty(map)) {
throw new RRException(error);
}
}
public static void isBlank(String string, ExceptionEnum error) {
if (StringUtils.isBlank(string)) {
throw new RRException(error);
}
}
public static void noBlank(String string, ExceptionEnum error) {
if (StringUtils.isNotBlank(string)) {
throw new RRException(error);
}
}
public static void isEquals(String obj1, String obj2, ExceptionEnum error) {
if (StringUtils.equals(obj1, obj2)) {
throw new RRException(error);
}
}
public static void noEquals(String obj1, String obj2, ExceptionEnum error) {
if (!StringUtils.equals(obj1, obj2)) {
throw new RRException(error);
}
}
public static void isEmpty(Object[] array, ExceptionEnum error) {
if (array.length == 0) {
throw new RRException(error);
}
}
public static void noEmpty(Object[] array, ExceptionEnum error) {
if (array.length > 0) {
throw new RRException(error);
}
}
public static void isEmpty(Object[] array, String error) {
if (array.length == 0) {
throw new RRException(error);
}
}
public static void noEmpty(Object[] array, String error) {
if (array.length > 0) {
throw new RRException(error);
}
}
public static void isEquals(String obj1, String obj2, String error) {
if (StringUtils.equals(obj1, obj2)) {
throw new RRException(error);
}
}
public static void noEquals(String obj1, String obj2, String error) {
if (!StringUtils.equals(obj1, obj2)) {
throw new RRException(error);
}
}
public static void throwException(ExceptionEnum error) {
throw new RRException(error);
}
public static void isEquals(Object obj1, Object obj2, String errorDesc) {
if (Objects.equals(obj1, obj2)) {
throw new RRException(errorDesc);
}
}
public static void noEquals(Object obj1, Object obj2, String errorDesc) {
if (!Objects.equals(obj1, obj2)) {
throw new RRException(errorDesc);
}
}
public static void isTrue(boolean b, String errorDesc) {
if (b) {
throw new RRException(errorDesc);
}
}
public static void isFalse(boolean b, String errorDesc) {
isTrue(!b, errorDesc);
}
public static void isNull(Object b, String errorDesc) {
if (Objects.isNull(b)) {
throw new RRException(errorDesc);
}
}
public static void noNull(Object b, String errorDesc) {
if (Objects.nonNull(b)) {
throw new RRException(errorDesc);
}
}
public static void isEmpty(Collection collection, String errorDesc) {
if (CollectionUtils.isEmpty(collection)) {
throw new RRException(errorDesc);
}
}
public static void noEmpty(Collection collection, String errorDesc) {
if (!CollectionUtils.isEmpty(collection)) {
throw new RRException(errorDesc);
}
}
public static void isEmpty(Map map, String errorDesc) {
if (CollectionUtils.isEmpty(map)) {
throw new RRException(errorDesc);
}
}
public static void noEmpty(Map map, String errorDesc) {
if (!CollectionUtils.isEmpty(map)) {
throw new RRException(errorDesc);
}
}
public static void noBlank(String string, String errorDesc) {
if (StringUtils.isNotBlank(string)) {
throw new RRException(errorDesc);
}
}
public static void isBlank(String string, String errorDesc) {
if (StringUtils.isBlank(string)) {
throw new RRException(errorDesc);
}
}
public static void throwException(String errorDesc) {
throw new RRException(errorDesc);
}
public static String isBlankToSelf(String s) {
return StringUtils.isBlank(s) ? "" : s;
}
}