You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

418 lines
12 KiB
Java

package com.engine.salary.remote.cbs8;
import com.alibaba.fastjson.JSON;
import com.engine.salary.exception.CBS8RunTimeException;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collector;
import java.util.stream.Collectors;
/**
*
* <p>Copyright: Copyright (c) 2022</p>
* <p>Company: </p>
*
* @author qiantao
* @version 1.0
**/
public class SalaryEntityUtil {
private static final DecimalFormat decimalFormat = new DecimalFormat("#,##0.00");
/**
*
* 00.00000
*/
public static final String NUMBER_REGEX = "(-?[1-9]\\d*\\.?\\d+)|(-?0\\.\\d*[0-9])|(\\d+)";
/**
*
*
* @param originMap map
* @param targetMap map
*/
public static void thousandthConvert(Map<String, Object> originMap, Map<String, Object> targetMap) {
if (MapUtils.isNotEmpty(originMap)) {
originMap.forEach((k, v) -> {
if (StringUtils.isNotBlank(String.valueOf(v))) {
targetMap.put(k, decimalFormat.format(Double.valueOf(String.valueOf(v))));
}
});
}
}
/**
*
*
* @param originString
* @return
*/
public static String thousandthConvert(String originString) {
if (StringUtils.isNotBlank(originString)) {
return decimalFormat.format(Double.valueOf(originString));
}
return "0.00";
}
/**
* : null0Mapempty
*
* @param obj
* @return
*/
public static boolean isNullOrEmpty(Object obj) {
if (obj == null) {
return true;
}
if (obj instanceof CharSequence) {
return ((CharSequence) obj).length() == 0;
}
if (obj instanceof Collection) {
return ((Collection) obj).isEmpty();
}
if (obj instanceof Map) {
return ((Map) obj).isEmpty();
}
if (obj instanceof Object[]) {
Object[] object = (Object[]) obj;
if (object.length == 0) {
return true;
}
boolean empty = true;
for (int i = 0; i < object.length; i++) {
if (!isNullOrEmpty(object[i])) {
empty = false;
break;
}
}
return empty;
}
return false;
}
public static boolean isNotNullOrEmpty(Object obj) {
return !isNullOrEmpty(obj);
}
public static <R, T, A> A properties(Collection<T> objs, Function<T, R> function, Collector<R, ?, A> collectors) {
return objs.stream().map(function).collect(collectors);
}
public static <R, T> Set<R> properties(Collection<T> objs, Function<T, R> function) {
if (CollectionUtils.isEmpty(objs)) {
return Sets.newHashSet();
}
return properties(objs, function, Collectors.toSet());
}
public static <R, T> Map<R, T> convert2Map(Collection<T> objs, Function<T, R> function) {
if (CollectionUtils.isEmpty(objs)) {
return Maps.newHashMap();
}
return objs.stream().collect(Collectors.toMap(function, Function.identity(), (a, b) -> a));
}
public static <T, K, V> Map<K, V> convert2Map(Collection<T> objs, Function<T, K> keyMapper, Function<T, V> valueMapper) {
if (CollectionUtils.isEmpty(objs)) {
return Maps.newHashMap();
}
return objs.stream()
.filter(e -> valueMapper.apply(e) != null && keyMapper.apply(e) != null)
.collect(Collectors.toMap(keyMapper, valueMapper, (a, b) -> a));
}
public static <R, T> Map<R, List<T>> group2Map(Collection<T> objs, Function<T, R> function) {
if (CollectionUtils.isEmpty(objs)) {
return Maps.newHashMap();
}
return objs.stream().collect(Collectors.groupingBy(function));
}
public static <T, K, V> Map<K, Set<V>> group2Map(Collection<T> objs, Function<T, K> keyMapper, Function<T, V> valueMapper) {
if (CollectionUtils.isEmpty(objs)) {
return Maps.newHashMap();
}
return objs.stream()
.filter(e -> keyMapper.apply(e) != null && valueMapper.apply(e) != null)
.collect(Collectors.groupingBy(keyMapper,
Collectors.collectingAndThen(Collectors.toList(), e -> e.stream().map(valueMapper).collect(Collectors.toSet()))));
}
public static <T, K, V> Map<K, List<V>> group2ListMap(Collection<T> objs, Function<T, K> keyMapper, Function<T, V> valueMapper) {
if (CollectionUtils.isEmpty(objs)) {
return Maps.newHashMap();
}
return objs.stream()
.filter(e -> keyMapper.apply(e) != null && valueMapper.apply(e) != null)
.collect(Collectors.groupingBy(keyMapper,
Collectors.collectingAndThen(Collectors.toList(), e -> e.stream().map(valueMapper).collect(Collectors.toList()))));
}
public static <R, T, K, V> Map<R, Map<K, V>> list2Map(Collection<T> objs, Function<T, R> function1, Function<T, K> function2, Function<T, V> function3) {
if (CollectionUtils.isEmpty(objs)) {
return Maps.newHashMap();
}
Map<R, List<T>> collect = objs.stream().collect(Collectors.groupingBy(function1));
Map<R, Map<K, V>> map = new HashMap<>();
for (Map.Entry<R, List<T>> entry: collect.entrySet()) {
Map<K, V> values = map.getOrDefault(entry.getKey(), new HashMap<>());
entry.getValue().forEach(e -> values.put(function2.apply(e), function3.apply(e)));
map.put(entry.getKey(), values);
}
return map;
}
public static <R, T, K> Map<R, Map<K, T>> list2Map(Collection<T> objs, Function<T, R> function1, Function<T, K> function2) {
if (CollectionUtils.isEmpty(objs)) {
return Maps.newHashMap();
}
Map<R, List<T>> collect = objs.stream().collect(Collectors.groupingBy(function1));
Map<R, Map<K, T>> map = new HashMap<>();
for (Map.Entry<R, List<T>> entry: collect.entrySet()) {
Map<K, T> values = map.getOrDefault(entry.getKey(), new HashMap<>());
entry.getValue().forEach(e -> values.put(function2.apply(e), e));
map.put(entry.getKey(), values);
}
return map;
}
/**
* LinkedHashMap
*
* @param keyExtractor
* @param <T>
* @return
*/
public static <T> Predicate<T> distinctByKey(Function<? super T, Object> keyExtractor) {
LinkedHashMap<Object, Boolean> map = new LinkedHashMap<>();
return t -> map.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
}
/**
* ConcurrentHashMap
*
* @param keyExtractor
* @param <T>
* @return
*/
public static <T> Predicate<T> distinctByKeyMap(Function<? super T, Object> keyExtractor) {
ConcurrentHashMap<Object, Boolean> map = new ConcurrentHashMap<>();
return t -> map.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
}
public static <T> BigDecimal reduce(Collection<T> objs, Function<T, BigDecimal> function) {
if (CollectionUtils.isEmpty(objs)) {
return BigDecimal.ZERO;
}
return objs.stream()
.filter(e -> function.apply(e) != null)
.map(function)
.reduce(BigDecimal.ZERO, BigDecimal::add);
}
public static BigDecimal empty2Zero(String value) {
if (StringUtils.isEmpty(value)) {
return BigDecimal.ZERO;
}
try {
return new BigDecimal(value);
} catch (Exception e) {
return BigDecimal.ZERO;
}
}
/**
*
*
* @param list1
* @param list2
* @param <T>
* @return
*/
public static <T> boolean judgeIntersection(List<T> list1, List<T> list2) {
boolean flag = false;
List<T> origin = new ArrayList<>();
origin.addAll(list1);
origin.retainAll(list2);
if (origin.size() > 0) {
flag = true;
}
return flag;
}
/**
* StringLong
*
* @param obj
* @return
*/
public static Long string2Long(String obj) {
if (NumberUtils.isCreatable(obj)) {
return Long.valueOf(obj);
}
return null;
}
/**
* StringInteger
*
* @param obj
* @return
*/
public static Integer string2Integer(String obj) {
if (NumberUtils.isCreatable(obj)) {
return Integer.valueOf(obj);
}
return null;
}
/**
* StringBigDecimal
*
* @param obj
* @return
*/
public static BigDecimal string2BigDecimal(String obj) {
if (NumberUtils.isCreatable(obj)) {
return new BigDecimal(obj);
}
return null;
}
/**
* StringBigDecimal
*
* @param obj
* @return
*/
public static BigDecimal string2BigDecimalDefault0(String obj) {
if (NumberUtils.isCreatable(obj)) {
return new BigDecimal(obj);
}
return BigDecimal.ZERO;
}
/**
* 0
*
* @param obj
* @return
*/
public static boolean StringEqZERO(String obj) {
if (NumberUtils.isCreatable(obj)) {
return BigDecimal.ZERO.compareTo(new BigDecimal(obj)) == 0;
}
return false;
}
public static Double string2DoubleDefault0(String obj) {
if (NumberUtils.isCreatable(obj)) {
return new Double(obj);
}
return new Double("0.0");
}
/**
* forEasy
*
* @param arr1
* @param arr2
* @return
*/
public static <T> Collection<T> intersectionForList(Collection<T> arr1, Collection<T> arr2) {
Collection<T> resultList = new ArrayList<>();
if (CollectionUtils.isEmpty(arr1) || CollectionUtils.isEmpty(arr1)) {
return resultList;
}
arr1.forEach(a1 -> {
if (arr2.contains(a1)) {
resultList.add(a1);
}
});
return resultList;
}
public static String toJSONString(Object obj) {
if (obj != null) {
return JSON.toJSONString(obj);
}
return "";
}
public static String null2String(Object obj) {
if (Objects.isNull(obj)) {
return "";
}
return obj.toString();
}
public static String null2String(Object obj, String def) {
if (Objects.isNull(obj)) {
return def;
}
return obj.toString();
}
public static Integer getIntValue(Object obj, Integer def) {
if (Objects.isNull(obj)) {
return def;
}
try {
return StringUtils.isEmpty(String.valueOf(obj)) ? def : Integer.valueOf(String.valueOf(obj));
} catch (NumberFormatException e) {
return def;
}
}
public static BigDecimal getBigDecimal(Object value, int scale) {
String valueStr = null2String(value);
if (StringUtils.isEmpty(valueStr)) {
return BigDecimal.ZERO;
}
try {
return new BigDecimal(valueStr).setScale(scale, RoundingMode.HALF_UP);
} catch (NumberFormatException e) {
return null;
}
}
public static BigDecimal getBigDecimal(Object value, int scale, BigDecimal defValue) {
try {
return new BigDecimal(null2String(value)).setScale(scale, RoundingMode.HALF_UP);
} catch (NumberFormatException e) {
return defValue;
}
}
public static <T> T findFirst(Collection<T> objs) {
if (CollectionUtils.isEmpty(objs)) {
throw new CBS8RunTimeException("the collection can not be empty");
}
return objs.stream().findFirst().orElse(null);
}
}