mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
deprecate OptionalBean
This commit is contained in:
@@ -3,12 +3,13 @@
|
||||
|
||||
-------------------------------------------------------------------------------------------------------------
|
||||
|
||||
# 5.7.12 (2021-09-01)
|
||||
# 5.7.12 (2021-09-02)
|
||||
|
||||
### 🐣新特性
|
||||
* 【system 】 OshiUtil增加getCurrentProcess方法
|
||||
* 【extra 】 SpringUtil增加getApplicationName、publishEvent方法(issue#I485NZ@Gitee)
|
||||
* 【core 】 BeanUtil.getProperty增加判空(issue#I488HA@Gitee)
|
||||
* 【core 】 OptionalBean弃用(pr#1182@Github)
|
||||
|
||||
### 🐞Bug修复
|
||||
|
||||
|
@@ -15,7 +15,9 @@ import java.util.function.Supplier;
|
||||
* @param <T> Bean类型
|
||||
* @author totalo
|
||||
* @since 5.4.7
|
||||
* @deprecated {@link java.util.Optional} 可以完成此类的所有功能。
|
||||
*/
|
||||
@Deprecated
|
||||
public final class OptionalBean<T> {
|
||||
|
||||
private static final OptionalBean<?> EMPTY = new OptionalBean<>();
|
||||
|
@@ -1,50 +0,0 @@
|
||||
package cn.hutool.core.bean;
|
||||
|
||||
import org.junit.Assert;
|
||||
import lombok.Data;
|
||||
import org.junit.Test;
|
||||
|
||||
public class OptionalBeanTest {
|
||||
|
||||
@Test
|
||||
public void optionalBeanTest() {
|
||||
User user = new User();
|
||||
user.setName("hello");
|
||||
String value1 = OptionalBean.ofNullable(user)
|
||||
.getBean(User::getSchool)
|
||||
.getBean(User.School::getAddress).get();
|
||||
Assert.assertNull(value1);
|
||||
|
||||
boolean present = OptionalBean.ofNullable(user)
|
||||
.getBean(User::getSchool)
|
||||
.getBean(User.School::getAddress).isPresent();
|
||||
Assert.assertFalse(present);
|
||||
|
||||
String value2 = OptionalBean.ofNullable(user)
|
||||
.getBean(User::getSchool)
|
||||
.getBean(User.School::getAddress).orElse("没得地址");
|
||||
Assert.assertEquals(value2, "没得地址");
|
||||
try {
|
||||
OptionalBean.ofNullable(user)
|
||||
.getBean(User::getSchool)
|
||||
.getBean(User.School::getAddress).orElseThrow(() -> new RuntimeException("空指针了"));
|
||||
} catch (Exception e) {
|
||||
Assert.assertEquals(e.getMessage(), "空指针了");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Data
|
||||
public static class User {
|
||||
private String name;
|
||||
private String gender;
|
||||
private School school;
|
||||
|
||||
@Data
|
||||
public static class School {
|
||||
private String name;
|
||||
private String address;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@@ -1,6 +1,5 @@
|
||||
package cn.hutool.json;
|
||||
|
||||
import cn.hutool.core.bean.OptionalBean;
|
||||
import cn.hutool.core.convert.Convert;
|
||||
import cn.hutool.core.convert.ConvertException;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
@@ -10,6 +9,7 @@ import cn.hutool.core.util.StrUtil;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Date;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* 用于JSON的Getter类,提供各种类型的Getter方法
|
||||
@@ -114,25 +114,28 @@ public interface JSONGetter<K> extends OptNullBasicTypeFromObjectGetter<K> {
|
||||
}
|
||||
|
||||
@Override
|
||||
default Date getDate(K key, Date defaultValue){
|
||||
default Date getDate(K key, Date defaultValue) {
|
||||
// 默认转换
|
||||
final Object obj = getObj(key);
|
||||
if (null == obj) {
|
||||
return defaultValue;
|
||||
}
|
||||
if(obj instanceof Date){
|
||||
if (obj instanceof Date) {
|
||||
return (Date) obj;
|
||||
}
|
||||
|
||||
String format = OptionalBean.ofNullable(getConfig()).getBean(JSONConfig::getDateFormat).get();
|
||||
if(StrUtil.isNotBlank(format)){
|
||||
final Optional<String> formatOps = Optional.ofNullable(getConfig()).map(JSONConfig::getDateFormat);
|
||||
if (formatOps.isPresent()) {
|
||||
final String format = formatOps.get();
|
||||
if (StrUtil.isNotBlank(format)) {
|
||||
// 用户指定了日期格式,获取日期属性时使用对应格式
|
||||
final String str = Convert.toStr(obj);
|
||||
if(null == str){
|
||||
if (null == str) {
|
||||
return defaultValue;
|
||||
}
|
||||
return DateUtil.parse(str, format);
|
||||
}
|
||||
}
|
||||
|
||||
return Convert.toDate(obj, defaultValue);
|
||||
}
|
||||
@@ -145,25 +148,28 @@ public interface JSONGetter<K> extends OptNullBasicTypeFromObjectGetter<K> {
|
||||
* @return {@link LocalDateTime}
|
||||
* @since 5.7.7
|
||||
*/
|
||||
default LocalDateTime getLocalDateTime(K key, LocalDateTime defaultValue){
|
||||
default LocalDateTime getLocalDateTime(K key, LocalDateTime defaultValue) {
|
||||
// 默认转换
|
||||
final Object obj = getObj(key);
|
||||
if (null == obj) {
|
||||
return defaultValue;
|
||||
}
|
||||
if(obj instanceof LocalDateTime){
|
||||
if (obj instanceof LocalDateTime) {
|
||||
return (LocalDateTime) obj;
|
||||
}
|
||||
|
||||
String format = OptionalBean.ofNullable(getConfig()).getBean(JSONConfig::getDateFormat).get();
|
||||
if(StrUtil.isNotBlank(format)){
|
||||
final Optional<String> formatOps = Optional.ofNullable(getConfig()).map(JSONConfig::getDateFormat);
|
||||
if (formatOps.isPresent()) {
|
||||
final String format = formatOps.get();
|
||||
if (StrUtil.isNotBlank(format)) {
|
||||
// 用户指定了日期格式,获取日期属性时使用对应格式
|
||||
final String str = Convert.toStr(obj);
|
||||
if(null == str){
|
||||
if (null == str) {
|
||||
return defaultValue;
|
||||
}
|
||||
return LocalDateTimeUtil.parse(str, format);
|
||||
}
|
||||
}
|
||||
|
||||
return Convert.toLocalDateTime(obj, defaultValue);
|
||||
}
|
||||
|
Reference in New Issue
Block a user