mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
fix methods
This commit is contained in:
parent
719b5a226d
commit
62e8deed90
@ -148,12 +148,12 @@ public class CollUtil {
|
||||
* @param <T> 集合类型
|
||||
* @param <E> 集合元素类型
|
||||
* @param collection 集合
|
||||
* @param supplier 默认值懒加载函数
|
||||
* @param defaultSupplier 默认值懒加载函数
|
||||
* @return 非空(empty)的原集合或默认集合
|
||||
* @since 5.7.15
|
||||
*/
|
||||
public static <T extends Collection<E>, E> T defaultIfEmpty(final T collection, final Supplier<? extends T> supplier) {
|
||||
return isEmpty(collection) ? supplier.get() : collection;
|
||||
public static <T extends Collection<E>, E> T defaultIfEmpty(final T collection, final Function<T, T> handler, final Supplier<? extends T> defaultSupplier) {
|
||||
return isEmpty(collection) ? defaultSupplier.get() : handler.apply(collection);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -2546,4 +2546,29 @@ public class CollUtil {
|
||||
|
||||
return IterUtil.isEqualList(list1, list2);
|
||||
}
|
||||
|
||||
/**
|
||||
* 一个对象不为空且不存在于该集合中时,加入到该集合中<br>
|
||||
* <pre>
|
||||
* null, null => false
|
||||
* [], null => false
|
||||
* null, "123" => false
|
||||
* ["123"], "123" => false
|
||||
* [], "123" => true
|
||||
* ["456"], "123" => true
|
||||
* [Animal{"name": "jack"}], Dog{"name": "jack"} => true
|
||||
* </pre>
|
||||
* @param collection 被加入的集合
|
||||
* @param object 要添加到集合的对象
|
||||
* @param <T> 集合元素类型
|
||||
* @param <S> 要添加的元素类型【为集合元素类型的类型或子类型】
|
||||
* @return 是否添加成功
|
||||
* @author Cloud-Style
|
||||
*/
|
||||
public static <T, S extends T> boolean addIfAbsent(Collection<T> collection, S object) {
|
||||
if (object == null || collection == null || collection.contains(object)) {
|
||||
return false;
|
||||
}
|
||||
return collection.add(object);
|
||||
}
|
||||
}
|
||||
|
@ -237,7 +237,7 @@ public final class UrlBuilder implements Builder<String> {
|
||||
* @return 协议,例如http
|
||||
*/
|
||||
public String getSchemeWithDefault() {
|
||||
return StrUtil.emptyToDefault(this.scheme, DEFAULT_SCHEME);
|
||||
return StrUtil.defaultIfEmpty(this.scheme, DEFAULT_SCHEME);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -14,6 +14,7 @@ import cn.hutool.core.text.split.SplitUtil;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.util.CharUtil;
|
||||
import cn.hutool.core.util.CharsetUtil;
|
||||
import cn.hutool.core.util.ObjUtil;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.charset.Charset;
|
||||
@ -25,6 +26,7 @@ import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* {@link CharSequence} 相关工具类封装
|
||||
@ -32,7 +34,7 @@ import java.util.function.Predicate;
|
||||
* @author looly
|
||||
* @since 5.5.3
|
||||
*/
|
||||
public class CharSequenceUtil extends StrChecker{
|
||||
public class CharSequenceUtil extends StrChecker {
|
||||
|
||||
public static final int INDEX_NOT_FOUND = Finder.INDEX_NOT_FOUND;
|
||||
|
||||
@ -41,18 +43,6 @@ public class CharSequenceUtil extends StrChecker{
|
||||
*/
|
||||
public static final String SPACE = " ";
|
||||
|
||||
/**
|
||||
* 当给定字符串为null时,转换为Empty
|
||||
*
|
||||
* @param str 被检查的字符串
|
||||
* @return 原字符串或者空串
|
||||
* @see #nullToEmpty(CharSequence)
|
||||
* @since 4.6.3
|
||||
*/
|
||||
public static String emptyIfNull(final CharSequence str) {
|
||||
return nullToEmpty(str);
|
||||
}
|
||||
|
||||
/**
|
||||
* 当给定字符串为null时,转换为Empty
|
||||
*
|
||||
@ -60,63 +50,7 @@ public class CharSequenceUtil extends StrChecker{
|
||||
* @return 转换后的字符串
|
||||
*/
|
||||
public static String nullToEmpty(final CharSequence str) {
|
||||
return nullToDefault(str, EMPTY);
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果字符串是 {@code null},则返回指定默认字符串,否则返回字符串本身。
|
||||
*
|
||||
* <pre>
|
||||
* nullToDefault(null, "default") = "default"
|
||||
* nullToDefault("", "default") = ""
|
||||
* nullToDefault(" ", "default") = " "
|
||||
* nullToDefault("bat", "default") = "bat"
|
||||
* </pre>
|
||||
*
|
||||
* @param str 要转换的字符串
|
||||
* @param defaultStr 默认字符串
|
||||
* @return 字符串本身或指定的默认字符串
|
||||
*/
|
||||
public static String nullToDefault(final CharSequence str, final String defaultStr) {
|
||||
return (str == null) ? defaultStr : str.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果字符串是{@code null}或者"",则返回指定默认字符串,否则返回字符串本身。
|
||||
*
|
||||
* <pre>
|
||||
* emptyToDefault(null, "default") = "default"
|
||||
* emptyToDefault("", "default") = "default"
|
||||
* emptyToDefault(" ", "default") = " "
|
||||
* emptyToDefault("bat", "default") = "bat"
|
||||
* </pre>
|
||||
*
|
||||
* @param str 要转换的字符串
|
||||
* @param defaultStr 默认字符串
|
||||
* @return 字符串本身或指定的默认字符串
|
||||
* @since 4.1.0
|
||||
*/
|
||||
public static String emptyToDefault(final CharSequence str, final String defaultStr) {
|
||||
return isEmpty(str) ? defaultStr : str.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果字符串是{@code null}或者""或者空白,则返回指定默认字符串,否则返回字符串本身。
|
||||
*
|
||||
* <pre>
|
||||
* blankToDefault(null, "default") = "default"
|
||||
* blankToDefault("", "default") = "default"
|
||||
* blankToDefault(" ", "default") = "default"
|
||||
* blankToDefault("bat", "default") = "bat"
|
||||
* </pre>
|
||||
*
|
||||
* @param str 要转换的字符串
|
||||
* @param defaultStr 默认字符串
|
||||
* @return 字符串本身或指定的默认字符串
|
||||
* @since 4.1.0
|
||||
*/
|
||||
public static String blankToDefault(final CharSequence str, final String defaultStr) {
|
||||
return isBlank(str) ? defaultStr : str.toString();
|
||||
return ObjUtil.defaultIfNull(str, EMPTY).toString();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -125,8 +59,85 @@ public class CharSequenceUtil extends StrChecker{
|
||||
* @param str 被转换的字符串
|
||||
* @return 转换后的字符串
|
||||
*/
|
||||
public static String emptyToNull(final CharSequence str) {
|
||||
return isEmpty(str) ? null : str.toString();
|
||||
public static <T extends CharSequence> T emptyToNull(final T str) {
|
||||
return isEmpty(str) ? null : str;
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果给定对象为{@code null}或者 "" 返回默认值
|
||||
*
|
||||
* <pre>
|
||||
* defaultIfEmpty(null, null) = null
|
||||
* defaultIfEmpty(null, "") = ""
|
||||
* defaultIfEmpty("", "zz") = "zz"
|
||||
* defaultIfEmpty(" ", "zz") = " "
|
||||
* defaultIfEmpty("abc", *) = "abc"
|
||||
* </pre>
|
||||
*
|
||||
* @param <T> 对象类型(必须实现CharSequence接口)
|
||||
* @param str 被检查对象,可能为{@code null}
|
||||
* @param defaultValue 被检查对象为{@code null}或者 ""返回的默认值,可以为{@code null}或者 ""
|
||||
* @return 被检查对象为{@code null}或者 ""返回默认值,否则返回原值
|
||||
* @since 5.0.4
|
||||
*/
|
||||
public static <T extends CharSequence> T defaultIfEmpty(final T str, final T defaultValue) {
|
||||
return StrUtil.isEmpty(str) ? defaultValue : str;
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果给定对象为{@code null}或者{@code ""}返回defaultHandler处理的结果, 否则返回自定义handler处理后的返回值
|
||||
*
|
||||
* @param <T> 被检查对象类型
|
||||
* @param <V> 结果类型
|
||||
* @param str String 类型
|
||||
* @param handler 非empty的处理方法
|
||||
* @param defaultSupplier empty时的处理方法
|
||||
* @return 处理后的返回值
|
||||
*/
|
||||
public static <T extends CharSequence, V> V defaultIfEmpty(final T str, final Function<T, V> handler, final Supplier<? extends V> defaultSupplier) {
|
||||
if (isNotEmpty(str)) {
|
||||
return handler.apply(str);
|
||||
}
|
||||
return defaultSupplier.get();
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果给定对象为{@code null}或者""或者空白符返回默认值
|
||||
*
|
||||
* <pre>
|
||||
* defaultIfBlank(null, null) = null
|
||||
* defaultIfBlank(null, "") = ""
|
||||
* defaultIfBlank("", "zz") = "zz"
|
||||
* defaultIfBlank(" ", "zz") = "zz"
|
||||
* defaultIfBlank("abc", *) = "abc"
|
||||
* </pre>
|
||||
*
|
||||
* @param <T> 对象类型(必须实现CharSequence接口)
|
||||
* @param str 被检查对象,可能为{@code null}
|
||||
* @param defaultValue 被检查对象为{@code null}或者 ""或者空白符返回的默认值,可以为{@code null}或者 ""或者空白符
|
||||
* @return 被检查对象为{@code null}或者 ""或者空白符返回默认值,否则返回原值
|
||||
* @since 5.0.4
|
||||
*/
|
||||
public static <T extends CharSequence> T defaultIfBlank(final T str, final T defaultValue) {
|
||||
return StrUtil.isBlank(str) ? defaultValue : str;
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果被检查对象为 {@code null} 或 "" 或 空白字符串时,返回默认值(由 defaultValueSupplier 提供);否则直接返回
|
||||
*
|
||||
* @param str 被检查对象
|
||||
* @param handler 非blank的处理方法
|
||||
* @param defaultSupplier 默认值提供者
|
||||
* @param <T> 对象类型(必须实现CharSequence接口)
|
||||
* @return 被检查对象为{@code null}返回默认值,否则返回自定义handle处理后的返回值
|
||||
* @throws NullPointerException {@code defaultValueSupplier == null} 时,抛出
|
||||
* @since 5.7.20
|
||||
*/
|
||||
public static <T extends CharSequence, V> V defaultIfBlank(final T str, final Function<T, V> handler, final Supplier<? extends V> defaultSupplier) {
|
||||
if (StrUtil.isBlank(str)) {
|
||||
return defaultSupplier.get();
|
||||
}
|
||||
return handler.apply(str);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------ Trim
|
||||
|
@ -18,6 +18,7 @@ import java.util.Enumeration;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
@ -282,130 +283,36 @@ public class ObjUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果被检查对象为 {@code null}, 返回默认值(由 defaultValueSupplier 提供);否则直接返回
|
||||
* 如果给定对象为{@code null} 返回默认值, 如果不为null 返回自定义handle处理后的返回值
|
||||
*
|
||||
* @param source 被检查对象
|
||||
* @param defaultValueSupplier 默认值提供者
|
||||
* @param <T> 对象类型
|
||||
* @return 被检查对象为{@code null}返回默认值,否则返回自定义handle处理后的返回值
|
||||
* @throws NullPointerException {@code defaultValueSupplier == null} 时,抛出
|
||||
* @since 5.7.20
|
||||
* @param <T> 被检查对象类型
|
||||
* @param source Object 类型对象
|
||||
* @param defaultSupplier 默认为空的处理逻辑
|
||||
* @return 处理后的返回值
|
||||
* @since 5.4.6
|
||||
*/
|
||||
public static <T> T defaultIfNull(final T source, final Supplier<? extends T> defaultValueSupplier) {
|
||||
if (isNull(source)) {
|
||||
return defaultValueSupplier.get();
|
||||
public static <T> T defaultIfNull(final T source, final Supplier<? extends T> defaultSupplier) {
|
||||
if (isNotNull(source)) {
|
||||
return source;
|
||||
}
|
||||
return source;
|
||||
return defaultSupplier.get();
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果给定对象为{@code null} 返回默认值, 如果不为null 返回自定义handle处理后的返回值
|
||||
*
|
||||
* @param <T> 被检查对象类型
|
||||
* @param source Object 类型对象
|
||||
* @param handle 非空时自定义的处理方法
|
||||
* @param defaultValue 默认为空的返回值
|
||||
* @param <T> 被检查对象为{@code null}返回默认值,否则返回自定义handle处理后的返回值
|
||||
* @param handler 非空时自定义的处理方法
|
||||
* @param defaultSupplier 默认为空的处理逻辑
|
||||
* @return 处理后的返回值
|
||||
* @since 5.4.6
|
||||
* @since 6.0.0
|
||||
*/
|
||||
public static <T> T defaultIfNull(final Object source, final Supplier<? extends T> handle, final T defaultValue) {
|
||||
public static <T> T defaultIfNull(final Object source, final Function<Object, T> handler, final Supplier<? extends T> defaultSupplier) {
|
||||
if (isNotNull(source)) {
|
||||
return handle.get();
|
||||
return handler.apply(source);
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果给定对象为{@code null}或者""返回默认值, 否则返回自定义handle处理后的返回值
|
||||
*
|
||||
* @param str String 类型
|
||||
* @param handle 自定义的处理方法
|
||||
* @param defaultValue 默认为空的返回值
|
||||
* @param <T> 被检查对象为{@code null}或者 ""返回默认值,否则返回自定义handle处理后的返回值
|
||||
* @return 处理后的返回值
|
||||
* @since 5.4.6
|
||||
*/
|
||||
public static <T> T defaultIfEmpty(final String str, final Supplier<? extends T> handle, final T defaultValue) {
|
||||
if (StrUtil.isNotEmpty(str)) {
|
||||
return handle.get();
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果给定对象为{@code null}或者 "" 返回默认值
|
||||
*
|
||||
* <pre>
|
||||
* ObjectUtil.defaultIfEmpty(null, null) = null
|
||||
* ObjectUtil.defaultIfEmpty(null, "") = ""
|
||||
* ObjectUtil.defaultIfEmpty("", "zz") = "zz"
|
||||
* ObjectUtil.defaultIfEmpty(" ", "zz") = " "
|
||||
* ObjectUtil.defaultIfEmpty("abc", *) = "abc"
|
||||
* </pre>
|
||||
*
|
||||
* @param <T> 对象类型(必须实现CharSequence接口)
|
||||
* @param str 被检查对象,可能为{@code null}
|
||||
* @param defaultValue 被检查对象为{@code null}或者 ""返回的默认值,可以为{@code null}或者 ""
|
||||
* @return 被检查对象为{@code null}或者 ""返回默认值,否则返回原值
|
||||
* @since 5.0.4
|
||||
*/
|
||||
public static <T extends CharSequence> T defaultIfEmpty(final T str, final T defaultValue) {
|
||||
return StrUtil.isEmpty(str) ? defaultValue : str;
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果被检查对象为 {@code null} 或 "" 时,返回默认值(由 defaultValueSupplier 提供);否则直接返回
|
||||
*
|
||||
* @param str 被检查对象
|
||||
* @param defaultValueSupplier 默认值提供者
|
||||
* @param <T> 对象类型(必须实现CharSequence接口)
|
||||
* @return 被检查对象为{@code null}返回默认值,否则返回自定义handle处理后的返回值
|
||||
* @throws NullPointerException {@code defaultValueSupplier == null} 时,抛出
|
||||
* @since 5.7.20
|
||||
*/
|
||||
public static <T extends CharSequence> T defaultIfEmpty(final T str, final Supplier<? extends T> defaultValueSupplier) {
|
||||
if (StrUtil.isEmpty(str)) {
|
||||
return defaultValueSupplier.get();
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果给定对象为{@code null}或者""或者空白符返回默认值
|
||||
*
|
||||
* <pre>
|
||||
* ObjectUtil.defaultIfBlank(null, null) = null
|
||||
* ObjectUtil.defaultIfBlank(null, "") = ""
|
||||
* ObjectUtil.defaultIfBlank("", "zz") = "zz"
|
||||
* ObjectUtil.defaultIfBlank(" ", "zz") = "zz"
|
||||
* ObjectUtil.defaultIfBlank("abc", *) = "abc"
|
||||
* </pre>
|
||||
*
|
||||
* @param <T> 对象类型(必须实现CharSequence接口)
|
||||
* @param str 被检查对象,可能为{@code null}
|
||||
* @param defaultValue 被检查对象为{@code null}或者 ""或者空白符返回的默认值,可以为{@code null}或者 ""或者空白符
|
||||
* @return 被检查对象为{@code null}或者 ""或者空白符返回默认值,否则返回原值
|
||||
* @since 5.0.4
|
||||
*/
|
||||
public static <T extends CharSequence> T defaultIfBlank(final T str, final T defaultValue) {
|
||||
return StrUtil.isBlank(str) ? defaultValue : str;
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果被检查对象为 {@code null} 或 "" 或 空白字符串时,返回默认值(由 defaultValueSupplier 提供);否则直接返回
|
||||
*
|
||||
* @param str 被检查对象
|
||||
* @param defaultValueSupplier 默认值提供者
|
||||
* @param <T> 对象类型(必须实现CharSequence接口)
|
||||
* @return 被检查对象为{@code null}返回默认值,否则返回自定义handle处理后的返回值
|
||||
* @throws NullPointerException {@code defaultValueSupplier == null} 时,抛出
|
||||
* @since 5.7.20
|
||||
*/
|
||||
public static <T extends CharSequence> T defaultIfBlank(final T str, final Supplier<? extends T> defaultValueSupplier) {
|
||||
if (StrUtil.isBlank(str)) {
|
||||
return defaultValueSupplier.get();
|
||||
}
|
||||
return str;
|
||||
return defaultSupplier.get();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -607,8 +514,8 @@ public class ObjUtil {
|
||||
*
|
||||
* @param objs 被检查对象
|
||||
* @return 是否存在
|
||||
* @since 5.5.3
|
||||
* @see ArrayUtil#hasNull(Object[])
|
||||
* @since 5.5.3
|
||||
*/
|
||||
public static boolean hasNull(final Object... objs) {
|
||||
return ArrayUtil.hasNull(objs);
|
||||
|
@ -2,7 +2,6 @@ package cn.hutool.core.util;
|
||||
|
||||
import cn.hutool.core.convert.Convert;
|
||||
import cn.hutool.core.lang.Console;
|
||||
import cn.hutool.core.text.StrUtil;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
@ -32,7 +31,7 @@ public class SystemUtil {
|
||||
* @see System#getenv(String)
|
||||
*/
|
||||
public static String get(final String name, final String defaultValue) {
|
||||
return StrUtil.nullToDefault(get(name, false), defaultValue);
|
||||
return ObjUtil.defaultIfNull(get(name, false), defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -9,6 +9,9 @@ import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.core.text.StrUtil;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.ToString;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
@ -27,6 +30,7 @@ import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.SortedSet;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* 集合工具类单元测试
|
||||
@ -35,6 +39,21 @@ import java.util.SortedSet;
|
||||
*/
|
||||
public class CollUtilTest {
|
||||
|
||||
@Test
|
||||
public void defaultIfEmpty() {
|
||||
ArrayList<String> strings = CollUtil.defaultIfEmpty(ListUtil.of(), ListUtil.of("1"));
|
||||
Assert.assertEquals(ListUtil.of("1"), strings);
|
||||
|
||||
strings = CollUtil.defaultIfEmpty(null, ListUtil.of("1"));
|
||||
Assert.assertEquals(ListUtil.of("1"), strings);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void defaultIfEmpty2() {
|
||||
ArrayList<String> strings = CollUtil.defaultIfEmpty(ListUtil.of(), Function.identity(), () -> ListUtil.of("1"));
|
||||
Assert.assertEquals(ListUtil.of("1"), strings);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPredicateContains() {
|
||||
final ArrayList<String> list = ListUtil.of("bbbbb", "aaaaa", "ccccc");
|
||||
@ -974,4 +993,39 @@ public class CollUtilTest {
|
||||
final Collection<String> collection = CollUtil.intersectionDistinct(list1, list2, list3);
|
||||
Assert.assertNotNull(collection);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addIfAbsentTest() {
|
||||
// 为false的情况
|
||||
Assert.assertFalse(CollUtil.addIfAbsent(null, null));
|
||||
Assert.assertFalse(CollUtil.addIfAbsent(ListUtil.of(), null));
|
||||
Assert.assertFalse(CollUtil.addIfAbsent(null, "123"));
|
||||
Assert.assertFalse(CollUtil.addIfAbsent(ListUtil.of("123"), "123"));
|
||||
Assert.assertFalse(CollUtil.addIfAbsent(ListUtil.of(new Animal("jack", 20)),
|
||||
new Animal("jack", 20)));
|
||||
|
||||
// 正常情况
|
||||
Assert.assertTrue(CollUtil.addIfAbsent(ListUtil.of("456"), "123"));
|
||||
Assert.assertTrue(CollUtil.addIfAbsent(ListUtil.of(new Animal("jack", 20)),
|
||||
new Dog("jack", 20)));
|
||||
Assert.assertTrue(CollUtil.addIfAbsent(ListUtil.of(new Animal("jack", 20)),
|
||||
new Animal("tom", 20)));
|
||||
}
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
static class Animal {
|
||||
private String name;
|
||||
private Integer age;
|
||||
}
|
||||
|
||||
@ToString(callSuper = true)
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Data
|
||||
static class Dog extends Animal {
|
||||
public Dog(String name, Integer age) {
|
||||
super(name, age);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,9 +1,12 @@
|
||||
package cn.hutool.core.text;
|
||||
|
||||
import cn.hutool.core.date.DatePattern;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.util.CharsetUtil;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class CharSequenceUtilTest {
|
||||
@ -166,4 +169,30 @@ public class CharSequenceUtilTest {
|
||||
final String a = "2142342422423423";
|
||||
Assert.assertTrue(StrUtil.containsAll(a, "214", "234"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void defaultIfEmptyTest() {
|
||||
final String emptyValue = "";
|
||||
final Instant result1 = CharSequenceUtil.defaultIfEmpty(emptyValue,
|
||||
(v) -> DateUtil.parse(v, DatePattern.NORM_DATETIME_PATTERN).toInstant(), Instant::now);
|
||||
Assert.assertNotNull(result1);
|
||||
|
||||
final String dateStr = "2020-10-23 15:12:30";
|
||||
final Instant result2 = CharSequenceUtil.defaultIfEmpty(dateStr,
|
||||
(v) -> DateUtil.parse(v, DatePattern.NORM_DATETIME_PATTERN).toInstant(), Instant::now);
|
||||
Assert.assertNotNull(result2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void defaultIfBlankTest() {
|
||||
final String emptyValue = " ";
|
||||
final Instant result1 = CharSequenceUtil.defaultIfBlank(emptyValue,
|
||||
(v) -> DateUtil.parse(v, DatePattern.NORM_DATETIME_PATTERN).toInstant(), Instant::now);
|
||||
Assert.assertNotNull(result1);
|
||||
|
||||
final String dateStr = "2020-10-23 15:12:30";
|
||||
final Instant result2 = CharSequenceUtil.defaultIfBlank(dateStr,
|
||||
(v) -> DateUtil.parse(v, DatePattern.NORM_DATETIME_PATTERN).toInstant(), Instant::now);
|
||||
Assert.assertNotNull(result2);
|
||||
}
|
||||
}
|
||||
|
@ -99,25 +99,14 @@ public class ObjUtilTest {
|
||||
|
||||
@Test
|
||||
public void defaultIfNullTest() {
|
||||
final String nullValue = null;
|
||||
final String dateStr = "2020-10-23 15:12:30";
|
||||
final Instant result1 = ObjUtil.defaultIfNull(dateStr,
|
||||
() -> DateUtil.parse(dateStr, DatePattern.NORM_DATETIME_PATTERN).toInstant(), Instant.now());
|
||||
(v) -> DateUtil.parse(v.toString(), DatePattern.NORM_DATETIME_PATTERN).toInstant(), Instant::now);
|
||||
Assert.assertNotNull(result1);
|
||||
final Instant result2 = ObjUtil.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";
|
||||
final Instant result1 = ObjUtil.defaultIfEmpty(emptyValue,
|
||||
() -> DateUtil.parse(emptyValue, DatePattern.NORM_DATETIME_PATTERN).toInstant(), Instant.now());
|
||||
Assert.assertNotNull(result1);
|
||||
final Instant result2 = ObjUtil.defaultIfEmpty(dateStr,
|
||||
() -> DateUtil.parse(dateStr, DatePattern.NORM_DATETIME_PATTERN).toInstant(), Instant.now());
|
||||
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);
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user