mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
fix code
This commit is contained in:
parent
ef10457954
commit
05b20382c0
@ -10,10 +10,10 @@ import cn.hutool.core.comparator.PropertyComparator;
|
|||||||
import cn.hutool.core.convert.CompositeConverter;
|
import cn.hutool.core.convert.CompositeConverter;
|
||||||
import cn.hutool.core.convert.Convert;
|
import cn.hutool.core.convert.Convert;
|
||||||
import cn.hutool.core.exceptions.UtilException;
|
import cn.hutool.core.exceptions.UtilException;
|
||||||
|
import cn.hutool.core.lang.Assert;
|
||||||
import cn.hutool.core.lang.func.SerBiConsumer;
|
import cn.hutool.core.lang.func.SerBiConsumer;
|
||||||
import cn.hutool.core.lang.func.SerConsumer3;
|
import cn.hutool.core.lang.func.SerConsumer3;
|
||||||
import cn.hutool.core.map.MapUtil;
|
import cn.hutool.core.map.MapUtil;
|
||||||
import cn.hutool.core.reflect.ClassUtil;
|
|
||||||
import cn.hutool.core.reflect.ConstructorUtil;
|
import cn.hutool.core.reflect.ConstructorUtil;
|
||||||
import cn.hutool.core.reflect.FieldUtil;
|
import cn.hutool.core.reflect.FieldUtil;
|
||||||
import cn.hutool.core.reflect.TypeUtil;
|
import cn.hutool.core.reflect.TypeUtil;
|
||||||
@ -32,7 +32,6 @@ import java.util.Comparator;
|
|||||||
import java.util.Deque;
|
import java.util.Deque;
|
||||||
import java.util.EnumSet;
|
import java.util.EnumSet;
|
||||||
import java.util.Enumeration;
|
import java.util.Enumeration;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
@ -714,15 +713,28 @@ public class CollUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建新的集合对象
|
* 创建新的集合对象,返回具体的泛型集合
|
||||||
*
|
*
|
||||||
* @param <T> 集合类型
|
* @param <T> 集合元素类型,rawtype 如 ArrayList.class, EnumSet.class ...
|
||||||
* @param collectionType 集合类型
|
* @param collectionType 集合类型
|
||||||
* @return 集合类型对应的实例
|
* @return 集合类型对应的实例
|
||||||
* @since 3.0.8
|
* @since 3.0.8
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
|
||||||
public static <T> Collection<T> create(final Class<?> collectionType) {
|
public static <T> Collection<T> create(final Class<?> collectionType) {
|
||||||
|
return create(collectionType, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建新的集合对象,返回具体的泛型集合
|
||||||
|
*
|
||||||
|
* @param <T> 集合元素类型,rawtype 如 ArrayList.class, EnumSet.class ...
|
||||||
|
* @param collectionType 集合类型
|
||||||
|
* @param elementType 集合元素类,只用于EnumSet创建,如果创建EnumSet,则此参数必须非空
|
||||||
|
* @return 集合类型对应的实例
|
||||||
|
* @since 3.0.8
|
||||||
|
*/
|
||||||
|
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||||
|
public static <T> Collection<T> create(final Class<?> collectionType, final Class<T> elementType) {
|
||||||
final Collection<T> list;
|
final Collection<T> list;
|
||||||
if (collectionType.isAssignableFrom(AbstractCollection.class)) {
|
if (collectionType.isAssignableFrom(AbstractCollection.class)) {
|
||||||
// 抽象集合默认使用ArrayList
|
// 抽象集合默认使用ArrayList
|
||||||
@ -743,7 +755,7 @@ public class CollUtil {
|
|||||||
return CompareUtil.compare(o1.toString(), o2.toString());
|
return CompareUtil.compare(o1.toString(), o2.toString());
|
||||||
});
|
});
|
||||||
} else if (collectionType.isAssignableFrom(EnumSet.class)) {
|
} else if (collectionType.isAssignableFrom(EnumSet.class)) {
|
||||||
list = (Collection<T>) EnumSet.noneOf((Class<Enum>) ClassUtil.getTypeArgument(collectionType));
|
list = (Collection<T>) EnumSet.noneOf((Class<Enum>) Assert.notNull(elementType));
|
||||||
}
|
}
|
||||||
|
|
||||||
// List
|
// List
|
||||||
@ -1313,6 +1325,7 @@ public class CollUtil {
|
|||||||
index++;
|
index++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return Convert.convert(int[].class, indexList);
|
return Convert.convert(int[].class, indexList);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1386,34 +1399,6 @@ public class CollUtil {
|
|||||||
return map;
|
return map;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 将数组转换为Map(HashMap),支持数组元素类型为:
|
|
||||||
*
|
|
||||||
* <pre>
|
|
||||||
* Map.Entry
|
|
||||||
* 长度大于1的数组(取前两个值),如果不满足跳过此元素
|
|
||||||
* Iterable 长度也必须大于1(取前两个值),如果不满足跳过此元素
|
|
||||||
* Iterator 长度也必须大于1(取前两个值),如果不满足跳过此元素
|
|
||||||
* </pre>
|
|
||||||
*
|
|
||||||
* <pre>
|
|
||||||
* Map<Object, Object> colorMap = CollectionUtil.toMap(new String[][] {{
|
|
||||||
* {"RED", "#FF0000"},
|
|
||||||
* {"GREEN", "#00FF00"},
|
|
||||||
* {"BLUE", "#0000FF"}});
|
|
||||||
* </pre>
|
|
||||||
* <p>
|
|
||||||
* 参考:commons-lang
|
|
||||||
*
|
|
||||||
* @param array 数组。元素类型为Map.Entry、数组、Iterable、Iterator
|
|
||||||
* @return {@link HashMap}
|
|
||||||
* @see MapUtil#of(Object[])
|
|
||||||
* @since 3.0.8
|
|
||||||
*/
|
|
||||||
public static HashMap<Object, Object> toMap(final Object[] array) {
|
|
||||||
return MapUtil.of(array);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将集合转换为排序后的TreeSet
|
* 将集合转换为排序后的TreeSet
|
||||||
*
|
*
|
||||||
@ -1441,32 +1426,6 @@ public class CollUtil {
|
|||||||
return new IteratorEnumeration<>(Objects.requireNonNull(iter));
|
return new IteratorEnumeration<>(Objects.requireNonNull(iter));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Enumeration转换为Iterator
|
|
||||||
* <p>
|
|
||||||
* Adapt the specified {@code Enumeration} to the {@code Iterator} interface
|
|
||||||
*
|
|
||||||
* @param <E> 集合元素类型
|
|
||||||
* @param e {@link Enumeration}
|
|
||||||
* @return {@link Iterator}
|
|
||||||
* @see IterUtil#asIterator(Enumeration)
|
|
||||||
*/
|
|
||||||
public static <E> Iterator<E> asIterator(final Enumeration<E> e) {
|
|
||||||
return IterUtil.asIterator(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@link Iterator} 转为 {@link Iterable}, 但是仅可使用一次
|
|
||||||
*
|
|
||||||
* @param <E> 元素类型
|
|
||||||
* @param iter {@link Iterator}
|
|
||||||
* @return {@link Iterable}
|
|
||||||
* @see IterUtil#asIterable(Iterator)
|
|
||||||
*/
|
|
||||||
public static <E> Iterable<E> asIterable(final Iterator<E> iter) {
|
|
||||||
return IterUtil.asIterable(iter);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@link Iterable}转为{@link Collection}<br>
|
* {@link Iterable}转为{@link Collection}<br>
|
||||||
* 首先尝试强转,强转失败则构建一个新的{@link ArrayList}
|
* 首先尝试强转,强转失败则构建一个新的{@link ArrayList}
|
||||||
|
@ -16,6 +16,9 @@ import java.util.Collection;
|
|||||||
*/
|
*/
|
||||||
public class CollectionConverter implements Converter {
|
public class CollectionConverter implements Converter {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 单例实体
|
||||||
|
*/
|
||||||
public static CollectionConverter INSTANCE = new CollectionConverter();
|
public static CollectionConverter INSTANCE = new CollectionConverter();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -36,7 +39,8 @@ public class CollectionConverter implements Converter {
|
|||||||
* @return 转换后的集合对象
|
* @return 转换后的集合对象
|
||||||
*/
|
*/
|
||||||
public Collection<?> convert(final Type collectionType, final Type elementType, final Object value) {
|
public Collection<?> convert(final Type collectionType, final Type elementType, final Object value) {
|
||||||
final Collection<Object> collection = CollUtil.create(TypeUtil.getClass(collectionType));
|
// pr#2684,兼容EnumSet创建
|
||||||
|
final Collection<?> collection = CollUtil.create(TypeUtil.getClass(collectionType), TypeUtil.getClass(elementType));
|
||||||
return CollUtil.addAll(collection, value, elementType);
|
return CollUtil.addAll(collection, value, elementType);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3043,6 +3043,21 @@ public class FileUtil extends PathUtil {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取指定文件的父路径
|
||||||
|
*
|
||||||
|
* <pre>
|
||||||
|
* getParent(file("d:/aaa/bbb/cc/ddd")) -》 "d:/aaa/bbb/cc"
|
||||||
|
* </pre>
|
||||||
|
*
|
||||||
|
* @param file 目录或文件
|
||||||
|
* @return 路径File,如果不存在返回null
|
||||||
|
* @since 6.0.0
|
||||||
|
*/
|
||||||
|
public static File getParent(final File file) {
|
||||||
|
return getParent(file, 1);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取指定层级的父路径
|
* 获取指定层级的父路径
|
||||||
*
|
*
|
||||||
|
42
hutool-core/src/test/java/cn/hutool/core/bean/Issue2683Test.java
Executable file
42
hutool-core/src/test/java/cn/hutool/core/bean/Issue2683Test.java
Executable file
@ -0,0 +1,42 @@
|
|||||||
|
package cn.hutool.core.bean;
|
||||||
|
|
||||||
|
import cn.hutool.core.collection.CollUtil;
|
||||||
|
import lombok.Data;
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.EnumSet;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* EnumSet创建时无法自动获取其元素类型,通过传入方式获取
|
||||||
|
*/
|
||||||
|
public class Issue2683Test {
|
||||||
|
|
||||||
|
enum Version {
|
||||||
|
dev,
|
||||||
|
prod
|
||||||
|
}
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public static class Vto {
|
||||||
|
EnumSet<Version> versions;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void beanWithEnumSetTest() {
|
||||||
|
final Vto v1 = new Vto();
|
||||||
|
v1.setVersions(EnumSet.allOf(Version.class));
|
||||||
|
final Vto v2 = BeanUtil.copyProperties(v1, Vto.class);
|
||||||
|
Assert.assertNotNull(v2);
|
||||||
|
Assert.assertNotNull(v2.getVersions());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void enumSetTest() {
|
||||||
|
final Collection<Version> objects = CollUtil.create(EnumSet.class, Version.class);
|
||||||
|
Assert.assertNotNull(objects);
|
||||||
|
Assert.assertTrue(EnumSet.class.isAssignableFrom(objects.getClass()));
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user