parent
8203992415
commit
21d10a00a6
@ -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;
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
@ -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> T findFirst(Collection<T> list, Predicate<T> predicate) {
|
||||
return stream(list).filter(predicate).findFirst().orElse((T) null);
|
||||
}
|
||||
|
||||
public static <T> T findAny(Collection<T> list, Predicate<T> predicate) {
|
||||
return stream(list).filter(predicate).findAny().orElse((T) null);
|
||||
}
|
||||
|
||||
public static <T> List findList(Collection<T> list, Predicate<T> predicate) {
|
||||
return (List)stream(list).filter(predicate).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public static <T, R> List toList(Collection<T> list, Function<T, R> function) {
|
||||
return (List)stream(list).map(function).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public static <T, R> Set toSet(Collection<T> list, Function<T, R> function) {
|
||||
return (Set)stream(list).map(function).collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
public static <T, K> Map toMap(Collection<T> list, Function<T, K> keyMapper) {
|
||||
return toMap(list, keyMapper, Function.identity());
|
||||
}
|
||||
|
||||
public static <T, K, U> Map toMap(Collection<T> list, Function<T, K> keyMapper, Function<T, U> valueMapper) {
|
||||
return (Map)stream(list).collect(Collectors.toMap(keyMapper, valueMapper));
|
||||
}
|
||||
|
||||
public static <T, R> Map groupBy(Collection<T> list, Function<T, R> function) {
|
||||
return (Map)stream(list).collect(Collectors.groupingBy(function));
|
||||
}
|
||||
|
||||
public static <T> BigDecimal sum(Collection<T> list, Function<T, BigDecimal> 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 <T> Long sum(Collection<T> list, ToLongFunction<T> function) {
|
||||
return stream(list).mapToLong(function).sum();
|
||||
}
|
||||
|
||||
public static <T> Double sum(Collection<T> list, ToDoubleFunction<T> function) {
|
||||
return stream(list).mapToDouble(function).sum();
|
||||
}
|
||||
|
||||
public static <T> Integer sum(Collection<T> list, ToIntFunction<T> function) {
|
||||
return stream(list).mapToInt(function).sum();
|
||||
}
|
||||
|
||||
public static <T> Stream<T> stream(Collection<T> 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 <T> Predicate<T> distinct(Function<T, ?> function) {
|
||||
Set<Object> seen = ConcurrentHashMap.newKeySet();
|
||||
return t -> seen.add(function.apply(t));
|
||||
}
|
||||
}
|
Loading…
Reference in new issue