add emptyIfNull

This commit is contained in:
Looly
2019-08-21 16:12:06 +08:00
parent d5076d9b33
commit 2f2c9e58d2
5 changed files with 103 additions and 55 deletions

View File

@@ -8,6 +8,7 @@
### 新特性 ### 新特性
* 【core】 改进CollUtil.zip逻辑减少内存复制issue#I10T01@Gitee * 【core】 改进CollUtil.zip逻辑减少内存复制issue#I10T01@Gitee
* 【extra】 邮件增加图片支持pr#495@Github * 【extra】 邮件增加图片支持pr#495@Github
* 【core】 MapUtil、CollUtil增加emptyIfNullissue#502@Github
### Bug修复 ### Bug修复
* 【http】 修复HttpRquest中body方法长度计算问题issue#I10UPG@Gitee * 【http】 修复HttpRquest中body方法长度计算问题issue#I10UPG@Gitee

View File

@@ -60,6 +60,32 @@ import cn.hutool.core.util.TypeUtil;
*/ */
public class CollUtil { public class CollUtil {
/**
* 如果提供的集合为{@code null},返回一个不可变的默认空集合,否则返回原集合<br>
* 空集合使用{@link Collections#emptySet()}
*
* @param <T> 集合元素类型
* @param set 提供的集合可能为null
* @return 原集合若为null返回空集合
* @since 4.6.3
*/
public static <T> Set<T> emptyIfNull(Set<T> set) {
return (null == set) ? Collections.<T>emptySet() : set;
}
/**
* 如果提供的集合为{@code null},返回一个不可变的默认空集合,否则返回原集合<br>
* 空集合使用{@link Collections#emptyList()}
*
* @param <T> 集合元素类型
* @param set 提供的集合可能为null
* @return 原集合若为null返回空集合
* @since 4.6.3
*/
public static <T> List<T> emptyIfNull(List<T> set) {
return (null == set) ? Collections.<T>emptyList() : set;
}
/** /**
* 两个集合的并集<br> * 两个集合的并集<br>
* 针对一个集合中存在多个相同元素的情况,计算两个集合中此元素的个数,保留最多的个数<br> * 针对一个集合中存在多个相同元素的情况,计算两个集合中此元素的个数,保留最多的个数<br>
@@ -245,7 +271,7 @@ public class CollUtil {
} }
return false; return false;
} }
/** /**
* 集合1中是否包含集合2中所有的元素既集合2是否为集合1的子集 * 集合1中是否包含集合2中所有的元素既集合2是否为集合1的子集
* *
@@ -258,7 +284,7 @@ public class CollUtil {
if (isEmpty(coll1) || isEmpty(coll2) || coll1.size() < coll2.size()) { if (isEmpty(coll1) || isEmpty(coll2) || coll1.size() < coll2.size()) {
return false; return false;
} }
for (Object object : coll2) { for (Object object : coll2) {
if (false == coll1.contains(object)) { if (false == coll1.contains(object)) {
return false; return false;
@@ -851,9 +877,9 @@ public class CollUtil {
if (list == null) { if (list == null) {
return null; return null;
} }
if(list.isEmpty()) { if (list.isEmpty()) {
return new ArrayList<>(0); return new ArrayList<>(0);
} }
final int size = list.size(); final int size = list.size();
@@ -1161,10 +1187,10 @@ public class CollUtil {
*/ */
public static List<Object> extract(Iterable<?> collection, Editor<Object> editor, boolean ignoreNull) { public static List<Object> extract(Iterable<?> collection, Editor<Object> editor, boolean ignoreNull) {
final List<Object> fieldValueList = new ArrayList<>(); final List<Object> fieldValueList = new ArrayList<>();
if(null == collection) { if (null == collection) {
return fieldValueList; return fieldValueList;
} }
Object value; Object value;
for (Object bean : collection) { for (Object bean : collection) {
value = editor.edit(bean); value = editor.edit(bean);
@@ -1519,7 +1545,7 @@ public class CollUtil {
int entryCount = Math.min(keys.size(), values.size()); int entryCount = Math.min(keys.size(), values.size());
final Map<K, V> map = newHashMap(entryCount); final Map<K, V> map = newHashMap(entryCount);
final Iterator<K> keyIterator = keys.iterator(); final Iterator<K> keyIterator = keys.iterator();
final Iterator<V> valueIterator = values.iterator(); final Iterator<V> valueIterator = values.iterator();
while (entryCount > 0) { while (entryCount > 0) {
@@ -1863,14 +1889,14 @@ public class CollUtil {
} }
final int size = collection.size(); final int size = collection.size();
if(0 == size) { if (0 == size) {
return null; return null;
} }
if (index < 0) { if (index < 0) {
index += size; index += size;
} }
// 检查越界 // 检查越界
if (index >= size) { if (index >= size) {
return null; return null;
@@ -2419,7 +2445,7 @@ public class CollUtil {
} }
return list; return list;
} }
/** /**
* 获取指定Map列表中所有的Key * 获取指定Map列表中所有的Key
* *
@@ -2428,18 +2454,18 @@ public class CollUtil {
* @return key集合 * @return key集合
* @since 4.5.12 * @since 4.5.12
*/ */
public static <K> Set<K> keySet(Collection<Map<K, ?>> mapCollection){ public static <K> Set<K> keySet(Collection<Map<K, ?>> mapCollection) {
if(isEmpty(mapCollection)) { if (isEmpty(mapCollection)) {
return new HashSet<>(); return new HashSet<>();
} }
final HashSet<K> set = new HashSet<>(mapCollection.size() * 16); final HashSet<K> set = new HashSet<>(mapCollection.size() * 16);
for (Map<K,?> map : mapCollection) { for (Map<K, ?> map : mapCollection) {
set.addAll(map.keySet()); set.addAll(map.keySet());
} }
return set; return set;
} }
/** /**
* 获取指定Map列表中所有的Value * 获取指定Map列表中所有的Value
* *
@@ -2448,12 +2474,12 @@ public class CollUtil {
* @return Value集合 * @return Value集合
* @since 4.5.12 * @since 4.5.12
*/ */
public static <V> List<V> values(Collection<Map<?, V>> mapCollection){ public static <V> List<V> values(Collection<Map<?, V>> mapCollection) {
final List<V> values = new ArrayList<>(); final List<V> values = new ArrayList<>();
for (Map<?, V> map : mapCollection) { for (Map<?, V> map : mapCollection) {
values.addAll(map.values()); values.addAll(map.values());
} }
return values; return values;
} }

View File

@@ -30,6 +30,17 @@ public class DateTime extends Date {
private Week firstDayOfWeek = Week.MONDAY; private Week firstDayOfWeek = Week.MONDAY;
/** 时区 */ /** 时区 */
private TimeZone timeZone; private TimeZone timeZone;
/**
* 转换时间戳为 DateTime
*
* @param timeMillis 时间戳,毫秒数
* @return DateTime
* @since 4.6.3
*/
public static DateTime of(long timeMillis) {
return new DateTime(timeMillis);
}
/** /**
* 转换JDK date为 DateTime * 转换JDK date为 DateTime

View File

@@ -2,6 +2,7 @@ package cn.hutool.core.map;
import java.util.AbstractMap; import java.util.AbstractMap;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
@@ -57,6 +58,19 @@ public class MapUtil {
return null != map && false == map.isEmpty(); return null != map && false == map.isEmpty();
} }
/**
* 如果提供的集合为{@code null},返回一个不可变的默认空集合,否则返回原集合<br>
* 空集合使用{@link Collections#emptyMap()}
*
* @param <T> 集合元素类型
* @param set 提供的集合可能为null
* @return 原集合若为null返回空集合
* @since 4.6.3
*/
public static <K, V> Map<K, V> emptyIfNull(Map<K, V> set) {
return (null == set) ? Collections.<K, V>emptyMap() : set;
}
// ----------------------------------------------------------------------------------------------- new HashMap // ----------------------------------------------------------------------------------------------- new HashMap
/** /**
* 新建一个HashMap * 新建一个HashMap
@@ -134,14 +148,14 @@ public class MapUtil {
} }
return treeMap; return treeMap;
} }
/** /**
* 创建键不重复Map * 创建键不重复Map
* *
* @return {@link IdentityHashMap} * @return {@link IdentityHashMap}
* @since 4.5.7 * @since 4.5.7
*/ */
public static <K, V> Map<K, V> newIdentityMap(int size){ public static <K, V> Map<K, V> newIdentityMap(int size) {
return new IdentityHashMap<>(size); return new IdentityHashMap<>(size);
} }
@@ -204,11 +218,7 @@ public class MapUtil {
* </pre> * </pre>
* *
* <pre> * <pre>
* Map&lt;Object, Object&gt; colorMap = MapUtil.of(new String[][] { * Map&lt;Object, Object&gt; colorMap = MapUtil.of(new String[][] { { "RED", "#FF0000" }, { "GREEN", "#00FF00" }, { "BLUE", "#0000FF" } });
* {"RED", "#FF0000"},
* {"GREEN", "#00FF00"},
* {"BLUE", "#0000FF"}
* });
* </pre> * </pre>
* *
* 参考commons-lang * 参考commons-lang
@@ -388,7 +398,7 @@ public class MapUtil {
public static <K, V> Map<K, V> toCamelCaseMap(Map<K, V> map) { public static <K, V> Map<K, V> toCamelCaseMap(Map<K, V> map) {
return (map instanceof LinkedHashMap) ? new CamelCaseLinkedMap<>(map) : new CamelCaseMap<>(map); return (map instanceof LinkedHashMap) ? new CamelCaseLinkedMap<>(map) : new CamelCaseMap<>(map);
} }
/** /**
* 将键值对转换为二维数组第一维是key第二纬是value * 将键值对转换为二维数组第一维是key第二纬是value
* *
@@ -397,15 +407,15 @@ public class MapUtil {
* @since 4.1.9 * @since 4.1.9
*/ */
public static Object[][] toObjectArray(Map<?, ?> map) { public static Object[][] toObjectArray(Map<?, ?> map) {
if(map == null) { if (map == null) {
return null; return null;
} }
final Object[][] result = new Object[map.size()][2]; final Object[][] result = new Object[map.size()][2];
if(map.isEmpty()) { if (map.isEmpty()) {
return result; return result;
} }
int index = 0; int index = 0;
for(Entry<?, ?> entry : map.entrySet()) { for (Entry<?, ?> entry : map.entrySet()) {
result[index][0] = entry.getKey(); result[index][0] = entry.getKey();
result[index][1] = entry.getValue(); result[index][1] = entry.getValue();
index++; index++;
@@ -489,10 +499,10 @@ public class MapUtil {
* @return 过滤后的Map * @return 过滤后的Map
*/ */
public static <K, V> Map<K, V> filter(Map<K, V> map, Editor<Entry<K, V>> editor) { public static <K, V> Map<K, V> filter(Map<K, V> map, Editor<Entry<K, V>> editor) {
if(null == map || null == editor) { if (null == map || null == editor) {
return map; return map;
} }
final Map<K, V> map2 = ObjectUtil.clone(map); final Map<K, V> map2 = ObjectUtil.clone(map);
if (isEmpty(map2)) { if (isEmpty(map2)) {
return map2; return map2;
@@ -525,10 +535,10 @@ public class MapUtil {
* @since 3.1.0 * @since 3.1.0
*/ */
public static <K, V> Map<K, V> filter(Map<K, V> map, Filter<Entry<K, V>> filter) { public static <K, V> Map<K, V> filter(Map<K, V> map, Filter<Entry<K, V>> filter) {
if(null == map || null == filter) { if (null == map || null == filter) {
return map; return map;
} }
final Map<K, V> map2 = ObjectUtil.clone(map); final Map<K, V> map2 = ObjectUtil.clone(map);
if (isEmpty(map2)) { if (isEmpty(map2)) {
return map2; return map2;
@@ -542,7 +552,7 @@ public class MapUtil {
} }
return map2; return map2;
} }
/** /**
* 过滤Map保留指定键值对如果键不存在跳过 * 过滤Map保留指定键值对如果键不存在跳过
* *
@@ -562,7 +572,7 @@ public class MapUtil {
map2.clear(); map2.clear();
for (K key : keys) { for (K key : keys) {
if(map.containsKey(key)) { if (map.containsKey(key)) {
map2.put(key, map.get(key)); map2.put(key, map.get(key));
} }
} }
@@ -675,7 +685,7 @@ public class MapUtil {
public static MapProxy createProxy(Map<?, ?> map) { public static MapProxy createProxy(Map<?, ?> map) {
return MapProxy.create(map); return MapProxy.create(map);
} }
/** /**
* 创建Map包装类MapWrapper<br> * 创建Map包装类MapWrapper<br>
* {@link MapWrapper}对Map做一次包装 * {@link MapWrapper}对Map做一次包装
@@ -745,7 +755,7 @@ public class MapUtil {
} }
}); });
} }
/** /**
* 获取Map指定key的值并转换为字符串 * 获取Map指定key的值并转换为字符串
* *
@@ -757,7 +767,7 @@ public class MapUtil {
public static String getStr(Map<?, ?> map, Object key) { public static String getStr(Map<?, ?> map, Object key) {
return get(map, key, String.class); return get(map, key, String.class);
} }
/** /**
* 获取Map指定key的值并转换为Integer * 获取Map指定key的值并转换为Integer
* *
@@ -769,7 +779,7 @@ public class MapUtil {
public static Integer getInt(Map<?, ?> map, Object key) { public static Integer getInt(Map<?, ?> map, Object key) {
return get(map, key, Integer.class); return get(map, key, Integer.class);
} }
/** /**
* 获取Map指定key的值并转换为Double * 获取Map指定key的值并转换为Double
* *
@@ -781,7 +791,7 @@ public class MapUtil {
public static Double getDouble(Map<?, ?> map, Object key) { public static Double getDouble(Map<?, ?> map, Object key) {
return get(map, key, Double.class); return get(map, key, Double.class);
} }
/** /**
* 获取Map指定key的值并转换为Float * 获取Map指定key的值并转换为Float
* *
@@ -793,7 +803,7 @@ public class MapUtil {
public static Float getFloat(Map<?, ?> map, Object key) { public static Float getFloat(Map<?, ?> map, Object key) {
return get(map, key, Float.class); return get(map, key, Float.class);
} }
/** /**
* 获取Map指定key的值并转换为Short * 获取Map指定key的值并转换为Short
* *
@@ -805,7 +815,7 @@ public class MapUtil {
public static Short getShort(Map<?, ?> map, Object key) { public static Short getShort(Map<?, ?> map, Object key) {
return get(map, key, Short.class); return get(map, key, Short.class);
} }
/** /**
* 获取Map指定key的值并转换为Bool * 获取Map指定key的值并转换为Bool
* *
@@ -817,7 +827,7 @@ public class MapUtil {
public static Boolean getBool(Map<?, ?> map, Object key) { public static Boolean getBool(Map<?, ?> map, Object key) {
return get(map, key, Boolean.class); return get(map, key, Boolean.class);
} }
/** /**
* 获取Map指定key的值并转换为Character * 获取Map指定key的值并转换为Character
* *
@@ -829,7 +839,7 @@ public class MapUtil {
public static Character getChar(Map<?, ?> map, Object key) { public static Character getChar(Map<?, ?> map, Object key) {
return get(map, key, Character.class); return get(map, key, Character.class);
} }
/** /**
* 获取Map指定key的值并转换为Long * 获取Map指定key的值并转换为Long
* *
@@ -841,7 +851,7 @@ public class MapUtil {
public static Long getLong(Map<?, ?> map, Object key) { public static Long getLong(Map<?, ?> map, Object key) {
return get(map, key, Long.class); return get(map, key, Long.class);
} }
/** /**
* 获取Map指定key的值并转换为{@link Date} * 获取Map指定key的值并转换为{@link Date}
* *
@@ -853,7 +863,7 @@ public class MapUtil {
public static Date getDate(Map<?, ?> map, Object key) { public static Date getDate(Map<?, ?> map, Object key) {
return get(map, key, Date.class); return get(map, key, Date.class);
} }
/** /**
* 获取Map指定key的值并转换为指定类型 * 获取Map指定key的值并转换为指定类型
* *
@@ -867,7 +877,7 @@ public class MapUtil {
public static <T> T get(Map<?, ?> map, Object key, Class<T> type) { public static <T> T get(Map<?, ?> map, Object key, Class<T> type) {
return null == map ? null : Convert.convert(type, map.get(key)); return null == map ? null : Convert.convert(type, map.get(key));
} }
/** /**
* 获取Map指定key的值并转换为指定类型 * 获取Map指定key的值并转换为指定类型
* *
@@ -881,7 +891,7 @@ public class MapUtil {
public static <T> T get(Map<?, ?> map, Object key, TypeReference<T> type) { public static <T> T get(Map<?, ?> map, Object key, TypeReference<T> type) {
return null == map ? null : Convert.convert(type, map.get(key)); return null == map ? null : Convert.convert(type, map.get(key));
} }
/** /**
* 重命名键<br> * 重命名键<br>
* 实现方式为一处然后重新put当旧的key不存在直接返回<br> * 实现方式为一处然后重新put当旧的key不存在直接返回<br>
@@ -895,8 +905,8 @@ public class MapUtil {
* @since 4.5.16 * @since 4.5.16
*/ */
public static <K, V> Map<K, V> renameKey(Map<K, V> map, K oldKey, K newKey) { public static <K, V> Map<K, V> renameKey(Map<K, V> map, K oldKey, K newKey) {
if(isNotEmpty(map) && map.containsKey(oldKey)) { if (isNotEmpty(map) && map.containsKey(oldKey)) {
if(map.containsKey(newKey)) { if (map.containsKey(newKey)) {
throw new IllegalArgumentException(StrUtil.format("The key '{}' exist !", newKey)); throw new IllegalArgumentException(StrUtil.format("The key '{}' exist !", newKey));
} }
map.put(newKey, map.remove(oldKey)); map.put(newKey, map.remove(oldKey));

View File

@@ -15,7 +15,7 @@ import cn.hutool.json.test.bean.UserA;
import cn.hutool.json.test.bean.UserC; import cn.hutool.json.test.bean.UserC;
public class JSONUtilTest { public class JSONUtilTest {
@Test @Test
public void toJsonStrTest() { public void toJsonStrTest() {
UserA a1 = new UserA(); UserA a1 = new UserA();
@@ -47,7 +47,7 @@ public class JSONUtilTest {
data.put("model2", model); data.put("model2", model);
JSONObject jsonObject = JSONUtil.parseObj(data); JSONObject jsonObject = JSONUtil.parseObj(data);
Assert.assertTrue(jsonObject.containsKey("model")); Assert.assertTrue(jsonObject.containsKey("model"));
Assert.assertEquals(1, jsonObject.getJSONObject("model").getInt("type").intValue()); Assert.assertEquals(1, jsonObject.getJSONObject("model").getInt("type").intValue());
Assert.assertEquals("17610836523", jsonObject.getJSONObject("model").getStr("mobile")); Assert.assertEquals("17610836523", jsonObject.getJSONObject("model").getStr("mobile"));
@@ -94,7 +94,7 @@ public class JSONUtilTest {
JSONObject propJson = JSONUtil.parseObj(prop); JSONObject propJson = JSONUtil.parseObj(prop);
Assert.assertEquals("", propJson.getStr("gender")); Assert.assertEquals("", propJson.getStr("gender"));
Assert.assertEquals(18, propJson.getInt("age").intValue()); Assert.assertEquals(18, propJson.getInt("age").intValue());
// Assert.assertEquals("{\"age\":18,\"gender\":\"男\"}", user.getProp()); // Assert.assertEquals("{\"age\":18,\"gender\":\"男\"}", user.getProp());
} }
@Test @Test
@@ -117,7 +117,7 @@ public class JSONUtilTest {
JSONObject jsonObject = JSONUtil.parseObj(html); JSONObject jsonObject = JSONUtil.parseObj(html);
Assert.assertEquals("Something\\u00a0must\\u00a0have\\u00a0been\\u00a0changed\\u00a0since\\u00a0you\\u00a0leave", jsonObject.getStrEscaped("name")); Assert.assertEquals("Something\\u00a0must\\u00a0have\\u00a0been\\u00a0changed\\u00a0since\\u00a0you\\u00a0leave", jsonObject.getStrEscaped("name"));
} }
@Test @Test
public void parseFromXmlTest() { public void parseFromXmlTest() {
String s = "<sfzh>640102197312070614</sfzh><sfz>640102197312070614X</sfz><name>aa</name><gender>1</gender>"; String s = "<sfzh>640102197312070614</sfzh><sfz>640102197312070614X</sfz><name>aa</name><gender>1</gender>";