add methods

This commit is contained in:
Looly 2022-04-28 17:30:55 +08:00
parent 2d2819ad28
commit 2881062f33
2 changed files with 12 additions and 26 deletions

View File

@ -7,9 +7,11 @@
### ❌不兼容特性
* 【extra 】 升级jakarta.validation-api到3.x包名变更导致不能向下兼容
* 【core 】 BeanUtil删除了beanToMap(Object)方法,因为有可变参数的方法,这个删除可能导致直接升级找不到方法,重新编译项目即可。
### 🐣新特性
* 【core 】 Singleton增加部分方法pr#609@Gitee
* 【core 】 BeanUtil增加beanToMap重载pr#2292@Github
### 🐞Bug修复
* 【db 】 修复RedisDS无法设置maxWaitMillis问题issue#I54TZ9@Gitee

View File

@ -24,8 +24,6 @@ import java.beans.PropertyEditorManager;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
@ -608,38 +606,24 @@ public class BeanUtil {
}
// --------------------------------------------------------------------------------------------- beanToMap
/**
* 对象转Map不进行驼峰转下划线不忽略值为空的字段
*
* @param bean bean对象
* @return Map
*/
public static Map<String, Object> beanToMap(Object bean) {
return beanToMap(bean, false, false);
}
/**
* 将bean的部分属性转换成map
* 将bean的部分属性转换成map<br>
* 可选拷贝哪些属性值默认是不忽略值为{@code null}的值的
*
* @param bean bean
* @param properties 属性值
* @param properties 需要拷贝的属性值{@code null}或空表示拷贝所有值
* @return Map
* @since 5.8.0
*/
public static Map<String, Object> beanToMap(Object bean, String... properties) {
if (ArrayUtil.isEmpty(properties)) {
return Collections.emptyMap();
Editor<String> keyEditor = null;
if(ArrayUtil.isNotEmpty(properties)){
final Set<String> propertiesSet = CollUtil.set(false, properties);
keyEditor = property -> propertiesSet.contains(property) ? property : null;
}
Set<String> propertiesSet = Arrays.stream(properties).collect(Collectors.toSet());
// 指明了要复制的属性 所以不忽略null值
return beanToMap(bean, new HashMap<>(properties.length), false,
property -> {
if (!propertiesSet.contains(property)) {
return null;
}
return property;
});
return beanToMap(bean, new LinkedHashMap<>(properties.length, 1), false, keyEditor);
}
/**