weaver-hrm-salary/src/com/engine/salary/util/SalaryEntityUtil.java

309 lines
9.5 KiB
Java
Raw Normal View History

2022-03-01 16:45:57 +08:00
package com.engine.salary.util;
2022-09-01 16:17:27 +08:00
import com.engine.salary.enums.SalaryRoundingModeEnum;
2022-03-01 16:45:57 +08:00
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;
2022-09-21 11:52:06 +08:00
import org.apache.commons.lang3.math.NumberUtils;
2022-03-01 16:45:57 +08:00
import java.math.BigDecimal;
2022-04-14 11:54:16 +08:00
import java.math.RoundingMode;
2022-03-01 16:45:57 +08:00
import java.text.DecimalFormat;
2022-07-05 15:04:00 +08:00
import java.util.*;
2022-10-08 09:14:13 +08:00
import java.util.concurrent.ConcurrentHashMap;
2022-03-01 16:45:57 +08:00
import java.util.function.Function;
2022-10-08 09:14:13 +08:00
import java.util.function.Predicate;
2022-03-01 16:45:57 +08:00
import java.util.stream.Collector;
import java.util.stream.Collectors;
/**
* @description: 实体类相关
* @author: xiajun
* @modified By: xiajun
* @date: Created in 10/28/21 5:36 PM
* @version:v1.0
*/
public class SalaryEntityUtil {
private static final DecimalFormat decimalFormat = new DecimalFormat("#,##0.00");
/**
* 千分位格式化
*
* @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";
}
/**
* 判断对象或对象数组中每一个对象是否为空: 对象为null字符序列长度为0集合类Map为empty
*
* @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()
2022-08-17 18:33:46 +08:00
.filter(e -> valueMapper.apply(e) != null && keyMapper.apply(e) != null)
.collect(Collectors.toMap(keyMapper, valueMapper, (a, b) -> a));
2022-03-01 16:45:57 +08:00
}
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()
2022-08-17 18:33:46 +08:00
.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()))));
2022-03-01 16:45:57 +08:00
}
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()
2022-08-17 18:33:46 +08:00
.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()))));
2022-03-01 16:45:57 +08:00
}
2022-10-08 09:14:13 +08:00
/**
* 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;
}
2022-03-01 16:45:57 +08:00
public static <T> BigDecimal reduce(Collection<T> objs, Function<T, BigDecimal> function) {
if (CollectionUtils.isEmpty(objs)) {
return BigDecimal.ZERO;
}
return objs.stream()
2022-08-17 18:33:46 +08:00
.filter(e -> function.apply(e) != null)
.map(function)
.reduce(BigDecimal.ZERO, BigDecimal::add);
2022-03-01 16:45:57 +08:00
}
public static BigDecimal empty2Zero(String value) {
if (StringUtils.isEmpty(value)) {
return BigDecimal.ZERO;
}
try {
return new BigDecimal(value);
} catch (Exception e) {
return BigDecimal.ZERO;
}
}
2022-04-14 11:54:16 +08:00
public static BigDecimal carryRule(Integer newScale, Integer rententionRule, BigDecimal value) {
2022-08-17 18:33:46 +08:00
//四舍五入
2022-07-05 15:04:00 +08:00
RoundingMode roundingMode = RoundingMode.HALF_UP;
2022-08-17 18:33:46 +08:00
//向上舍入
2022-07-05 15:04:00 +08:00
if (Objects.equals(rententionRule, 3)) {
roundingMode = RoundingMode.UP;
}
2022-08-17 18:33:46 +08:00
//向下舍入
2022-07-05 15:04:00 +08:00
if (Objects.equals(rententionRule, 4)) {
roundingMode = RoundingMode.DOWN;
}
2022-08-17 18:33:46 +08:00
//见分取角(只取保留小数后一位向上舍入)
if (Objects.equals(rententionRule, 5)) {
value = value.setScale(newScale + 1, RoundingMode.FLOOR);
roundingMode = RoundingMode.UP;
}
2022-09-01 16:17:27 +08:00
//向上取偶
if (Objects.equals(rententionRule, SalaryRoundingModeEnum.UP_EVEN.getValue())) {
value = value.setScale(0, RoundingMode.UP);
int number = value.intValue();
if (number % 2 != 0) {
value = value.add(BigDecimal.valueOf(1));
}
}
2022-08-17 18:33:46 +08:00
2022-07-05 15:04:00 +08:00
return value.setScale(newScale, roundingMode);
2022-04-14 11:54:16 +08:00
}
2022-08-24 12:03:04 +08:00
/**
* 两个集合是否有交集
2022-09-21 11:52:06 +08:00
*
2022-08-24 12:03:04 +08:00
* @param list1
* @param list2
* @param <T>
* @return
*/
2022-09-21 11:52:06 +08:00
public static <T> boolean judgeIntersection(List<T> list1, List<T> list2) {
2022-08-24 12:03:04 +08:00
boolean flag = false;
List<T> origin = new ArrayList<>();
origin.addAll(list1);
origin.retainAll(list2);
2022-09-21 11:52:06 +08:00
if (origin.size() > 0) {
2022-08-24 12:03:04 +08:00
flag = true;
}
return flag;
}
2022-09-21 11:52:06 +08:00
/**
* String转Long
2022-09-22 17:01:21 +08:00
*
2022-09-21 11:52:06 +08:00
* @param obj
* @return
*/
public static Long string2Long(String obj) {
if (NumberUtils.isCreatable(obj)) {
return Long.valueOf(obj);
}
return null;
}
2022-09-21 19:08:44 +08:00
/**
* String转BigDecimal
2022-09-22 17:01:21 +08:00
*
2022-09-21 19:08:44 +08:00
* @param obj
* @return
*/
public static BigDecimal string2BigDecimal(String obj) {
if (NumberUtils.isCreatable(obj)) {
return new BigDecimal(obj);
}
return null;
}
2022-09-22 16:48:59 +08:00
/**
* String转BigDecimal
2022-09-22 17:01:21 +08:00
*
2022-09-22 16:48:59 +08:00
* @param obj
* @return
*/
public static BigDecimal string2BigDecimalDefault0(String obj) {
if (NumberUtils.isCreatable(obj)) {
return new BigDecimal(obj);
}
return BigDecimal.ZERO;
}
2022-09-22 17:01:21 +08:00
/**
* 判断字符串是否等于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");
}
2022-03-01 16:45:57 +08:00
}