add param

This commit is contained in:
Looly
2020-09-17 18:15:28 +08:00
parent a5f757679e
commit 75a6da5b5f
4 changed files with 77 additions and 33 deletions

View File

@@ -19,6 +19,7 @@
* 【core 】 增加Calculatorissue#1090@Github * 【core 】 增加Calculatorissue#1090@Github
* 【core 】 IdcardUtil增加getIdcardInfo方法issue#1092@Github * 【core 】 IdcardUtil增加getIdcardInfo方法issue#1092@Github
* 【core 】 改进ObjectUtil.equal支持BigDecimal判断 * 【core 】 改进ObjectUtil.equal支持BigDecimal判断
* 【core 】 ArrayConverter增加可选是否忽略错误issue#I1VNYQ@Gitee
### Bug修复 ### Bug修复
* 【core 】 修复Dict.of错误issue#I1UUO5@Gitee * 【core 】 修复Dict.of错误issue#I1UUO5@Gitee

View File

@@ -350,11 +350,7 @@ public class ConverterRegistry implements Serializable {
// 数组转换 // 数组转换
if (rowType.isArray()) { if (rowType.isArray()) {
final ArrayConverter arrayConverter = new ArrayConverter(rowType); final ArrayConverter arrayConverter = new ArrayConverter(rowType);
try {
return (T) arrayConverter.convert(value, defaultValue); return (T) arrayConverter.convert(value, defaultValue);
} catch (Exception e) {
// 数组转换失败进行下一步
}
} }
// 表示非需要特殊转换的对象 // 表示非需要特殊转换的对象

View File

@@ -2,7 +2,7 @@ package cn.hutool.core.convert.impl;
import cn.hutool.core.collection.IterUtil; import cn.hutool.core.collection.IterUtil;
import cn.hutool.core.convert.AbstractConverter; import cn.hutool.core.convert.AbstractConverter;
import cn.hutool.core.convert.ConverterRegistry; import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ArrayUtil; import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
@@ -22,28 +22,47 @@ public class ArrayConverter extends AbstractConverter<Object> {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private final Class<?> targetType; private final Class<?> targetType;
/** 目标元素类型 */ /**
* 目标元素类型
*/
private final Class<?> targetComponentType; private final Class<?> targetComponentType;
/**
* 是否忽略元素转换错误
*/
private boolean ignoreElementError;
/** /**
* 构造 * 构造
* *
* @param targetType 目标数组类型 * @param targetType 目标数组类型
*/ */
public ArrayConverter(Class<?> targetType) { public ArrayConverter(Class<?> targetType) {
this(targetType, false);
}
/**
* 构造
*
* @param targetType 目标数组类型
* @param ignoreElementError 是否忽略元素转换错误
*/
public ArrayConverter(Class<?> targetType, boolean ignoreElementError) {
if (null == targetType) { if (null == targetType) {
// 默认Object数组 // 默认Object数组
targetType = Object[].class; targetType = Object[].class;
} }
if(targetType.isArray()) { if (targetType.isArray()) {
this.targetType = targetType; this.targetType = targetType;
this.targetComponentType = targetType.getComponentType(); this.targetComponentType = targetType.getComponentType();
}else { } else {
//用户传入类为非数组时,按照数组元素类型对待 //用户传入类为非数组时,按照数组元素类型对待
this.targetComponentType = targetType; this.targetComponentType = targetType;
this.targetType = ArrayUtil.getArrayType(targetType); this.targetType = ArrayUtil.getArrayType(targetType);
} }
this.ignoreElementError = ignoreElementError;
} }
@Override @Override
@@ -51,13 +70,24 @@ public class ArrayConverter extends AbstractConverter<Object> {
return value.getClass().isArray() ? convertArrayToArray(value) : convertObjectToArray(value); return value.getClass().isArray() ? convertArrayToArray(value) : convertObjectToArray(value);
} }
@SuppressWarnings({ "unchecked", "rawtypes" }) @SuppressWarnings({"unchecked", "rawtypes"})
@Override @Override
public Class getTargetType() { public Class getTargetType() {
return this.targetType; return this.targetType;
} }
/**
* 设置是否忽略元素转换错误
*
* @param ignoreElementError 是否忽略元素转换错误
* @since 5.4.3
*/
public void setIgnoreElementError(boolean ignoreElementError) {
this.ignoreElementError = ignoreElementError;
}
// -------------------------------------------------------------------------------------- Private method start // -------------------------------------------------------------------------------------- Private method start
/** /**
* 数组对数组转换 * 数组对数组转换
* *
@@ -74,9 +104,8 @@ public class ArrayConverter extends AbstractConverter<Object> {
final int len = ArrayUtil.length(array); final int len = ArrayUtil.length(array);
final Object result = Array.newInstance(targetComponentType, len); final Object result = Array.newInstance(targetComponentType, len);
final ConverterRegistry converter = ConverterRegistry.getInstance();
for (int i = 0; i < len; i++) { for (int i = 0; i < len; i++) {
Array.set(result, i, converter.convert(targetComponentType, Array.get(array, i))); Array.set(result, i, convertComponentType(Array.get(array, i)));
} }
return result; return result;
} }
@@ -98,14 +127,13 @@ public class ArrayConverter extends AbstractConverter<Object> {
return convertArrayToArray(strings); return convertArrayToArray(strings);
} }
final ConverterRegistry converter = ConverterRegistry.getInstance();
Object result; Object result;
if (value instanceof List) { if (value instanceof List) {
// List转数组 // List转数组
final List<?> list = (List<?>) value; final List<?> list = (List<?>) value;
result = Array.newInstance(targetComponentType, list.size()); result = Array.newInstance(targetComponentType, list.size());
for (int i = 0; i < list.size(); i++) { for (int i = 0; i < list.size(); i++) {
Array.set(result, i, converter.convert(targetComponentType, list.get(i))); Array.set(result, i, convertComponentType(list.get(i)));
} }
} else if (value instanceof Collection) { } else if (value instanceof Collection) {
// 集合转数组 // 集合转数组
@@ -114,7 +142,7 @@ public class ArrayConverter extends AbstractConverter<Object> {
int i = 0; int i = 0;
for (Object element : collection) { for (Object element : collection) {
Array.set(result, i, converter.convert(targetComponentType, element)); Array.set(result, i, convertComponentType(element));
i++; i++;
} }
} else if (value instanceof Iterable) { } else if (value instanceof Iterable) {
@@ -122,16 +150,16 @@ public class ArrayConverter extends AbstractConverter<Object> {
final List<?> list = IterUtil.toList((Iterable<?>) value); final List<?> list = IterUtil.toList((Iterable<?>) value);
result = Array.newInstance(targetComponentType, list.size()); result = Array.newInstance(targetComponentType, list.size());
for (int i = 0; i < list.size(); i++) { for (int i = 0; i < list.size(); i++) {
Array.set(result, i, converter.convert(targetComponentType, list.get(i))); Array.set(result, i, convertComponentType(list.get(i)));
} }
} else if (value instanceof Iterator) { } else if (value instanceof Iterator) {
// 可循环对象转数组可循环对象无法获取长度因此先转为List后转为数组 // 可循环对象转数组可循环对象无法获取长度因此先转为List后转为数组
final List<?> list = IterUtil.toList((Iterator<?>) value); final List<?> list = IterUtil.toList((Iterator<?>) value);
result = Array.newInstance(targetComponentType, list.size()); result = Array.newInstance(targetComponentType, list.size());
for (int i = 0; i < list.size(); i++) { for (int i = 0; i < list.size(); i++) {
Array.set(result, i, converter.convert(targetComponentType, list.get(i))); Array.set(result, i, convertComponentType(list.get(i)));
} }
}else if (value instanceof Serializable && byte.class == targetComponentType) { } else if (value instanceof Serializable && byte.class == targetComponentType) {
// 用户可能想序列化指定对象 // 用户可能想序列化指定对象
result = ObjectUtil.serialize(value); result = ObjectUtil.serialize(value);
} else { } else {
@@ -150,8 +178,19 @@ public class ArrayConverter extends AbstractConverter<Object> {
*/ */
private Object[] convertToSingleElementArray(Object value) { private Object[] convertToSingleElementArray(Object value) {
final Object[] singleElementArray = ArrayUtil.newArray(targetComponentType, 1); final Object[] singleElementArray = ArrayUtil.newArray(targetComponentType, 1);
singleElementArray[0] = ConverterRegistry.getInstance().convert(targetComponentType, value); singleElementArray[0] = convertComponentType(value);
return singleElementArray; return singleElementArray;
} }
/**
* 转换元素类型
*
* @param value 值
* @return 转换后的值,转换失败若{@link #ignoreElementError}为true返回null否则抛出异常
* @since 5.4.3
*/
private Object convertComponentType(Object value) {
return Convert.convertWithCheck(this.targetComponentType, value, null, this.ignoreElementError);
}
// -------------------------------------------------------------------------------------- Private method end // -------------------------------------------------------------------------------------- Private method end
} }

