From 34f20cb4d5782956d78f45c372cc7acc2ebca511 Mon Sep 17 00:00:00 2001 From: liliexuan Date: Fri, 23 Oct 2020 12:43:45 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BF=94=E5=9B=9E=E5=AF=B9=E8=B1=A1=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E8=87=AA=E5=AE=9A=E4=B9=89=E5=A4=84=E7=90=86=E5=90=8E?= =?UTF-8?q?=E8=BF=94=E5=9B=9E=EF=BC=8C=E5=A6=82=E6=9E=9C=E4=B8=8D=E5=AD=98?= =?UTF-8?q?=E5=9C=A8=E5=88=99=E8=BF=94=E5=9B=9E=E7=94=A8=E6=88=B7=E5=AE=9A?= =?UTF-8?q?=E4=B9=89=E7=B1=BB=E5=9E=8B=E7=9A=84=E9=BB=98=E8=AE=A4=E5=AF=B9?= =?UTF-8?q?=E8=B1=A1.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/cn/hutool/core/util/ObjectUtil.java | 38 ++++++++++++++++++- .../cn/hutool/core/util/ObjectUtilTest.java | 37 +++++++++++++++--- 2 files changed, 69 insertions(+), 6 deletions(-) diff --git a/hutool-core/src/main/java/cn/hutool/core/util/ObjectUtil.java b/hutool-core/src/main/java/cn/hutool/core/util/ObjectUtil.java index 14100d28a..8d476d737 100644 --- a/hutool-core/src/main/java/cn/hutool/core/util/ObjectUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/util/ObjectUtil.java @@ -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 被检查对象为{@code null}返回默认值,否则返回自定义handle处理后的返回值 + * @return + */ + public static T defaultIfNull(final Object source, Supplier handle, final T defaultValue) { + if (Objects.nonNull(source)) { + return handle.get(); + } + return defaultValue; + } + + /** + * 如果给定对象为{@code null}或者""返回默认值, 否则返回自定义handle处理后的返回值 + * @param str String 类型 + * @param handle 自定义的处理方法 + * @param defaultValue 默认为空的返回值 + * @param 被检查对象为{@code null}或者 ""返回默认值,否则返回自定义handle处理后的返回值 + * @return + */ + public static T defaultIfEmpty(final String str, Supplier handle, final T defaultValue) { + if (StrUtil.isNotEmpty(str)) { + return handle.get(); + } + return defaultValue; + } + /** * 如果给定对象为{@code null}或者 "" 返回默认值 * diff --git a/hutool-core/src/test/java/cn/hutool/core/util/ObjectUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/util/ObjectUtilTest.java index d9f3c627c..68199123e 100644 --- a/hutool-core/src/test/java/cn/hutool/core/util/ObjectUtilTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/util/ObjectUtilTest.java @@ -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); + + } }