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..482de45c1 --- /dev/null +++ b/hutool-core/src/main/java/cn/hutool/core/convert/CastUtil.java @@ -0,0 +1,113 @@ +package cn.hutool.core.convert; + +import java.util.Collection; +import java.util.List; +import java.util.Map; +import java.util.Set; + +/** + * 转换工具类,提供集合、Map等向上向下转换工具 + * + * @author feg545 + * @since 5.8.1 + */ +public class CastUtil { + + /** + * 泛型集合向上转型。例如将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; + } + + /** + * 泛型接口向上转型。例如将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 泛化集合 + */ + @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; + } +} 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..b4b422789 --- /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.ListUtil; +import cn.hutool.core.collection.SetUtil; +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= ListUtil.of(1,2,3); + List list = ListUtil.of(1, 2, 3); + Set set = SetUtil.of(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); + } +}