View File

@@ -1,16 +1,15 @@
package cn.hutool.core.convert; package cn.hutool.core.convert;
import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import cn.hutool.core.convert.impl.ArrayConverter; import cn.hutool.core.convert.impl.ArrayConverter;
import cn.hutool.core.io.FileUtil; import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Console; import cn.hutool.core.lang.Console;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import java.io.File;
import java.net.URL;
import java.util.ArrayList;
/** /**
* 类型转换工具单元测试<br> * 类型转换工具单元测试<br>
@@ -36,6 +35,15 @@ public class ConvertToArrayTest {
Assert.assertArrayEquals(intArray2, new Integer[]{1,2,3,4,5}); Assert.assertArrayEquals(intArray2, new Integer[]{1,2,3,4,5});
} }
@Test
public void toIntArrayTestIgnoreComponentErrorTest() {
String[] b = { "a", "1" };
final ArrayConverter arrayConverter = new ArrayConverter(Integer[].class, true);
Integer[] integerArray = (Integer[]) arrayConverter.convert(b, null);
Assert.assertArrayEquals(integerArray, new Integer[]{null, 1});
}
@Test @Test
public void toLongArrayTest() { public void toLongArrayTest() {
String[] b = { "1", "2", "3", "4" }; String[] b = { "1", "2", "3", "4" };