add methods

This commit is contained in:
Looly 2022-04-06 09:46:44 +08:00
parent 04612516d4
commit 58c7c688ae
5 changed files with 36 additions and 29 deletions

View File

@ -3,10 +3,13 @@
-------------------------------------------------------------------------------------------------------------
# 5.8.0.M3 (2022-04-02)
# 5.8.0.M3 (2022-04-06)
### ❌不兼容特性
### 🐣新特性
* 【core 】 CopyOptions支持以Lambda方式设置忽略属性列表pr#590@Gitee
### 🐞Bug修复
-------------------------------------------------------------------------------------------------------------

View File

@ -7,13 +7,10 @@ import cn.hutool.core.util.ArrayUtil;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.function.BiFunction;
import java.util.function.BiPredicate;
import java.util.stream.Collectors;
/**
* 属性拷贝选项<br>
@ -167,27 +164,13 @@ public class CopyOptions implements Serializable {
/**
* 设置忽略的目标对象中属性列表设置一个属性列表不拷贝这些属性值Lambda方式
*
* @param func1 忽略的目标对象中属性列表设置一个属性列表不拷贝这些属性值
* @param funcs 忽略的目标对象中属性列表设置一个属性列表不拷贝这些属性值
* @return CopyOptions
* @since 5.8.0
*/
public <P, R> CopyOptions setIgnoreProperties(Func1<P, R>... func1) {
List<String> ignoreProperties = Arrays.asList(func1)
.stream()
.map(t -> {
String name = LambdaUtil.getMethodName(t);
if (name.startsWith("is")) {
name = name.substring(2);
} else if (name.startsWith("get") || name.startsWith("set")) {
name = name.substring(3);
} else {
throw new RuntimeException("Error parsing property name '" + name + "'. Didn't start with 'is', 'get' or 'set'.");
}
if (name.length() == 1 || (name.length() > 1 && !Character.isUpperCase(name.charAt(1)))) {
name = name.substring(0, 1).toLowerCase(Locale.ENGLISH) + name.substring(1);
}
return name;
})
.collect(Collectors.toList());
@SuppressWarnings("unchecked")
public <P, R> CopyOptions setIgnoreProperties(Func1<P, R>... funcs) {
final Set<String> ignoreProperties = ArrayUtil.mapToSet(funcs, LambdaUtil::getFieldName);
return setPropertiesFilter((field, o) -> false == ignoreProperties.contains(field.getName()));
}

View File

@ -123,7 +123,9 @@ public class FileTypeUtil {
}
/**
* 根据文件流的头部信息获得文件类型
* 根据文件流的头部信息获得文件类型<br>
* 注意此方法会读取头部28个bytes造成此流接下来读取时缺少部分bytes<br>
* 因此如果想服用此流流需支持{@link InputStream#reset()}方法
*
* @param in {@link InputStream}
* @return 类型文件的扩展名未找到为{@code null}
@ -136,13 +138,16 @@ public class FileTypeUtil {
/**
* 根据文件流的头部信息获得文件类型
* 注意此方法会读取头部28个bytes造成此流接下来读取时缺少部分bytes<br>
* 因此如果想服用此流流需支持{@link InputStream#reset()}方法
*
* <pre>
* 1无法识别类型默认按照扩展名识别
* 2xlsdocmsi头信息无法区分按照扩展名区分
* 3zip可能为docxxlsxpptxjarwarofd头信息无法区分按照扩展名区分
* </pre>
* @param in {@link InputStream}
*
* @param in {@link InputStream}
* @param filename 文件名
* @return 类型文件的扩展名未找到为{@code null}
* @throws IORuntimeException 读取流引起的异常

View File

@ -382,10 +382,10 @@ public class ArrayUtil extends PrimitiveArrayUtil {
*/
@SuppressWarnings({"unchecked"})
public static <T> T[] replace(T[] buffer, int index, T... values) {
if(isEmpty(values)){
if (isEmpty(values)) {
return buffer;
}
if(isEmpty(buffer)){
if (isEmpty(buffer)) {
return values;
}
if (index < 0) {
@ -1673,7 +1673,7 @@ public class ArrayUtil extends PrimitiveArrayUtil {
}
/**
* 按照指定规则将一种类型的数组元素提取后转换为List
* 按照指定规则将一种类型的数组元素提取后转换为{@link List}
*
* @param array 被转换的数组
* @param func 转换规则函数
@ -1686,6 +1686,20 @@ public class ArrayUtil extends PrimitiveArrayUtil {
return Arrays.stream(array).map(func).collect(Collectors.toList());
}
/**
* 按照指定规则将一种类型的数组元素提取后转换为{@link Set}
*
* @param array 被转换的数组
* @param func 转换规则函数
* @param <T> 原数组类型
* @param <R> 目标数组类型
* @return 转换后的数组
* @since 5.8.0
*/
public static <T, R> Set<R> mapToSet(T[] array, Function<? super T, ? extends R> func) {
return Arrays.stream(array).map(func).collect(Collectors.toSet());
}
/**
* 判断两个数组是否相等判断依据包括数组长度和每个元素都相等
*

View File

@ -564,11 +564,13 @@ public class BeanUtilTest {
@Test
public void copyBeanPropertiesFunctionFilterTest() {
//https://gitee.com/dromara/hutool/pulls/590
Person o = new Person();
o.setName("asd");
o.setAge(123);
o.setOpenid("asd");
@SuppressWarnings("unchecked")
CopyOptions copyOptions = CopyOptions.create().setIgnoreProperties(Person::getAge,Person::getOpenid);
Person n = new Person();
BeanUtil.copyProperties(o, n, copyOptions);