add class

This commit is contained in:
Looly 2022-05-12 01:30:51 +08:00
parent 9835d1bd80
commit 5c31ecf526
2 changed files with 158 additions and 0 deletions

View File

@ -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 <T> 元素类型
* @return 转换后的集合
*/
@SuppressWarnings("unchecked")
public static <T> Collection<T> castUp(Collection<? extends T> collection) {
return (Collection<T>) collection;
}
/**
* 泛型集合向下转型例如将Collection&lt;Number&gt;转换为Collection&lt;Integer&gt;
*
* @param collection 集合
* @param <T> 元素类型
* @return 转换后的集合
*/
@SuppressWarnings("unchecked")
public static <T> Collection<T> castDown(Collection<? super T> collection) {
return (Collection<T>) collection;
}
/**
* 泛型集合向上转型例如将Set&lt;Integer&gt;转换为Set&lt;Number&gt;
*
* @param set 集合
* @param <T> 泛型
* @return 泛化集合
*/
@SuppressWarnings("unchecked")
public static <T> Set<T> castUp(Set<? extends T> set) {
return (Set<T>) set;
}
/**
* 泛型集合向下转型例如将Set&lt;Number&gt;转换为Set&lt;Integer&gt;
*
* @param set 集合
* @param <T> 泛型子类
* @return 泛化集合
*/
@SuppressWarnings("unchecked")
public static <T> Set<T> castDown(Set<? super T> set) {
return (Set<T>) set;
}
/**
* 泛型接口向上转型例如将List&lt;Integer&gt;转换为List&lt;Number&gt;
*
* @param list 集合
* @param <T> 泛型的父类
* @return 泛化集合
*/
@SuppressWarnings("unchecked")
public static <T> List<T> castUp(List<? extends T> list) {
return (List<T>) list;
}
/**
* 泛型集合向下转型例如将List&lt;Number&gt;转换为List&lt;Integer&gt;
*
* @param list 集合
* @param <T> 泛型的子类
* @return 泛化集合
*/
@SuppressWarnings("unchecked")
public static <T> List<T> castDown(List<? super T> list) {
return (List<T>) list;
}
/**
* 泛型集合向下转型例如将Map&lt;Integer, Integer&gt;转换为Map&lt;Number,Number&gt;
*
* @param map 集合
* @param <K> 泛型父类
* @param <V> 泛型父类
* @return 泛化集合
*/
@SuppressWarnings("unchecked")
public static <K, V> Map<K, V> castUp(Map<? extends K, ? extends V> map) {
return (Map<K, V>) map;
}
/**
* 泛型集合向下转型例如将Map&lt;Number,Number&gt;转换为Map&lt;Integer, Integer&gt;
*
* @param map 集合
* @param <K> 泛型子类
* @param <V> 泛型子类
* @return 泛化集合
*/
@SuppressWarnings("unchecked")
public static <K, V> Map<K, V> castDown(Map<? super K, ? super V> map) {
return (Map<K, V>) map;
}
}

View File

@ -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<Integer> collection= ListUtil.of(1,2,3);
List<Integer> list = ListUtil.of(1, 2, 3);
Set<Integer> set = SetUtil.of(1, 2, 3);
Map<Integer, Integer> map = new HashMap<>();
map.put(1, 1);
Collection<Number> collection2 = CastUtil.castUp(collection);
Assert.assertSame(collection, collection2);
Collection<Integer> collection3 = CastUtil.castDown(collection2);
Assert.assertSame(collection2, collection3);
List<Number> list2 = CastUtil.castUp(list);
Assert.assertSame(list, list2);
List<Integer> list3 = CastUtil.castDown(list2);
Assert.assertSame(list2, list3);
Set<Number> set2 = CastUtil.castUp(set);
Assert.assertSame(set, set2);
Set<Integer> set3 = CastUtil.castDown(set2);
Assert.assertSame(set2, set3);
Map<Number, Serializable> map2 = CastUtil.castUp(map);
Assert.assertSame(map, map2);
Map<Integer, Number> map3 = CastUtil.castDown(map2);
Assert.assertSame(map2, map3);
}
}