mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
!199 ObjectUtil返回对象增加自定义处理后返回,如果不存在则返回用户定义类型的默认对象.
Merge pull request !199 from Min/v5-dev
This commit is contained in:
commit
30695b906b
@ -14,7 +14,12 @@ import java.io.ObjectOutputStream;
|
||||
import java.io.Serializable;
|
||||
import java.lang.reflect.Array;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.*;
|
||||
import java.util.Collection;
|
||||
import java.util.Enumeration;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* 对象工具类,包括判空、克隆、序列化等操作
|
||||
@ -297,6 +302,37 @@ public class ObjectUtil {
|
||||
return (null != object) ? object : defaultValue;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 如果给定对象为{@code null} 返回默认值, 如果不为null 返回自定义handle处理后的返回值
|
||||
* @param source Object 类型对象
|
||||
* @param handle 自定义的处理方法
|
||||
* @param defaultValue 默认为空的返回值
|
||||
* @param <T> 被检查对象为{@code null}返回默认值,否则返回自定义handle处理后的返回值
|
||||
* @return
|
||||
*/
|
||||
public static <T> T defaultIfNull(final Object source, Supplier<? extends T> handle, final T defaultValue) {
|
||||
if (Objects.nonNull(source)) {
|
||||
return handle.get();
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果给定对象为{@code null}或者""返回默认值, 否则返回自定义handle处理后的返回值
|
||||
* @param str String 类型
|
||||
* @param handle 自定义的处理方法
|
||||
* @param defaultValue 默认为空的返回值
|
||||
* @param <T> 被检查对象为{@code null}或者 ""返回默认值,否则返回自定义handle处理后的返回值
|
||||
* @return
|
||||
*/
|
||||
public static <T> T defaultIfEmpty(final String str, Supplier<? extends T> handle, final T defaultValue) {
|
||||
if (StrUtil.isNotEmpty(str)) {
|
||||
return handle.get();
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果给定对象为{@code null}或者 "" 返回默认值
|
||||
*
|
||||
|
@ -1,12 +1,14 @@
|
||||
package cn.hutool.core.util;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import cn.hutool.core.clone.CloneSupport;
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.date.DatePattern;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class ObjectUtilTest {
|
||||
|
||||
@ -29,4 +31,29 @@ public class ObjectUtilTest {
|
||||
String result = ObjectUtil.toString(strings);
|
||||
Assert.assertEquals("[1, 2]", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void defaultIfNullTest() {
|
||||
final String nullValue = null;
|
||||
final String dateStr = "2020-10-23 15:12:30";
|
||||
Instant result1 = ObjectUtil.defaultIfNull(dateStr,
|
||||
() -> DateUtil.parse(dateStr, DatePattern.NORM_DATETIME_PATTERN).toInstant(), Instant.now());
|
||||
Assert.assertNotNull(result1);
|
||||
Instant result2 = ObjectUtil.defaultIfNull(nullValue,
|
||||
() -> DateUtil.parse(nullValue, DatePattern.NORM_DATETIME_PATTERN).toInstant(), Instant.now());
|
||||
Assert.assertNotNull(result2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void defaultIfEmptyTest() {
|
||||
final String emptyValue = "";
|
||||
final String dateStr = "2020-10-23 15:12:30";
|
||||
Instant result1 = ObjectUtil.defaultIfEmpty(emptyValue,
|
||||
() -> DateUtil.parse(emptyValue, DatePattern.NORM_DATETIME_PATTERN).toInstant(), Instant.now());
|
||||
Assert.assertNotNull(result1);
|
||||
Instant result2 = ObjectUtil.defaultIfEmpty(dateStr,
|
||||
() -> DateUtil.parse(dateStr, DatePattern.NORM_DATETIME_PATTERN).toInstant(), Instant.now());
|
||||
Assert.assertNotNull(result2);
|
||||
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user