From 541ab9ed5535d2b36daed8f57aefd6a72285016d Mon Sep 17 00:00:00 2001 From: Looly Date: Thu, 12 May 2022 01:21:44 +0800 Subject: [PATCH] add CastUtil --- CHANGELOG.md | 1 + .../cn/hutool/core/collection/CollUtil.java | 92 +------------- .../java/cn/hutool/core/convert/CastUtil.java | 118 ++++++++++++++++++ .../hutool/core/collection/CollUtilTest.java | 31 ----- .../cn/hutool/core/convert/CastUtilTest.java | 45 +++++++ 5 files changed, 165 insertions(+), 122 deletions(-) create mode 100644 hutool-core/src/main/java/cn/hutool/core/convert/CastUtil.java create mode 100644 hutool-core/src/test/java/cn/hutool/core/convert/CastUtilTest.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 9ded6245a..42bf8f963 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ * 【core 】 BooleanUtil增加toBooleanObject方法(issue#I56AG3@Gitee) * 【core 】 CharSequenceUtil增加startWithAnyIgnoreCase方法(issue#2312@Github) * 【system 】 JavaInfo增加版本(issue#2310@Github) +* 【core 】 新增CastUtil(pr#2313@Github) * ### 🐞Bug修复 * 【core 】 MapUtil.map对null友好,且修复了测试用例中分组问题(pr#614@Gitee) diff --git a/hutool-core/src/main/java/cn/hutool/core/collection/CollUtil.java b/hutool-core/src/main/java/cn/hutool/core/collection/CollUtil.java index 3a80610da..a764a6c4e 100755 --- a/hutool-core/src/main/java/cn/hutool/core/collection/CollUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/collection/CollUtil.java @@ -1058,7 +1058,7 @@ public class CollUtil { * @param 集合元素类型 * @param 唯一键类型 * @param collection 集合 - * @param override 是否覆盖模式,如果为{@code true},加入的新值会覆盖相同key的旧值,否则会忽略新加值 + * @param override 是否覆盖模式,如果为{@code true},加入的新值会覆盖相同key的旧值,否则会忽略新加值 * @return {@link ArrayList} * @since 5.8.0 */ @@ -3056,94 +3056,4 @@ public class CollUtil { return IterUtil.isEqualList(list1, list2); } - - /** - * 泛型集合向上转型。例如将Collection<Integer>转换为Collection<Number> - * @param collection 集合 - * @param 泛型 - * @return 泛化集合 - */ - @SuppressWarnings("unchecked") - public static Collection castup(Collection collection){ - return (Collection) collection; - } - - /** - * 泛型集合向下转型。例如将Collection<Number>转换为Collection<Integer> - * @param collection - * @param - * @return - */ - @SuppressWarnings("unchecked") - public static Collection castdown(Collection collection){ - return (Collection) collection; - } - - /** - * 泛型集合向上转型。例如将Set<Integer>转换为Set<Number> - * @param set 集合 - * @param 泛型 - * @return 泛化集合 - */ - @SuppressWarnings("unchecked") - public static Set castup(Set set){ - return (Set) set; - } - - /** - * 泛型集合向下转型。例如将Set<Number>转换为Set<Integer> - * @param set 集合 - * @param 泛型子类 - * @return 泛化集合 - */ - @SuppressWarnings("unchecked") - public static Set castdown(Set set){ - return (Set) set; - } - - /** - * 泛型集合向下转型。例如将Map<Integer, Integer>转换为Map<Number,Number> - * @param map 集合 - * @param 泛型父类 - * @param 泛型父类 - * @return 泛化集合 - */ - @SuppressWarnings("unchecked") - public static Map castup(Map map){ - return (Map) map; - } - - /** - * 泛型集合向下转型。例如将Map<Number,Number>转换为Map<Integer, Integer> - * @param map 集合 - * @param 泛型子类 - * @param 泛型子类 - * @return 泛化集合 - */ - @SuppressWarnings("unchecked") - public static Map castdown(Map map){ - return (Map) map; - } - - /** - * 泛型接口向上转型。例如将List<Integer>转换为List<Number> - * @param list 集合 - * @param 泛型的父类 - * @return 泛化集合 - */ - @SuppressWarnings("unchecked") - public static List castup(List list){ - return (List) list; - } - - /** - * 泛型集合向下转型。例如将List<Number>转换为List<Integer> - * @param list 集合 - * @param 泛型的子类 - * @return 泛化集合 - */ - @SuppressWarnings("unchecked") - public static List castdown(List list){ - return (List) list; - } } diff --git a/hutool-core/src/main/java/cn/hutool/core/convert/CastUtil.java b/hutool-core/src/main/java/cn/hutool/core/convert/CastUtil.java new file mode 100644 index 000000000..5279bb7e5 --- /dev/null +++ b/hutool-core/src/main/java/cn/hutool/core/convert/CastUtil.java @@ -0,0 +1,118 @@ +package cn.hutool.core.convert; + +import java.util.Collection; +import java.util.List; +import java.util.Map; +import java.util.Set; + +/** + * 转换工具类,提供集合、Map等向上向下转换工具 + * + * @author looly + * @since 5.8.1 + */ +public class CastUtil { + /** + * 泛型集合向上转型。例如将Collection<Integer>转换为Collection<Number> + * + * @param collection 集合 + * @param 元素类型 + * @return 转换后的集合 + * @since 5.8.1 + */ + @SuppressWarnings("unchecked") + public static Collection castUp(Collection collection) { + return (Collection) collection; + } + + /** + * 泛型集合向下转型。例如将Collection<Number>转换为Collection<Integer> + * + * @param collection 集合 + * @param 元素类型 + * @return 转换后的集合 + * @since 5.8.1 + */ + @SuppressWarnings("unchecked") + public static Collection castDown(Collection collection) { + return (Collection) collection; + } + + /** + * 泛型集合向上转型。例如将Set<Integer>转换为Set<Number> + * + * @param set 集合 + * @param 泛型 + * @return 泛化集合 + * @since 5.8.1 + */ + @SuppressWarnings("unchecked") + public static Set castUp(Set set) { + return (Set) set; + } + + /** + * 泛型集合向下转型。例如将Set<Number>转换为Set<Integer> + * + * @param set 集合 + * @param 泛型子类 + * @return 泛化集合 + * @since 5.8.1 + */ + @SuppressWarnings("unchecked") + public static Set castDown(Set set) { + return (Set) set; + } + + /** + * 泛型接口向上转型。例如将List<Integer>转换为List<Number> + * + * @param list 集合 + * @param 泛型的父类 + * @return 泛化集合 + */ + @SuppressWarnings("unchecked") + public static List castUp(List list) { + return (List) list; + } + + /** + * 泛型集合向下转型。例如将List<Number>转换为List<Integer> + * + * @param list 集合 + * @param 泛型的子类 + * @return 泛化集合 + */ + @SuppressWarnings("unchecked") + public static List castDown(List list) { + return (List) list; + } + + /** + * 泛型集合向下转型。例如将Map<Integer, Integer>转换为Map<Number,Number> + * + * @param map 集合 + * @param 泛型父类 + * @param 泛型父类 + * @return 泛化集合 + * @since 5.8.1 + */ + @SuppressWarnings("unchecked") + public static Map castUp(Map map) { + return (Map) map; + } + + /** + * 泛型集合向下转型。例如将Map<Number,Number>转换为Map<Integer, Integer> + * + * @param map 集合 + * @param 泛型子类 + * @param 泛型子类 + * @return 泛化集合 + * @since 5.8.1 + */ + @SuppressWarnings("unchecked") + public static Map castDown(Map map) { + return (Map) map; + } +} diff --git a/hutool-core/src/test/java/cn/hutool/core/collection/CollUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/collection/CollUtilTest.java index 50d32c86a..512b13e2d 100755 --- a/hutool-core/src/test/java/cn/hutool/core/collection/CollUtilTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/collection/CollUtilTest.java @@ -10,7 +10,6 @@ import lombok.Data; import org.junit.Assert; import org.junit.Test; -import java.io.Serializable; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -909,36 +908,6 @@ public class CollUtilTest { Assert.assertEquals("bb", distinct.get(1).getName()); } - @Test - public void testCastToSuper() { - Collection collection=CollUtil.newLinkedList(1,2,3); - List list = CollUtil.newArrayList(1, 2, 3); - Set set = CollUtil.newHashSet(1, 2, 3); - Map map = new HashMap<>(); - map.put(1, 1); - - Collection collection2 = CollUtil.castup(collection); - Assert.assertSame(collection, collection2); - - Collection collection3 = CollUtil.castdown(collection2); - Assert.assertSame(collection2, collection3); - - List list2 = CollUtil.castup(list); - Assert.assertSame(list, list2); - List list3 = CollUtil.castdown(list2); - Assert.assertSame(list2, list3); - - Set set2 = CollUtil.castup(set); - Assert.assertSame(set, set2); - Set set3 = CollUtil.castdown(set2); - Assert.assertSame(set2, set3); - - Map map2 = CollUtil.castup(map); - Assert.assertSame(map, map2); - Map map3 = CollUtil.castdown(map2); - Assert.assertSame(map2, map3); - } - @Data @AllArgsConstructor static class Person { diff --git a/hutool-core/src/test/java/cn/hutool/core/convert/CastUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/convert/CastUtilTest.java new file mode 100644 index 000000000..16e9e01ee --- /dev/null +++ b/hutool-core/src/test/java/cn/hutool/core/convert/CastUtilTest.java @@ -0,0 +1,45 @@ +package cn.hutool.core.convert; + +import cn.hutool.core.collection.CollUtil; +import org.junit.Assert; +import org.junit.Test; + +import java.io.Serializable; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; + +public class CastUtilTest { + + @Test + public void testCastToSuper() { + Collection collection= CollUtil.newLinkedList(1,2,3); + List list = CollUtil.newArrayList(1, 2, 3); + Set set = CollUtil.newHashSet(1, 2, 3); + Map map = new HashMap<>(); + map.put(1, 1); + + Collection collection2 = CastUtil.castUp(collection); + Assert.assertSame(collection, collection2); + + Collection collection3 = CastUtil.castDown(collection2); + Assert.assertSame(collection2, collection3); + + List list2 = CastUtil.castUp(list); + Assert.assertSame(list, list2); + List list3 = CastUtil.castDown(list2); + Assert.assertSame(list2, list3); + + Set set2 = CastUtil.castUp(set); + Assert.assertSame(set, set2); + Set set3 = CastUtil.castDown(set2); + Assert.assertSame(set2, set3); + + Map map2 = CastUtil.castUp(map); + Assert.assertSame(map, map2); + Map map3 = CastUtil.castDown(map2); + Assert.assertSame(map2, map3); + } +}