mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
add methods
This commit is contained in:
parent
b30bf90210
commit
b9c7b5f4bf
@ -30,6 +30,7 @@ import org.dromara.hutool.core.text.replacer.SearchReplacer;
|
|||||||
import org.dromara.hutool.core.text.split.SplitUtil;
|
import org.dromara.hutool.core.text.split.SplitUtil;
|
||||||
import org.dromara.hutool.core.util.ByteUtil;
|
import org.dromara.hutool.core.util.ByteUtil;
|
||||||
import org.dromara.hutool.core.util.CharsetUtil;
|
import org.dromara.hutool.core.util.CharsetUtil;
|
||||||
|
import org.dromara.hutool.core.util.ObjUtil;
|
||||||
|
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.nio.CharBuffer;
|
import java.nio.CharBuffer;
|
||||||
@ -100,8 +101,8 @@ public class CharSequenceUtil extends StrValidator {
|
|||||||
*
|
*
|
||||||
* @param obj 对象
|
* @param obj 对象
|
||||||
* @return 字符串
|
* @return 字符串
|
||||||
* @since 4.1.3
|
|
||||||
* @see String#valueOf(Object)
|
* @see String#valueOf(Object)
|
||||||
|
* @since 4.1.3
|
||||||
*/
|
*/
|
||||||
public static String toString(final Object obj) {
|
public static String toString(final Object obj) {
|
||||||
return String.valueOf(obj);
|
return String.valueOf(obj);
|
||||||
@ -133,6 +134,19 @@ public class CharSequenceUtil extends StrValidator {
|
|||||||
// endregion
|
// endregion
|
||||||
|
|
||||||
// region ----- defaultIf
|
// region ----- defaultIf
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 当给定字符串为空字符串时,转换为""<br>
|
||||||
|
* 此方法与{@link #toStringOrEmpty(Object)}不同的是,如果提供的{@link CharSequence}非String,则保持原状
|
||||||
|
*
|
||||||
|
* @param str 被转换的字符串
|
||||||
|
* @return 转换后的字符串
|
||||||
|
* @see #toStringOrEmpty(Object)
|
||||||
|
*/
|
||||||
|
public static CharSequence emptyIfNull(final CharSequence str) {
|
||||||
|
return null == str ? EMPTY : str;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 当给定字符串为空字符串时,转换为{@code null}
|
* 当给定字符串为空字符串时,转换为{@code null}
|
||||||
*
|
*
|
||||||
@ -144,6 +158,54 @@ public class CharSequenceUtil extends StrValidator {
|
|||||||
return isEmpty(str) ? null : str;
|
return isEmpty(str) ? null : str;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>如果给定字符串为{@code null}返回默认值
|
||||||
|
* <pre>{@code
|
||||||
|
* defaultIfNull(null, null); // = null
|
||||||
|
* defaultIfNull(null, ""); // = ""
|
||||||
|
* defaultIfNull(null, "zz"); // = "zz"
|
||||||
|
* defaultIfNull("abc", *); // = "abc"
|
||||||
|
* }</pre>
|
||||||
|
*
|
||||||
|
* @param <T> 字符串类型
|
||||||
|
* @param str 被检查字符串,可能为{@code null}
|
||||||
|
* @param defaultValue 被检查字符串为{@code null}返回的默认值,可以为{@code null}
|
||||||
|
* @return 被检查字符串不为 {@code null} 返回原值,否则返回默认值
|
||||||
|
* @see ObjUtil#defaultIfNull(Object, Object)
|
||||||
|
*/
|
||||||
|
public static <T extends CharSequence> T defaultIfNull(final T str, final T defaultValue) {
|
||||||
|
return ObjUtil.defaultIfNull(str, defaultValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 如果给定字符串不为{@code null} 返回原值, 否则返回 {@link Supplier#get()} 提供的默认值
|
||||||
|
*
|
||||||
|
* @param <T> 被检查字符串类型
|
||||||
|
* @param source 被检查字符串,可能为{@code null}
|
||||||
|
* @param defaultSupplier 为空时的默认值提供者
|
||||||
|
* @return 被检查字符串不为 {@code null} 返回原值,否则返回 {@link Supplier#get()} 提供的默认值
|
||||||
|
* @see ObjUtil#defaultIfNull(Object, Supplier)
|
||||||
|
*/
|
||||||
|
public static <T extends CharSequence> T defaultIfNull(final T source, final Supplier<? extends T> defaultSupplier) {
|
||||||
|
return ObjUtil.defaultIfNull(source, defaultSupplier);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 如果给定字符串不为{@code null} 返回自定义handler处理后的结果,否则返回 {@link Supplier#get()} 提供的默认值
|
||||||
|
*
|
||||||
|
* @param <R> 返回值类型
|
||||||
|
* @param <T> 被检查对象类型
|
||||||
|
* @param source 被检查对象,可能为{@code null}
|
||||||
|
* @param handler 非空时自定义的处理方法
|
||||||
|
* @param defaultSupplier 为空时的默认值提供者
|
||||||
|
* @return 被检查对象不为 {@code null} 返回处理后的结果,否则返回 {@link Supplier#get()} 提供的默认值
|
||||||
|
* @since 6.0.0
|
||||||
|
* @see ObjUtil#defaultIfNull(Object, Function, Supplier)
|
||||||
|
*/
|
||||||
|
public static <T extends CharSequence, R> R defaultIfNull(final T source, final Function<? super T, ? extends R> handler, final Supplier<? extends R> defaultSupplier) {
|
||||||
|
return ObjUtil.defaultIfNull(source, handler, defaultSupplier);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 如果给定对象为{@code null}或者 "" 返回默认值
|
* 如果给定对象为{@code null}或者 "" 返回默认值
|
||||||
*
|
*
|
||||||
@ -165,6 +227,18 @@ public class CharSequenceUtil extends StrValidator {
|
|||||||
return isEmpty(str) ? defaultValue : str;
|
return isEmpty(str) ? defaultValue : str;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 如果给定对象为{@code null}或者{@code ""}返回原值, 否则返回自定义handler处理后的返回值
|
||||||
|
*
|
||||||
|
* @param <T> 被检查对象类型
|
||||||
|
* @param str String 类型
|
||||||
|
* @param defaultSupplier empty时的处理方法
|
||||||
|
* @return 处理后的返回值
|
||||||
|
*/
|
||||||
|
public static <T extends CharSequence> T defaultIfEmpty(final T str, final Supplier<? extends T> defaultSupplier) {
|
||||||
|
return isEmpty(str) ? defaultSupplier.get() : str;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 如果给定对象为{@code null}或者{@code ""}返回defaultHandler处理的结果, 否则返回自定义handler处理后的返回值
|
* 如果给定对象为{@code null}或者{@code ""}返回defaultHandler处理的结果, 否则返回自定义handler处理后的返回值
|
||||||
*
|
*
|
||||||
@ -176,10 +250,7 @@ public class CharSequenceUtil extends StrValidator {
|
|||||||
* @return 处理后的返回值
|
* @return 处理后的返回值
|
||||||
*/
|
*/
|
||||||
public static <T extends CharSequence, V> V defaultIfEmpty(final T str, final Function<T, V> handler, final Supplier<? extends V> defaultSupplier) {
|
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 isEmpty(str) ? defaultSupplier.get() : handler.apply(str);
|
||||||
return handler.apply(str);
|
|
||||||
}
|
|
||||||
return defaultSupplier.get();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -203,6 +274,18 @@ public class CharSequenceUtil extends StrValidator {
|
|||||||
return isBlank(str) ? defaultValue : str;
|
return isBlank(str) ? defaultValue : str;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 如果给定对象为{@code null}或者{@code ""}返回原值, 否则返回自定义handler处理后的返回值
|
||||||
|
*
|
||||||
|
* @param <T> 被检查对象类型
|
||||||
|
* @param str String 类型
|
||||||
|
* @param defaultSupplier empty时的处理方法
|
||||||
|
* @return 处理后的返回值
|
||||||
|
*/
|
||||||
|
public static <T extends CharSequence> T defaultIfBlank(final T str, final Supplier<? extends T> defaultSupplier) {
|
||||||
|
return isBlank(str) ? defaultSupplier.get() : str;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 如果被检查对象为 {@code null} 或 "" 或 空白字符串时,返回默认值(由 defaultValueSupplier 提供);否则直接返回
|
* 如果被检查对象为 {@code null} 或 "" 或 空白字符串时,返回默认值(由 defaultValueSupplier 提供);否则直接返回
|
||||||
*
|
*
|
||||||
@ -216,10 +299,7 @@ public class CharSequenceUtil extends StrValidator {
|
|||||||
* @since 5.7.20
|
* @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) {
|
public static <T extends CharSequence, V> V defaultIfBlank(final T str, final Function<T, V> handler, final Supplier<? extends V> defaultSupplier) {
|
||||||
if (isBlank(str)) {
|
return isBlank(str) ? defaultSupplier.get() : handler.apply(str);
|
||||||
return defaultSupplier.get();
|
|
||||||
}
|
|
||||||
return handler.apply(str);
|
|
||||||
}
|
}
|
||||||
// endregion
|
// endregion
|
||||||
|
|
||||||
|
@ -278,11 +278,11 @@ public class ObjUtil {
|
|||||||
/**
|
/**
|
||||||
* <p>如果给定对象为{@code null}返回默认值
|
* <p>如果给定对象为{@code null}返回默认值
|
||||||
* <pre>{@code
|
* <pre>{@code
|
||||||
* ObjectUtil.defaultIfNull(null, null); // = null
|
* ObjUtil.defaultIfNull(null, null); // = null
|
||||||
* ObjectUtil.defaultIfNull(null, ""); // = ""
|
* ObjUtil.defaultIfNull(null, ""); // = ""
|
||||||
* ObjectUtil.defaultIfNull(null, "zz"); // = "zz"
|
* ObjUtil.defaultIfNull(null, "zz"); // = "zz"
|
||||||
* ObjectUtil.defaultIfNull("abc", *); // = "abc"
|
* ObjUtil.defaultIfNull("abc", *); // = "abc"
|
||||||
* ObjectUtil.defaultIfNull(Boolean.TRUE, *); // = Boolean.TRUE
|
* ObjUtil.defaultIfNull(Boolean.TRUE, *); // = Boolean.TRUE
|
||||||
* }</pre>
|
* }</pre>
|
||||||
*
|
*
|
||||||
* @param <T> 对象类型
|
* @param <T> 对象类型
|
||||||
@ -299,16 +299,29 @@ public class ObjUtil {
|
|||||||
* 如果给定对象不为{@code null} 返回原值, 否则返回 {@link Supplier#get()} 提供的默认值
|
* 如果给定对象不为{@code null} 返回原值, 否则返回 {@link Supplier#get()} 提供的默认值
|
||||||
*
|
*
|
||||||
* @param <T> 被检查对象类型
|
* @param <T> 被检查对象类型
|
||||||
* @param source 被检查对象,可能为{@code null}
|
* @param object 被检查对象,可能为{@code null}
|
||||||
* @param defaultSupplier 为空时的默认值提供者
|
* @param defaultSupplier 为空时的默认值提供者
|
||||||
* @return 被检查对象不为 {@code null} 返回原值,否则返回 {@link Supplier#get()} 提供的默认值
|
* @return 被检查对象不为 {@code null} 返回原值,否则返回 {@link Supplier#get()} 提供的默认值
|
||||||
* @since 5.4.6
|
* @since 5.4.6
|
||||||
*/
|
*/
|
||||||
public static <T> T defaultIfNull(final T source, final Supplier<? extends T> defaultSupplier) {
|
public static <T> T defaultIfNull(final T object, final Supplier<? extends T> defaultSupplier) {
|
||||||
if (isNotNull(source)) {
|
return isNull(object) ? defaultSupplier.get() : object;
|
||||||
return source;
|
}
|
||||||
}
|
|
||||||
return defaultSupplier.get();
|
/**
|
||||||
|
* 如果给定对象不为{@code null} 返回自定义handler处理后的结果,否则返回默认值
|
||||||
|
*
|
||||||
|
* @param <R> 返回值类型
|
||||||
|
* @param <T> 被检查对象类型
|
||||||
|
* @param object 被检查对象,可能为{@code null}
|
||||||
|
* @param handler 非空时自定义的处理方法
|
||||||
|
* @param defaultValue 为空时的默认返回值
|
||||||
|
* @return 被检查对象不为 {@code null} 返回处理后的结果,否则返回默认值
|
||||||
|
* @since 6.0.0
|
||||||
|
*/
|
||||||
|
public static <T, R> R defaultIfNull(final T object,
|
||||||
|
final Function<? super T, ? extends R> handler, final R defaultValue) {
|
||||||
|
return isNull(object) ? defaultValue : handler.apply(object);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -316,17 +329,15 @@ public class ObjUtil {
|
|||||||
*
|
*
|
||||||
* @param <R> 返回值类型
|
* @param <R> 返回值类型
|
||||||
* @param <T> 被检查对象类型
|
* @param <T> 被检查对象类型
|
||||||
* @param source 被检查对象,可能为{@code null}
|
* @param object 被检查对象,可能为{@code null}
|
||||||
* @param handler 非空时自定义的处理方法
|
* @param handler 非空时自定义的处理方法
|
||||||
* @param defaultSupplier 为空时的默认值提供者
|
* @param defaultSupplier 为空时的默认值提供者
|
||||||
* @return 被检查对象不为 {@code null} 返回处理后的结果,否则返回 {@link Supplier#get()} 提供的默认值
|
* @return 被检查对象不为 {@code null} 返回处理后的结果,否则返回 {@link Supplier#get()} 提供的默认值
|
||||||
* @since 6.0.0
|
* @since 6.0.0
|
||||||
*/
|
*/
|
||||||
public static <T, R> R defaultIfNull(final T source, final Function<? super T, ? extends R> handler, final Supplier<? extends R> defaultSupplier) {
|
public static <T, R> R defaultIfNull(final T object,
|
||||||
if (isNotNull(source)) {
|
final Function<? super T, ? extends R> handler, final Supplier<? extends R> defaultSupplier) {
|
||||||
return handler.apply(source);
|
return isNull(object) ? defaultSupplier.get() : handler.apply(object);
|
||||||
}
|
|
||||||
return defaultSupplier.get();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -354,22 +365,6 @@ public class ObjUtil {
|
|||||||
consumer.accept(source);
|
consumer.accept(source);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 如果给定对象不为{@code null} 返回自定义handler处理后的结果,否则返回默认值
|
|
||||||
*
|
|
||||||
* @param <R> 返回值类型
|
|
||||||
* @param <T> 被检查对象类型
|
|
||||||
* @param source 被检查对象,可能为{@code null}
|
|
||||||
* @param handler 非空时自定义的处理方法
|
|
||||||
* @param defaultValue 为空时的默认返回值
|
|
||||||
* @return 被检查对象不为 {@code null} 返回处理后的结果,否则返回默认值
|
|
||||||
* @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);
|
|
||||||
}
|
|
||||||
// endregion
|
// endregion
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user