diff --git a/luoo_common/pom.xml b/luoo_common/pom.xml index 3f91e65..5ae7f32 100644 --- a/luoo_common/pom.xml +++ b/luoo_common/pom.xml @@ -12,5 +12,12 @@ jjwt 0.9.1 + + + org.apache.commons + commons-lang3 + 3.14.0 + + diff --git a/luoo_common/src/main/java/com/luoo/common/exception/BizException.java b/luoo_common/src/main/java/com/luoo/common/exception/BizException.java new file mode 100644 index 0000000..486ed81 --- /dev/null +++ b/luoo_common/src/main/java/com/luoo/common/exception/BizException.java @@ -0,0 +1,20 @@ +package com.luoo.common.exception; + +public class BizException extends RuntimeException { + private static final long serialVersionUID = 2466145171145432619L; + + private Integer code = 500; + + public BizException(String message) { + super(message); + } + + public BizException(String message, Integer code) { + super(message); + this.code = code; + } + + public Integer getCode() { + return this.code; + } +} diff --git a/luoo_common/src/main/java/com/luoo/common/util/AssertUtil.java b/luoo_common/src/main/java/com/luoo/common/util/AssertUtil.java new file mode 100644 index 0000000..cf1926c --- /dev/null +++ b/luoo_common/src/main/java/com/luoo/common/util/AssertUtil.java @@ -0,0 +1,146 @@ +package com.luoo.common.util; + +import com.luoo.common.exception.BizException; +import org.apache.commons.lang3.StringUtils; + +import java.util.Arrays; +import java.util.Collection; + +public class AssertUtil { + + public static void mustTrue(boolean flag, String tips) { + if (!flag) { + assertError(tips); + } + } + + public static void mustFalse(boolean flag, String tips) { + mustTrue(!flag, tips); + } + + public static void valueLimit(Number value, Number min, Number max, String tips) { + if (null == value || value.longValue() < min.longValue() || value.longValue() > max.longValue()) { + assertError(tips); + } + } + + public static void valueMinLimit(Number value, Number min, String tips) { + if (null == value || value.longValue() < min.longValue()) { + assertError(tips); + } + } + + public static void valueMaxLimit(Number value, Number max, String tips) { + if (null == value || value.longValue() > max.longValue()) { + assertError(tips); + } + } + + public static void lengthLimit(String text, int min, int max, String tips) { + if (null == text || text.length() < min || text.length() > max) { + assertError(tips); + } + } + + public static void sizeLimit(Collection array, int min, int max, String tips) { + if (null == array || array.size() < min || array.size() > max) { + assertError(tips); + } + } + + public static void sizeLimit(Object[] array, int min, int max, String tips) { + if (null == array || array.length < min || array.length > max) { + assertError(tips); + } + } + + public static void mustNotIn(Object source, Object[] arrays, String tips) { + Object find = CollectionUtil.findAny(Arrays.asList(arrays), source::equals); + if (null != find) { + assertError(tips); + } + } + + public static void mustIn(Object source, Object[] arrays, String tips) { + Object find = CollectionUtil.findAny(Arrays.asList(arrays), source::equals); + if (null == find) { + assertError(tips); + } + } + + public static void mustNotEq(Object source, Object unexpect, String tips) { + if (unexpect.equals(source)) { + assertError(tips); + } + } + + public static void mustEq(Object source, Object expect, String tips) { + if (!expect.equals(source)) { + assertError(tips); + } + } + + public static void mustNull(Object source, String tips) { + if (null != source) { + assertError(tips); + } + } + + public static void mustNotNull(Object source, String tips) { + if (null == source) { + assertError(tips); + } + } + + public static void mustNotBlank(String text, String tips) { + if (StringUtils.isBlank(text)) { + assertError(tips); + } + } + + public static void mustEmpty(String text, String tips) { + if (!StringUtils.isEmpty(text)) { + assertError(tips); + } + } + + public static void mustEmpty(Collection collection, String tips) { + if (null != collection && collection.size() > 0) { + assertError(tips); + } + } + + public static void mustEmpty(Object[] arrays, String tips) { + if (null != arrays && arrays.length > 0) { + assertError(tips); + } + } + + public static void mustNotEmpty(String text, String tips) { + if (StringUtils.isEmpty(text)) { + assertError(tips); + } + } + + public static void mustNotEmpty(Collection collection, String tips) { + if (null == collection || collection.size() == 0) { + assertError(tips); + } + } + + public static void mustNotEmpty(Object[] arrays, String tips) { + if (null == arrays || arrays.length == 0) { + assertError(tips); + } + } + + public static void mustGtZero(Long value, String tips) { + if (value <= 0) { + assertError(tips); + } + } + + public static void assertError(String tips) { + throw new BizException(tips); + } +} diff --git a/luoo_common/src/main/java/com/luoo/common/util/CollectionUtil.java b/luoo_common/src/main/java/com/luoo/common/util/CollectionUtil.java new file mode 100644 index 0000000..1e93ee4 --- /dev/null +++ b/luoo_common/src/main/java/com/luoo/common/util/CollectionUtil.java @@ -0,0 +1,83 @@ +package com.luoo.common.util; + +import java.math.BigDecimal; +import java.util.Collection; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.function.*; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +public class CollectionUtil { + private static Boolean USE_PARALLEL = false; + + static void init(boolean useParallel) { + USE_PARALLEL = useParallel; + } + + public static T findFirst(Collection list, Predicate predicate) { + return stream(list).filter(predicate).findFirst().orElse((T) null); + } + + public static T findAny(Collection list, Predicate predicate) { + return stream(list).filter(predicate).findAny().orElse((T) null); + } + + public static List findList(Collection list, Predicate predicate) { + return (List)stream(list).filter(predicate).collect(Collectors.toList()); + } + + public static List toList(Collection list, Function function) { + return (List)stream(list).map(function).collect(Collectors.toList()); + } + + public static Set toSet(Collection list, Function function) { + return (Set)stream(list).map(function).collect(Collectors.toSet()); + } + + public static Map toMap(Collection list, Function keyMapper) { + return toMap(list, keyMapper, Function.identity()); + } + + public static Map toMap(Collection list, Function keyMapper, Function valueMapper) { + return (Map)stream(list).collect(Collectors.toMap(keyMapper, valueMapper)); + } + + public static Map groupBy(Collection list, Function function) { + return (Map)stream(list).collect(Collectors.groupingBy(function)); + } + + public static BigDecimal sum(Collection list, Function function) { + return (BigDecimal)stream(list).map((x) -> { + BigDecimal value = (BigDecimal)function.apply(x); + return value == null ? BigDecimal.ZERO : value; + }).reduce(BigDecimal.ZERO, BigDecimal::add); + } + + public static Long sum(Collection list, ToLongFunction function) { + return stream(list).mapToLong(function).sum(); + } + + public static Double sum(Collection list, ToDoubleFunction function) { + return stream(list).mapToDouble(function).sum(); + } + + public static Integer sum(Collection list, ToIntFunction function) { + return stream(list).mapToInt(function).sum(); + } + + public static Stream stream(Collection list) { + if (list == null) { + throw new NullPointerException("list is marked non-null but is null"); + } else { + return USE_PARALLEL ? list.parallelStream() : list.stream(); + } + } + + public static Predicate distinct(Function function) { + Set seen = ConcurrentHashMap.newKeySet(); + return t -> seen.add(function.apply(t)); + } +}