|
| 1 | +package com.luna.common.check; |
| 2 | + |
| 3 | +import java.util.Collection; |
| 4 | +import java.util.Map; |
| 5 | + |
| 6 | +import org.apache.commons.collections4.CollectionUtils; |
| 7 | +import org.apache.commons.collections4.MapUtils; |
| 8 | +import org.apache.commons.lang3.StringUtils; |
| 9 | + |
| 10 | +import com.luna.common.exception.BaseException; |
| 11 | + |
| 12 | +/** |
| 13 | + * 校验辅助类 |
| 14 | + * |
| 15 | + * @author luna |
| 16 | + **/ |
| 17 | +public class AssertUtil { |
| 18 | + |
| 19 | + /** |
| 20 | + * 私有构造方法 |
| 21 | + */ |
| 22 | + private AssertUtil() {} |
| 23 | + |
| 24 | + /** |
| 25 | + * 校验指定对象不能为null |
| 26 | + * |
| 27 | + * @param object 对象 |
| 28 | + * @param baseException 运行时异常 |
| 29 | + */ |
| 30 | + public static void notNull(Object object, BaseException baseException, String... extendInfos) { |
| 31 | + isTrue(object != null, baseException, extendInfos); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * 校验指定集合不能为空 |
| 36 | + * |
| 37 | + * @param collection 集合 |
| 38 | + * @param baseException 运行时异常 |
| 39 | + */ |
| 40 | + public static void notEmpty(Collection collection, BaseException baseException, |
| 41 | + String... extendInfos) { |
| 42 | + isTrue(CollectionUtils.isNotEmpty(collection), |
| 43 | + baseException, extendInfos); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * 校验指定对象不能为null |
| 48 | + * |
| 49 | + * @param map map集合 |
| 50 | + * @param baseException 运行时异常 |
| 51 | + */ |
| 52 | + public static void notEmpty(Map map, BaseException baseException, |
| 53 | + String... extendInfos) { |
| 54 | + isTrue(MapUtils.isNotEmpty(map), baseException, extendInfos); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * 校验字符串不能为空 |
| 59 | + * |
| 60 | + * @param str 字符串 |
| 61 | + * @param baseException 运行时异常 |
| 62 | + */ |
| 63 | + public static void notBlank(String str, BaseException baseException, |
| 64 | + String... extendInfos) { |
| 65 | + if (StringUtils.isBlank(str)) { |
| 66 | + isTrue(false, baseException, extendInfos); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * 校验指定条件为true |
| 72 | + * |
| 73 | + * @param condition 条件 |
| 74 | + * @param baseException 运行时异常 |
| 75 | + */ |
| 76 | + public static void isFalse(boolean condition, BaseException baseException, |
| 77 | + String... extendInfos) { |
| 78 | + if (condition) { |
| 79 | + fail(baseException, extendInfos); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * 校验指定条件为true |
| 85 | + * |
| 86 | + * @param condition 条件 |
| 87 | + * @param baseException 运行时异常 |
| 88 | + */ |
| 89 | + public static void isTrue(boolean condition, BaseException baseException, |
| 90 | + String... extendInfos) { |
| 91 | + if (!condition) { |
| 92 | + fail(baseException, extendInfos); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * 抛出指定运行时异常 |
| 98 | + * |
| 99 | + * @param baseException 运行时异常 |
| 100 | + */ |
| 101 | + public static void fail(BaseException baseException, String... extendInfos) { |
| 102 | + Assert.notNull(baseException); |
| 103 | + |
| 104 | + throw new BaseException(baseException, extendInfos); |
| 105 | + } |
| 106 | +} |
0 commit comments