java common judge utils
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.ims.hornet.api.common.interfaces.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import java.io.*;
import java.lang.reflect.Array;
import java.util.*;
import java.util.Map.Entry;
import java.util.stream.Collectors;
import static org.apache.commons.lang3.StringUtils.isBlank;
import static org.apache.commons.lang3.StringUtils.isNotBlank;
public class CommonUtils {
private final static String[] EMPTY_ARRAY = new String[0];
private final static Integer[] EMPTY_ARRAY_INTEGER = new Integer[0];
/**
* @date: 2021-04-15 16:44
* @description:
*/
public static boolean notEmpty(String var) {
return isNotBlank(var);
}
/**
* @author: victor
* @date: 2021-04-15 16:44
* @description:
*/
public static boolean empty(String var) {
return isBlank(var);
}
/**
* @author: victor
* @date: 2021-04-15 16:44
* @description:
*/
public static boolean notEmpty(long var) {
return 0 != var;
}
/**
* @author: victor
* @date: 2021-04-15 16:44
* @description:
*/
public static boolean empty(long var) {
return 0 == var;
}
/**
* @author: victor
* @date: 2021-04-15 16:44
* @description:
*/
public static boolean notEmpty(int var) {
return 0 != var;
}
/**
* @author: victor
* @date: 2021-04-15 16:44
* @description:
*/
public static boolean empty(int var) {
return 0 == var;
}
/**
* @author: victor
* @date: 2021-04-15 16:44
* @description:
*/
public static boolean notEmpty(Object var) {
return null != var && !"".equals(var);
}
/**
* @author: victor
* @date: 2021-04-15 16:44
* @description:
*/
public static boolean empty(Object var) {
return null == var;
}
/**
* @author: victor
* @date: 2021-04-15 16:44
* @description:
*/
public static boolean notEmpty(Collection<?> var) {
return null != var && !var.isEmpty();
}
/**
* @author: victor
* @date: 2021-04-15 16:44
* @description:
*/
public static boolean empty(Collection<?> var) {
return null == var || var.isEmpty();
}
/**
* @author: victor
* @date: 2021-04-15 16:44
* @description:
*/
public static boolean notEmpty(Map<?, ?> var) {
return null != var && !var.isEmpty();
}
/**
* @author: victor
* @date: 2021-04-15 16:44
* @description:
*/
public static boolean empty(Map<?, ?> var) {
return null == var || var.isEmpty();
}
/**
* @author: victor
* @date: 2021-04-15 16:44
* @description:
*/
public static boolean notEmpty(File file) {
return null != file && file.exists();
}
/**
* @author: victor
* @date: 2021-04-15 16:44
* @description:
*/
public static boolean empty(File file) {
return null == file || !file.exists();
}
/**
* @author: victor
* @date: 2021-04-15 16:44
* @description:
*/
public static boolean notEmpty(Object[] var) {
return null != var && 0 < var.length;
}
/**
* @author: victor
* @date: 2021-04-15 16:44
* @description:
*/
public static boolean empty(Object[] var) {
return null == var || 0 == var.length;
}
/**
* @author: victor
* @date: 2021-04-15 16:44
* @description:
*/
public static boolean notEmpty(Set<?> var) {
return null != var && !var.isEmpty();
}
/**
* @author: victor
* @date: 2021-04-15 16:44
* @description:
*/
public static boolean empty(Set<?> var) {
return null == var || var.isEmpty();
}
/**
* @author: victor
* @date: 2021-07-09 13:29
* @description:
*/
public static boolean isTrue(Boolean var) {
if (var != null && var) {
return true;
} else {
return false;
}
}
/**
* @author: victor
* @date: 2021-04-15 16:44
* @description:
*/
public static <K extends Serializable, T extends BaseId<K>> Set<K> getIdSet(Collection<T> entityList) {
HashSet<K> idSet = Sets.newLinkedHashSet();
if (notEmpty(entityList)) {
for (T entity : entityList) {
idSet.add(entity.getId());
}
}
return idSet;
}
/**
* @author: victor
* @date: 2021-04-15 16:44
* @description:
*/
public static <K, T> Set<K> getIdSet(Collection<T> entityList, GetId<K, T> getId) {
HashSet<K> idSet = Sets.newLinkedHashSet();
if (notEmpty(entityList)) {
for (T entity : entityList) {
idSet.add(getId.getId(entity));
}
}
return idSet;
}
/**
* @author: victor
* @date: 2021-04-15 16:44
* @description:
*/
public static <K extends Serializable, T extends Serializable> Set<K> getIdSet(Collection<T> entityList, GetId<K, T> getId, boolean removeNull) {
HashSet<K> idSet = Sets.newLinkedHashSet();
if (notEmpty(entityList)) {
for (T entity : entityList) {
K value = getId.getId(entity);
if (removeNull && CommonUtils.empty(value)) {
continue;
}
idSet.add(getId.getId(entity));
}
}
return idSet;
}
/**
* @author: victor
* @date: 2021-04-15 16:44
* @description:
*/
public static <K extends Serializable, T extends Serializable> List<K> getList(Collection<T> entityList, GetId<K, T> getId) {
List<K> list = Lists.newArrayList();
if (notEmpty(entityList)) {
for (T entity : entityList) {
list.add(getId.getId(entity));
}
}
return list;
}
/**
* @author: victor
* @date: 2021-04-15 16:44
* @description:
*/
public static <K extends Serializable, T extends BaseId<K>> Map<K, T> getIdMap(Collection<T> entityList) {
Map<K, T> idMap = Maps.newLinkedHashMap();
if (notEmpty(entityList)) {
for (T entity : entityList) {
idMap.put(entity.getId(), entity);
}
}
return idMap;
}
/**
* @author: victor
* @date: 2021-04-15 16:44
* @description:
*/
public static <K, T> Map<K, T> getIdMap(Collection<T> entityList, GetId<K, T> getBaseId) {
Map<K, T> idMap = Maps.newLinkedHashMap();
if (notEmpty(entityList)) {
for (T entity : entityList) {
idMap.put(getBaseId.getId(entity), entity);
}
}
return idMap;
}
/**
* @author: victor
* @date: 2021-04-15 16:44
* @description:
*/
public static <K, V, T> Map<K, V> toMap(Collection<T> entityList, GetId<K, T> key, GetId<V, T> value) {
Map<K, V> idMap = Maps.newLinkedHashMap();
if (notEmpty(entityList)) {
for (T t : entityList) {
idMap.put(key.getId(t), value.getId(t));
}
}
return idMap;
}
/**
* @author: victor
* @date: 2021-04-15 16:44
* @description:
*/
public static <K extends Serializable, V extends Serializable, T extends Serializable> Map<K, V> toMapRemoveEmpty(Collection<T> entityList, GetId<K, T> key, GetId<V, T> value) {
Map<K, V> idMap = Maps.newLinkedHashMap();
if (empty(entityList)) {
return idMap;
}
for (T t : entityList) {
K k = key.getId(t);
V v = value.getId(t);
if (CommonUtils.empty(k) || CommonUtils.empty(v)) {
continue;
}
idMap.put(k, v);
}
return idMap;
}
/**
* @author: victor
* @date: 2021-04-15 16:44
* @description:
*/
public static <T extends Serializable> T deepClone(T a) {
T t = null;
if (a != null) {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(a);
oos.flush();
oos.close();
byte[] arrByte = baos.toByteArray();
ByteArrayInputStream bais = new ByteArrayInputStream(arrByte);
ObjectInputStream ois = new ObjectInputStream(bais);
t = (T) ois.readObject();
ois.close();
} catch (Exception e) {
}
}
return t;
}
/**
* @author: victor
* @date: 2021-04-15 16:44
* @description:
*/
public static String nullToEmpty(String str) {
return str == null ? "" : str;
}
/**
* @author: victor
* @date: 2021-04-15 16:44
* @description:
*/
public static Long nullToEmpty(Long long1) {
return long1 == null ? Long.valueOf(0) : long1;
}
/**
* @author: victor
* @date: 2021-04-15 16:44
* @description:
*/
public static <T> List<T> nullToEmpty(List<T> list) {
return list == null ? Lists.newArrayList() : list;
}
/**
* @author: victor
* @date: 2021-04-15 16:44
* @description:
*/
public static <K, V> Map<K, V> nullToEmpty(Map<K, V> map) {
return map == null ? Maps.<K, V>newHashMap() : map;
}
/**
* @author: victor
* @date: 2021-04-15 16:44
* @description:
*/
public static String[] nullToEmpty(String[] lastIgnores) {
return lastIgnores == null ? new String[0] : lastIgnores;
}
/**
* @author: victor
* @date: 2021-04-15 16:44
* @description:
*/
public static <T> String[] nullToEmptyForEach(String[] strs) {
return strs == null ? EMPTY_ARRAY : strs;
}
/**
* @author: victor
* @date: 2021-04-15 16:44
* @description:
*/
public static Integer[] nullToEmptyForEach(Integer[] arrs) {
return arrs == null ? EMPTY_ARRAY_INTEGER : arrs;
}
/**
* @author: victor
* @date: 2021-04-15 16:44
* @description:
*/
public static <T> Set<T> nullToEmptyForEach(Set<T> set) {
return set == null ? Collections.EMPTY_SET : set;
}
/**
* @author: victor
* @date: 2021-04-15 16:44
* @description:
*/
public static <K, V> Map<K, V> nullToEmptyForEach(Map<K, V> map) {
return map == null ? Collections.emptyMap() : map;
}
/**
* @author: victor
* @date: 2021-04-15 16:44
* @description:
*/
public static <T> Collection<T> nullToEmptyList(Collection<T> collection) {
return collection == null ? Collections.EMPTY_LIST : collection;
}
/**
* @author: victor
* @date: 2021-04-15 16:44
* @description:
*/
public static <K, V> LinkedHashMap<K, List<V>> group(List<V> list, GetId<K, V> getId) {
LinkedHashMap<K, List<V>> groupMap = Maps.newLinkedHashMap();
if (notEmpty(list)) {
for (V entity : list) {
K key = getId.getId(entity);
List<V> groupList = groupMap.get(key);
if (groupList == null) {
groupList = Lists.newArrayList();
groupMap.put(key, groupList);
}
groupList.add(entity);
}
}
return groupMap;
}
/**
* @author: victor
* @date: 2021-04-15 16:44
* @description:
*/
public static <T extends Serializable> List<T> deepCloneList(List<T> list) {
List<T> clones = Lists.newArrayList();
for (T t : nullToEmpty(list)) {
T clone = deepClone(t);
clones.add(clone);
}
return clones;
}
/**
* @author: victor
* @date: 2021-04-15 16:52
* @description:
*/
public static <T> T getOne(List<T> list) {
return notEmpty(list) ? list.get(0) : null;
}
/**
* @author: victor
* @date: 2021-04-15 16:52
* @description:
*/
public static <T> T getLast(List<T> list) {
return notEmpty(list) ? list.get(CommonUtils.size(list) - 1) : null;
}
/**
* @author: victor
* @date: 2021-04-15 16:50
* @description:
*/
public static <T, K> T convert(K source, Class<T> clazz) {
T toValue = Base.objectMapper.convertValue(source, clazz);
return toValue;
}
/**
* @author: victor
* @date: 2021-04-15 16:44
* @description:
*/
public static <T, K> List<T> convertList(List<K> fromList, Class<T> clazz) {
List<T> toList = Lists.newArrayList();
for (K k : CommonUtils.nullToEmpty(fromList)) {
toList.add(convert(k, clazz));
}
return toList;
}
/**
* @author: victor
* @date: 2021-04-15 16:44
* @description:
*/
public static <T> int size(Collection<T> list) {
return list == null ? 0 : list.size();
}
/**
* @author: victor
* @date: 2021-04-15 16:44
* @description:
*/
public static <K, V> int size(Map<K, V> map) {
return map == null ? 0 : map.size();
}
/**
* @author: victor
* @date: 2021-04-15 16:44
* @description:
*/
public static <T> int size(T[] arrs) {
return arrs == null ? 0 : arrs.length;
}
/**
* @author: victor
* @date: 2021-04-15 16:48
* @description:
*/
public static <T> List<T> filter(List<T> list, Filter<T> filter) {
if (empty(list)) return list;
Iterator<T> iterator = list.iterator();
while (iterator.hasNext()) {
T record = iterator.next();
if (!filter.accept(record)) {
iterator.remove();
}
}
return list;
}
/**
* @author: victor
* @date: 2021-04-15 16:48
* @description:
*/
public static <T> Set<T> filter(Set<T> set, Filter<T> filter) {
if (empty(set)) return set;
Iterator<T> iterator = set.iterator();
while (iterator.hasNext()) {
T record = iterator.next();
if (!filter.accept(record)) {
iterator.remove();
}
}
return set;
}
/**
* @author: victor
* @date: 2021-04-15 16:44
* @description:
*/
public static <K, V> void forEach(Map<K, V> map, ForEachMap<K, V> forEach) {
if (CommonUtils.empty(map)) {
return;
}
for (Entry<K, V> entry : map.entrySet()) {
forEach.forEach(entry.getKey(), entry.getValue());
}
}
/**
* @author: victor
* @date: 2021-04-15 16:44
* @description:
*/
public static <T extends Comparable<? super T>> void sort(List<T> list) {
if (CommonUtils.empty(list)) {
return;
}
Collections.sort(list);
}
/**
* @author: victor
* @date: 2021-04-15 16:44
* @description:
*/
public static <T> void sort(List<T> list, Comparator<T> c) {
if (CommonUtils.empty(list) || CommonUtils.empty(c)) {
return;
}
Collections.sort(list, c);
}
/**
* @author: victor
* @date: 2021-04-15 16:44
* @description:
*/
public static <T> void sort(List<T> list, Comparator<T>... cs) {
if (CommonUtils.empty(list) || CommonUtils.empty(cs)) {
return;
}
Collections.sort(list, (t1, t2) -> {
int i = 0;
for (Comparator c : cs) {
i = c.compare(t1, t2);
if (i != 0) {
return i;
}
}
return i;
});
}
/**
* @author: victor
* @date: 2021-04-15 16:44
* @description:
*/
public static <T> boolean contains(List<T> list, T record) {
if (CommonUtils.empty(list) || record == null) {
return false;
}
for (T t : list) {
if (record.equals(t)) {
return true;
}
}
return false;
}
/**
* @author: victor
* @date: 2021-04-15 16:44
* @description:
*/
public static <T> T createIfNull(T record, SingletonCrearter<T> creater) {
if (record == null) {
synchronized (CommonUtils.class) {
record = creater.createIfNull();
}
}
return record;
}
/**
* @author: victor
* @date: 2021-04-15 16:45
* @description:
*/
public static <T> List<T> toList(T[] arrs) {
List<T> list = Lists.newArrayList();
if (CommonUtils.notEmpty(arrs)) {
for (T record : arrs) {
list.add(record);
}
}
return list;
}
/**
* @author: victor
* @date: 2021-04-15 16:44
* @description:
*/
public static <T> Set<T> toSet(T[] arrs) {
Set<T> set = Sets.newHashSet();
if (CommonUtils.notEmpty(arrs)) {
for (T record : arrs) {
set.add(record);
}
}
return set;
}
/**
* @author: victor
* @date: 2021-04-15 16:44
* @description:
*/
public static <T> T[] toArray(List<T> idList, Class<T> type) {
T[] arr = (T[]) Array.newInstance(type, idList.size());
idList.toArray(arr);
return arr;
}
/**
* @author: victor
* @date: 2021-04-15 16:44
* @description:
*/
public static <T> T[] toArray(Set<T> idsets, Class<T> type) {
T[] arr = (T[]) Array.newInstance(type, idsets.size());
idsets.toArray(arr);
return arr;
}
/**
* @author: victor
* @date: 2021-04-15 16:44
* @description:
*/
public static <T> T defaultValue(T integer, T defaultValue) {
return integer != null ? integer : defaultValue;
}
/**
* @author: victor
* @date: 2021-04-15 16:44
* @description:
*/
public static <T> List<T> subList(List<T> list, int beginIndex, int endIndex) {
int size = CommonUtils.size(list);
if (beginIndex >= size) {
return Lists.newArrayList();
}
endIndex = Math.min(size, endIndex);
if (list != null && !list.isEmpty()) {
return Lists.newArrayList(list.subList(beginIndex, endIndex));
}
return Lists.newArrayList();
}
/**
* @author: victor
* @date: 2021-04-15 16:44
* @description:
*/
public static <T> List<List<T>> splitList(List<T> list, int size) {
List<List<T>> splitList = Lists.newArrayList();
int splitSize = CommonUtils.size(list) / size;
if (CommonUtils.size(list) % size > 0) {
splitSize += 1;
}
for (int i = 0; i < splitSize; i++) {
List<T> subList = CommonUtils.subList(list, i * size, (i + 1) * size);
splitList.add(subList);
}
return splitList;
}
/**
* @author: victor
* @date: 2021-04-19 11:11
* @description:
*/
public static String getStringRandom(int length) {
String val = "";
Random random = new Random(length);
for (int i = 0; i < length; i++) {
String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
if ("char".equalsIgnoreCase(charOrNum)) {
int temp = random.nextInt(2) % 2 == 0 ? 65 : 97;
val += (char) (random.nextInt(26) + temp);
} else if ("num".equalsIgnoreCase(charOrNum)) {
val += String.valueOf(random.nextInt(10));
}
}
return val;
}
/**
* @author: victor
* @date: 2021-04-15 16:44
* @description:
*/
public static <T> Set<T> findDuplicate(List<T> list) {
Set<T> items = new HashSet<>();
return list.stream()
.filter(n -> !items.add(n)) // Set.add() returns false if the element was already in the set.
.collect(Collectors.toSet());
}
/**
* @author: victor
* @date: 2021-04-15 16:44
* @description:
*/
public static <T> List<List<T>> splitSet(Set<T> set, int size) {
if (CommonUtils.empty(set)) {
return Lists.newArrayList();
}
List<T> list = set.stream().collect(Collectors.toList());
List<List<T>> splitList = splitList(list, size);
return splitList;
}
/**
* @author: victor
* @date: 2021-04-15 16:44
* @description:
*/
public static <T> T[] convertArray(Class<T> targetType, Object[] arrayObjects) {
if (targetType == null) {
return (T[]) arrayObjects;
}
if (arrayObjects == null) {
return null;
}
T[] targetArray = (T[]) Array.newInstance(targetType, arrayObjects.length);
try {
System.arraycopy(arrayObjects, 0, targetArray, 0, arrayObjects.length);
} catch (ArrayStoreException e) {
e.printStackTrace();
}
return targetArray;
}
/**
* @author: victor
* @date: 2021-04-19 10:27
* @description:
*/
public static String like(String var) {
return "%" + var + "%";
}
/**
* @author: victor
* @date: 2021-08-23 13:33
* @description:
*/
public static <K, V> void distinct(List<V> list, GetId<K, V> getId) {
final Set<K> set = Sets.newHashSet();
CommonUtils.filter(list, t -> set.add(getId.getId(t)));
}
/**
*@author: victor
*@date: 2021-08-30 19:10
*@description:
*/
public static void copyProperties(Object source, Object target) throws BeansException {
BeanUtils.copyProperties(source, target);
}
/**
*@author: victor
*@date: 2021-08-30 19:10
*@description:
*/
public static <T> T copyProperties(Object source, Class<T> clazz){
T t;
try {
t = clazz.newInstance();
} catch (InstantiationException e) {
throw new RuntimeException("instantiation exception happen when instance class:"+clazz.getName());
} catch (IllegalAccessException e) {
throw new RuntimeException("illegal Access exception happen when instance class:"+clazz.getName());
}
BeanUtils.copyProperties(source, t);
return t;
}
/**
*@author: victor
*@date: 2021-08-30 19:10
*@description:
*/
public static <T> List<T> copyProperties(List list, Class<T> clazz){
List<T> rtList=new ArrayList();
if(notEmpty(list)) {
for(Object object:list) {
T t = copyProperties(object, clazz);
if(notEmpty(t)){
rtList.add(t);
}
}
}
return rtList;
}
/**
*@author: victor
*@date: 2021-09-02 .
*@description:
*/
public static <T> void removeEmptyElements(List<T> list){
if(notEmpty(list)){
Iterator<T> iterator = list.iterator();
while(iterator.hasNext()){
T next = iterator.next();
if(next==null||"".equals(next)){
iterator.remove();
}
}
}
}
/**
*@author: victor
*@date: 2021-09-15
*@description:
*/
public static List<String> splitToList(String str,String seperator){
if(notEmpty(str)) {
List<String> list = new ArrayList<>();
String[] array = str.split(seperator);
for (int i = 0; i < array.length; i++) {
list.add(array[i]);
}
return list;
}
return Lists.newArrayList();
}
/**
*@author: victor
*@date: 2021-09-18 15:00
*@description: get random sub list
*/
public static <T extends Serializable> List<T> getRandomSubList (List<T> list, int subListCount) {
if (empty(list)) {
throw new RuntimeException("error,list is empty");
}
if (list.size() > 2000) {
throw new RuntimeException("error,list's size is too large,the maximum is 2000");
}
if (subListCount <=0) {
throw new RuntimeException("error,subListCount must be larger than 0");
}
if (subListCount > list.size()) {
throw new RuntimeException("error,subListCount is larger than list's size");
}
int[] allBalls = new int[list.size()];
for (int i = 0; i < allBalls.length; i++) {
allBalls[i] = i;
}
int[] hitNumbers = new int[subListCount];
int index;
Random random = new Random();
for (int i = 0; i < hitNumbers.length; i++) {
index = random.nextInt(allBalls.length - i);
hitNumbers[i] = allBalls[index];
int temp = allBalls[index];
allBalls[index] = allBalls[allBalls.length - 1 - i];
allBalls[allBalls.length - 1 - i] = temp;
}
List<T> resultList=new ArrayList<>();
for(int hitNumber:hitNumbers){
T t = list.get(hitNumber);
resultList.add(t);
}
return resultList;
}
/**
*@author: victor
*@date: 2021-10-14 16:35
*@description:
*/
public static String ifNull(String... strs){
for(String str:strs){
if(notEmpty(str)){
return str;
}
}
return null;
}
/**
*@author: victor
*@date: 2021-12-06 17:41
*@description:
*/
public static <T> int compare(Comparable<T> s1, T s2){
return compare(s1, s2, false, false);
}
/**
*@author: victor
*@date: 2021-12-06 17:41
*@description:
*/
public static <T> int compare(Comparable<T> s1, T s2, boolean nullFirst, boolean desc){
if(s1 == null){
if(s2 == null){
return 0;
}
return nullFirst?-1:1;
}else{
if(s2 == null){
return nullFirst?1:-1;
}
return desc? 0 - s1.compareTo(s2): s1.compareTo(s2);
}
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.