优化空值针容错

This commit is contained in:
thinkgem
2023-04-03 11:26:54 +08:00
parent 6757958342
commit 8336b993ac
2 changed files with 29 additions and 25 deletions

View File

@@ -65,17 +65,26 @@ public class ListUtils extends org.apache.commons.collections.ListUtils {
@SafeVarargs
public static <E> ArrayList<E> newArrayList(E... elements) {
if (elements == null) {
return newArrayList();
}
ArrayList<E> list = new ArrayList<E>(elements.length);
Collections.addAll(list, elements);
return list;
}
public static <E> ArrayList<E> newArrayList(Iterable<? extends E> elements) {
if (elements == null) {
return newArrayList();
}
return (elements instanceof Collection) ? new ArrayList<E>(cast(elements))
: newArrayList(elements.iterator());
}
public static <E> ArrayList<E> newArrayList(Iterator<? extends E> elements) {
if (elements == null) {
return newArrayList();
}
ArrayList<E> list = newArrayList();
addAll(list, elements);
return list;
@@ -86,6 +95,9 @@ public class ListUtils extends org.apache.commons.collections.ListUtils {
}
public static <E> LinkedList<E> newLinkedList(Iterable<? extends E> elements) {
if (elements == null) {
return newLinkedList();
}
LinkedList<E> list = newLinkedList();
addAll(list, elements);
return list;
@@ -96,6 +108,9 @@ public class ListUtils extends org.apache.commons.collections.ListUtils {
}
public static <E> CopyOnWriteArrayList<E> newCopyOnWriteArrayList(Iterable<? extends E> elements) {
if (elements == null) {
return new CopyOnWriteArrayList<E>();
}
Collection<? extends E> elementsCollection = (elements instanceof Collection)
? cast(elements) : newArrayList(elements);
return new CopyOnWriteArrayList<E>(elementsCollection);
@@ -106,6 +121,9 @@ public class ListUtils extends org.apache.commons.collections.ListUtils {
}
private static <T> boolean addAll(Collection<T> addTo, Iterator<? extends T> iterator) {
if (addTo == null || iterator == null) {
return false;
}
boolean wasModified = false;
while (iterator.hasNext()) {
wasModified |= addTo.add(iterator.next());
@@ -114,6 +132,9 @@ public class ListUtils extends org.apache.commons.collections.ListUtils {
}
public static <T> boolean addAll(Collection<T> addTo, Iterable<? extends T> elementsToAdd) {
if (addTo == null || elementsToAdd == null) {
return false;
}
if (elementsToAdd instanceof Collection) {
Collection<? extends T> c = cast(elementsToAdd);
return addTo.addAll(c);

View File

@@ -4,25 +4,14 @@
*/
package com.jeesite.common.collect;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import com.jeesite.common.lang.StringUtils;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;
import com.jeesite.common.lang.StringUtils;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
/**
* Map工具类实现 Map <-> Bean 互相转换
@@ -85,8 +74,7 @@ public class MapUtils extends org.apache.commons.collections.MapUtils {
* @param clazz
* @param list
*/
public static <T, V> List<T> toObjectList(Class<T> clazz, List<HashMap<String, V>> list) throws Exception,
InvocationTargetException, NoSuchMethodException, InstantiationException {
public static <T, V> List<T> toObjectList(Class<T> clazz, List<HashMap<String, V>> list) throws Exception {
List<T> retList = new ArrayList<T>();
if (list != null && !list.isEmpty()) {
for (HashMap<String, V> m : list) {
@@ -101,8 +89,7 @@ public class MapUtils extends org.apache.commons.collections.MapUtils {
* @param clazz 目标对象的类
* @param map 待转换Map
*/
public static <T, V> T toObject(Class<T> clazz, Map<String, V> map) throws Exception,
InvocationTargetException {
public static <T, V> T toObject(Class<T> clazz, Map<String, V> map) throws Exception {
T object = clazz.getDeclaredConstructor().newInstance();
return toObject(object, map);
}
@@ -113,15 +100,13 @@ public class MapUtils extends org.apache.commons.collections.MapUtils {
* @param map 待转换Map
* @param toCamelCase 是否去掉下划线
*/
public static <T, V> T toObject(Class<T> clazz, Map<String, V> map, boolean toCamelCase) throws Exception,
InvocationTargetException {
public static <T, V> T toObject(Class<T> clazz, Map<String, V> map, boolean toCamelCase) throws Exception {
T object = clazz.getDeclaredConstructor().newInstance();
return toObject(object, map, toCamelCase);
}
/**
* 将Map转换为Object
* @param clazz 目标对象的类
* @param map 待转换Map
*/
public static <T, V> T toObject(T object, Map<String, V> map) throws InstantiationException, IllegalAccessException, InvocationTargetException {
@@ -203,9 +188,7 @@ public class MapUtils extends org.apache.commons.collections.MapUtils {
/**
* 转换成Map并提供字段命名驼峰转平行
* @param clazz 目标对象所在类
* @param object 目标对象
* @param map 待转换Map
* @throws NoSuchMethodException
* @throws InvocationTargetException
* @throws IllegalAccessException