mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
!783 将ObjUtil.defaultIfNull检验参数类型从Object改为泛型,并补充重载方法和测试用例
Merge pull request !783 from Createsequence/fix-obj
This commit is contained in:
commit
c555a0cbab
@ -13,11 +13,7 @@ import cn.hutool.core.text.StrUtil;
|
||||
|
||||
import java.lang.reflect.Array;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Collection;
|
||||
import java.util.Enumeration;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.*;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
@ -301,6 +297,7 @@ public class ObjUtil {
|
||||
/**
|
||||
* 如果给定对象为{@code null} 返回默认值, 如果不为null 返回自定义handle处理后的返回值
|
||||
*
|
||||
* @param <R> 返回值类型
|
||||
* @param <T> 被检查对象类型
|
||||
* @param source Object 类型对象
|
||||
* @param handler 非空时自定义的处理方法
|
||||
@ -308,13 +305,30 @@ public class ObjUtil {
|
||||
* @return 处理后的返回值
|
||||
* @since 6.0.0
|
||||
*/
|
||||
public static <T> T defaultIfNull(final Object source, final Function<Object, T> handler, final Supplier<? extends T> defaultSupplier) {
|
||||
public static <T, R> R defaultIfNull(
|
||||
final T source, final Function<? super T, ? extends R> handler, final Supplier<? extends R> defaultSupplier) {
|
||||
if (isNotNull(source)) {
|
||||
return handler.apply(source);
|
||||
}
|
||||
return defaultSupplier.get();
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果给定对象为{@code null}返回默认值, 如果不为null返回自定义handle处理后的返回值
|
||||
*
|
||||
* @param <R> 返回值类型
|
||||
* @param <T> 被检查对象类型
|
||||
* @param source Object 类型对象
|
||||
* @param handler 非空时自定义的处理方法
|
||||
* @param defaultValue 默认为空的返回值
|
||||
* @return 处理后的返回值
|
||||
* @since 6.0.0
|
||||
*/
|
||||
public static <T, R> R defaultIfNull(
|
||||
final T source, final Function<? super T, ? extends R> handler, final R defaultValue) {
|
||||
return isNull(source) ? defaultValue : handler.apply(source);
|
||||
}
|
||||
|
||||
/**
|
||||
* 克隆对象<br>
|
||||
* 如果对象实现Cloneable接口,调用其clone方法<br>
|
||||
|
@ -1,18 +1,16 @@
|
||||
package cn.hutool.core.util;
|
||||
|
||||
import cn.hutool.core.collection.ListUtil;
|
||||
import cn.hutool.core.date.DatePattern;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.exceptions.CloneRuntimeException;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class ObjUtilTest {
|
||||
|
||||
@ -99,15 +97,20 @@ public class ObjUtilTest {
|
||||
|
||||
@Test
|
||||
public void defaultIfNullTest() {
|
||||
final String dateStr = "2020-10-23 15:12:30";
|
||||
final Instant result1 = ObjUtil.defaultIfNull(dateStr,
|
||||
(v) -> DateUtil.parse(v.toString(), DatePattern.NORM_DATETIME_PATTERN).toInstant(), Instant::now);
|
||||
Assert.assertNotNull(result1);
|
||||
final Object val1 = new Object();
|
||||
final Object val2 = new Object();
|
||||
|
||||
final String nullValue = null;
|
||||
final Instant result2 = ObjUtil.defaultIfNull(nullValue,
|
||||
(v) -> DateUtil.parse(v.toString(), DatePattern.NORM_DATETIME_PATTERN).toInstant(), Instant::now);
|
||||
Assert.assertNotNull(result2);
|
||||
Assert.assertSame(val1, ObjUtil.defaultIfNull(val1, () -> val2));
|
||||
Assert.assertSame(val2, ObjUtil.defaultIfNull(null, () -> val2));
|
||||
|
||||
Assert.assertSame(val1, ObjUtil.defaultIfNull(val1, val2));
|
||||
Assert.assertSame(val2, ObjUtil.defaultIfNull(null, val2));
|
||||
|
||||
Assert.assertSame(val1, ObjUtil.defaultIfNull(val1, Function.identity(), () -> val2));
|
||||
Assert.assertSame(val2, ObjUtil.defaultIfNull(null, Function.identity(), () -> val2));
|
||||
|
||||
Assert.assertSame(val1, ObjUtil.defaultIfNull(val1, Function.identity(), val2));
|
||||
Assert.assertSame(val2, ObjUtil.defaultIfNull(null, Function.identity(), val2));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
Loading…
x
Reference in New Issue
Block a user