mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
fix code
This commit is contained in:
parent
c4627a339d
commit
b30bf90210
@ -115,7 +115,7 @@ public class PercentCodec implements Encoder<byte[], byte[]>, Serializable {
|
||||
*/
|
||||
public String encode(final CharSequence path, final Charset charset, final char... customSafeChar) {
|
||||
if (null == charset || StrUtil.isEmpty(path)) {
|
||||
return StrUtil.str(path);
|
||||
return StrUtil.toStringOrNull(path);
|
||||
}
|
||||
|
||||
final StringBuilder rewrittenPath = new StringBuilder(path.length() * 3);
|
||||
|
@ -15,6 +15,7 @@ package org.dromara.hutool.core.collection.iter;
|
||||
import org.dromara.hutool.core.array.ArrayUtil;
|
||||
import org.dromara.hutool.core.collection.CollUtil;
|
||||
import org.dromara.hutool.core.collection.ListUtil;
|
||||
import org.dromara.hutool.core.convert.Convert;
|
||||
import org.dromara.hutool.core.lang.Assert;
|
||||
import org.dromara.hutool.core.map.MapUtil;
|
||||
import org.dromara.hutool.core.math.NumberUtil;
|
||||
@ -906,7 +907,7 @@ public class IterUtil {
|
||||
* @since 5.8.0
|
||||
*/
|
||||
public static <E> String toStr(final Iterator<E> iterator) {
|
||||
return toStr(iterator, ObjUtil::toString);
|
||||
return toStr(iterator, Convert::toStrOrNullStr);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -206,7 +206,7 @@ public class ZipWriter implements Closeable {
|
||||
* @throws IORuntimeException IO异常
|
||||
*/
|
||||
public ZipWriter add(String path, final InputStream in) throws IORuntimeException {
|
||||
path = StrUtil.emptyIfNull(path);
|
||||
path = StrUtil.toStringOrEmpty(path);
|
||||
if (null == in) {
|
||||
// 空目录需要检查路径规范性,目录以"/"结尾
|
||||
path = StrUtil.addSuffixIfNot(path, StrUtil.SLASH);
|
||||
|
@ -77,6 +77,18 @@ public class Convert {
|
||||
return toStr(value, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换为字符串<br>
|
||||
* 如果给定的值为{@code null},或者转换失败,返回默认值"null"(即null这个字符串)<br>
|
||||
* 转换失败不会报错
|
||||
*
|
||||
* @param value 被转换的值
|
||||
* @return 结果
|
||||
*/
|
||||
public static String toStrOrNullStr(final Object value) {
|
||||
return toStr(value, StrUtil.NULL);
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换为String数组
|
||||
*
|
||||
@ -547,9 +559,9 @@ public class Convert {
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <E extends Enum<E>> E toEnum(final Class<E> clazz, final Object value, final E defaultValue) {
|
||||
try{
|
||||
try {
|
||||
return (E) EnumConverter.INSTANCE.convert(clazz, value);
|
||||
} catch (final Exception ignore){
|
||||
} catch (final Exception ignore) {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
@ -661,10 +673,10 @@ public class Convert {
|
||||
* @param className 类的字符串表示
|
||||
* @param value 值
|
||||
* @return 转换后的值
|
||||
* @since 4.0.7
|
||||
* @throws ConvertException 转换器不存在
|
||||
* @since 4.0.7
|
||||
*/
|
||||
public static <T> T convertByClassName(final String className, final Object value) throws ConvertException{
|
||||
public static <T> T convertByClassName(final String className, final Object value) throws ConvertException {
|
||||
return convert(ClassLoaderUtil.loadClass(className), value);
|
||||
}
|
||||
|
||||
@ -675,11 +687,11 @@ public class Convert {
|
||||
* @param type 类型
|
||||
* @param value 值
|
||||
* @return 转换后的值
|
||||
* @since 4.0.0
|
||||
* @throws ConvertException 转换器不存在
|
||||
* @since 4.0.0
|
||||
*/
|
||||
public static <T> T convert(final Class<T> type, final Object value) throws ConvertException{
|
||||
return convert((Type)type, value);
|
||||
public static <T> T convert(final Class<T> type, final Object value) throws ConvertException {
|
||||
return convert((Type) type, value);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -691,7 +703,7 @@ public class Convert {
|
||||
* @return 转换后的值
|
||||
* @throws ConvertException 转换器不存在
|
||||
*/
|
||||
public static <T> T convert(final TypeReference<T> reference, final Object value) throws ConvertException{
|
||||
public static <T> T convert(final TypeReference<T> reference, final Object value) throws ConvertException {
|
||||
return convert(reference.getType(), value, null);
|
||||
}
|
||||
|
||||
@ -704,7 +716,7 @@ public class Convert {
|
||||
* @return 转换后的值
|
||||
* @throws ConvertException 转换器不存在
|
||||
*/
|
||||
public static <T> T convert(final Type type, final Object value) throws ConvertException{
|
||||
public static <T> T convert(final Type type, final Object value) throws ConvertException {
|
||||
return convert(type, value, null);
|
||||
}
|
||||
|
||||
@ -720,7 +732,7 @@ public class Convert {
|
||||
* @since 4.0.0
|
||||
*/
|
||||
public static <T> T convert(final Class<T> type, final Object value, final T defaultValue) throws ConvertException {
|
||||
return convert((Type)type, value, defaultValue);
|
||||
return convert((Type) type, value, defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -783,7 +795,7 @@ public class Convert {
|
||||
try {
|
||||
return compositeConverter.convert(type, value, defaultValue);
|
||||
} catch (final Exception e) {
|
||||
if(quietly){
|
||||
if (quietly) {
|
||||
return defaultValue;
|
||||
}
|
||||
throw e;
|
||||
@ -791,6 +803,7 @@ public class Convert {
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------- 全角半角转换
|
||||
|
||||
/**
|
||||
* 半角转全角
|
||||
*
|
||||
@ -809,7 +822,7 @@ public class Convert {
|
||||
* @return 全角字符串,{@code null}返回{@code null}
|
||||
*/
|
||||
public static String toSBC(final String input, final Set<Character> notConvertSet) {
|
||||
if(StrUtil.isEmpty(input)){
|
||||
if (StrUtil.isEmpty(input)) {
|
||||
return input;
|
||||
}
|
||||
final char[] c = input.toCharArray();
|
||||
@ -847,7 +860,7 @@ public class Convert {
|
||||
*/
|
||||
@SuppressWarnings("UnnecessaryUnicodeEscape")
|
||||
public static String toDBC(final String text, final Set<Character> notConvertSet) {
|
||||
if(StrUtil.isBlank(text)) {
|
||||
if (StrUtil.isBlank(text)) {
|
||||
return text;
|
||||
}
|
||||
final char[] c = text.toCharArray();
|
||||
@ -869,6 +882,7 @@ public class Convert {
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------- hex
|
||||
|
||||
/**
|
||||
* 字符串转换成十六进制字符串,结果为小写
|
||||
*
|
||||
@ -971,13 +985,14 @@ public class Convert {
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------- 原始包装类型转换
|
||||
|
||||
/**
|
||||
* 原始类转为包装类,非原始类返回原类
|
||||
*
|
||||
* @see BasicType#wrap(Class)
|
||||
* @param clazz 原始类
|
||||
* @return 包装类
|
||||
* @see BasicType#wrap(Class)
|
||||
* @see BasicType#wrap(Class)
|
||||
*/
|
||||
public static Class<?> wrap(final Class<?> clazz) {
|
||||
return BasicType.wrap(clazz);
|
||||
@ -986,16 +1001,17 @@ public class Convert {
|
||||
/**
|
||||
* 包装类转为原始类,非包装类返回原类
|
||||
*
|
||||
* @see BasicType#unWrap(Class)
|
||||
* @param clazz 包装类
|
||||
* @return 原始类
|
||||
* @see BasicType#unWrap(Class)
|
||||
* @see BasicType#unWrap(Class)
|
||||
*/
|
||||
public static Class<?> unWrap(final Class<?> clazz) {
|
||||
return BasicType.unWrap(clazz);
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------- 数字和英文转换
|
||||
|
||||
/**
|
||||
* 将阿拉伯数字转为英文表达方式
|
||||
*
|
||||
@ -1047,7 +1063,7 @@ public class Convert {
|
||||
* @return 数字
|
||||
* @since 5.6.0
|
||||
*/
|
||||
public static BigDecimal chineseToNumber(final String number){
|
||||
public static BigDecimal chineseToNumber(final String number) {
|
||||
return ChineseNumberParser.parseFromChineseNumber(number);
|
||||
}
|
||||
|
||||
@ -1059,7 +1075,7 @@ public class Convert {
|
||||
* @since 3.2.3
|
||||
*/
|
||||
public static String digitToChinese(final Number n) {
|
||||
if(null == n) {
|
||||
if (null == n) {
|
||||
return "零";
|
||||
}
|
||||
return ChineseNumberFormatter.of()
|
||||
@ -1078,11 +1094,12 @@ public class Convert {
|
||||
* @return 返回结果以元为单位的BigDecimal类型数字
|
||||
* @since 5.8.5
|
||||
*/
|
||||
public static BigDecimal chineseMoneyToNumber(final String chineseMoneyAmount){
|
||||
public static BigDecimal chineseMoneyToNumber(final String chineseMoneyAmount) {
|
||||
return ChineseNumberParser.parseFromChineseMoney(chineseMoneyAmount);
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------- 数字转换
|
||||
|
||||
/**
|
||||
* int转byte
|
||||
*
|
||||
|
@ -228,7 +228,7 @@ public class BetweenFormatter implements Serializable {
|
||||
* @return this
|
||||
*/
|
||||
public BetweenFormatter setSeparator(final String separator) {
|
||||
this.separator = StrUtil.emptyIfNull(separator);
|
||||
this.separator = StrUtil.toStringOrEmpty(separator);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
@ -799,11 +799,13 @@ public class CalendarUtil {
|
||||
* @since 5.7.14
|
||||
*/
|
||||
public static Calendar parse(final CharSequence str, final boolean lenient, final PositionDateParser parser) {
|
||||
Assert.notBlank(str, "Date str must be not blank!");
|
||||
Assert.notNull(parser, "Parser must be not null!");
|
||||
final Calendar calendar = Calendar.getInstance(parser.getTimeZone(), parser.getLocale());
|
||||
calendar.clear();
|
||||
calendar.setLenient(lenient);
|
||||
|
||||
return parser.parse(StrUtil.str(str), new ParsePosition(0), calendar) ? calendar : null;
|
||||
return parser.parse(str.toString(), new ParsePosition(0), calendar) ? calendar : null;
|
||||
}
|
||||
// endregion
|
||||
|
||||
|
@ -69,7 +69,7 @@ public class CharSequenceResource implements Resource, Serializable {
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return StrUtil.str(this.name);
|
||||
return StrUtil.toString(this.name);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -225,7 +225,7 @@ public class ResourceUtil {
|
||||
* @return {@link URL}
|
||||
*/
|
||||
public static URL getResourceUrl(String resource, final Class<?> baseClass) {
|
||||
resource = StrUtil.emptyIfNull(resource);
|
||||
resource = StrUtil.toStringOrEmpty(resource);
|
||||
return (null != baseClass) ? baseClass.getResource(resource) : ClassLoaderUtil.getClassLoader().getResource(resource);
|
||||
}
|
||||
|
||||
|
@ -893,7 +893,7 @@ public class Validator {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
new java.net.URL(StrUtil.str(value));
|
||||
new java.net.URL(value.toString());
|
||||
} catch (final MalformedURLException e) {
|
||||
return false;
|
||||
}
|
||||
|
@ -32,7 +32,7 @@ import java.util.List;
|
||||
*/
|
||||
public class UrlPath {
|
||||
|
||||
private List<String> segments;
|
||||
private List<CharSequence> segments;
|
||||
private boolean withEngTag;
|
||||
|
||||
/**
|
||||
@ -72,7 +72,7 @@ public class UrlPath {
|
||||
*
|
||||
* @return 节点列表
|
||||
*/
|
||||
public List<String> getSegments() {
|
||||
public List<CharSequence> getSegments() {
|
||||
return ObjUtil.defaultIfNull(this.segments, ListUtil.empty());
|
||||
}
|
||||
|
||||
@ -82,7 +82,7 @@ public class UrlPath {
|
||||
* @param index 节点位置
|
||||
* @return 节点,无节点或者越界返回null
|
||||
*/
|
||||
public String getSegment(final int index) {
|
||||
public CharSequence getSegment(final int index) {
|
||||
if (null == this.segments || index >= this.segments.size()) {
|
||||
return null;
|
||||
}
|
||||
@ -154,7 +154,7 @@ public class UrlPath {
|
||||
}
|
||||
|
||||
final StringBuilder builder = new StringBuilder();
|
||||
for (final String segment : segments) {
|
||||
for (final CharSequence segment : segments) {
|
||||
// https://www.ietf.org/rfc/rfc3986.html#section-3.3
|
||||
// 此处Path中是允许有`:`的,之前理解有误,应该是相对URI的第一个segment中不允许有`:`
|
||||
builder.append(CharUtil.SLASH).append(RFC3986.SEGMENT.encode(segment, charset));
|
||||
@ -189,11 +189,10 @@ public class UrlPath {
|
||||
this.segments = new LinkedList<>();
|
||||
}
|
||||
|
||||
final String seg = StrUtil.str(segment);
|
||||
if (before) {
|
||||
this.segments.add(0, seg);
|
||||
this.segments.add(0, segment);
|
||||
} else {
|
||||
this.segments.add(seg);
|
||||
this.segments.add(segment);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -392,7 +392,7 @@ public class UrlQuery {
|
||||
final boolean isFormUrlEncoded = EncodeMode.FORM_URL_ENCODED == this.encodeMode;
|
||||
if (null != key) {
|
||||
final String actualKey = UrlDecoder.decode(key, charset, isFormUrlEncoded);
|
||||
this.query.put(actualKey, StrUtil.emptyIfNull(UrlDecoder.decode(value, charset, isFormUrlEncoded)));
|
||||
this.query.put(actualKey, StrUtil.toStringOrEmpty(UrlDecoder.decode(value, charset, isFormUrlEncoded)));
|
||||
} else if (null != value) {
|
||||
// name为空,value作为name,value赋值null
|
||||
this.query.put(UrlDecoder.decode(value, charset, isFormUrlEncoded), null);
|
||||
|
@ -213,9 +213,11 @@ public class UrlQueryUtil {
|
||||
|
||||
final Map<String, List<String>> params = new LinkedHashMap<>();
|
||||
queryMap.forEach((key, value) -> {
|
||||
final List<String> values = params.computeIfAbsent(StrUtil.str(key), k -> new ArrayList<>(1));
|
||||
if(null != key && null != value){
|
||||
final List<String> values = params.computeIfAbsent(key.toString(), k -> new ArrayList<>(1));
|
||||
// 一般是一个参数
|
||||
values.add(StrUtil.str(value));
|
||||
values.add(key.toString());
|
||||
}
|
||||
});
|
||||
return params;
|
||||
}
|
||||
|
@ -531,7 +531,7 @@ public class UrlUtil {
|
||||
if (isEncodePath) {
|
||||
path = RFC3986.PATH.encode(path, CharsetUtil.UTF_8);
|
||||
}
|
||||
return protocol + domain + StrUtil.emptyIfNull(path) + StrUtil.emptyIfNull(params);
|
||||
return protocol + domain + StrUtil.toStringOrEmpty(path) + StrUtil.toStringOrEmpty(params);
|
||||
}
|
||||
// endregion
|
||||
|
||||
|
@ -231,7 +231,7 @@ public class ClassScanner implements Serializable {
|
||||
* @param charset 编码
|
||||
*/
|
||||
public ClassScanner(String packageName, final Predicate<Class<?>> classPredicate, final Charset charset) {
|
||||
packageName = StrUtil.emptyIfNull(packageName);
|
||||
packageName = StrUtil.toStringOrEmpty(packageName);
|
||||
this.packageName = packageName;
|
||||
this.packageNameWithDot = StrUtil.addSuffixIfNot(packageName, StrUtil.DOT);
|
||||
this.packageDirName = packageName.replace(CharUtil.DOT, File.separatorChar);
|
||||
|
@ -381,8 +381,8 @@ public class ReUtil {
|
||||
* @return 删除后剩余的内容
|
||||
*/
|
||||
public static String delFirst(final String regex, final CharSequence content) {
|
||||
if (ArrayUtil.hasBlank(regex, content)) {
|
||||
return StrUtil.str(content);
|
||||
if (StrUtil.hasEmpty(regex, content)) {
|
||||
return StrUtil.toStringOrNull(content);
|
||||
}
|
||||
|
||||
final Pattern pattern = PatternPool.get(regex, Pattern.DOTALL);
|
||||
@ -411,7 +411,7 @@ public class ReUtil {
|
||||
*/
|
||||
public static String replaceFirst(final Pattern pattern, final CharSequence content, final String replacement) {
|
||||
if (null == pattern || StrUtil.isEmpty(content)) {
|
||||
return StrUtil.str(content);
|
||||
return StrUtil.toStringOrNull(content);
|
||||
}
|
||||
|
||||
return pattern.matcher(content).replaceFirst(replacement);
|
||||
@ -426,8 +426,8 @@ public class ReUtil {
|
||||
* @since 5.6.5
|
||||
*/
|
||||
public static String delLast(final String regex, final CharSequence str) {
|
||||
if (ArrayUtil.hasBlank(regex, str)) {
|
||||
return StrUtil.str(str);
|
||||
if (StrUtil.isEmpty(regex) || StrUtil.isEmpty(str)) {
|
||||
return StrUtil.toStringOrNull(str);
|
||||
}
|
||||
|
||||
final Pattern pattern = PatternPool.get(regex, Pattern.DOTALL);
|
||||
@ -450,7 +450,7 @@ public class ReUtil {
|
||||
}
|
||||
}
|
||||
|
||||
return StrUtil.str(str);
|
||||
return StrUtil.toStringOrNull(str);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -462,7 +462,7 @@ public class ReUtil {
|
||||
*/
|
||||
public static String delAll(final String regex, final CharSequence content) {
|
||||
if (StrUtil.hasEmpty(regex, content)) {
|
||||
return StrUtil.str(content);
|
||||
return StrUtil.toStringOrNull(content);
|
||||
}
|
||||
|
||||
final Pattern pattern = PatternPool.get(regex, Pattern.DOTALL);
|
||||
@ -478,7 +478,7 @@ public class ReUtil {
|
||||
*/
|
||||
public static String delAll(final Pattern pattern, final CharSequence content) {
|
||||
if (null == pattern || StrUtil.isEmpty(content)) {
|
||||
return StrUtil.str(content);
|
||||
return StrUtil.toStringOrNull(content);
|
||||
}
|
||||
|
||||
return pattern.matcher(content).replaceAll(StrUtil.EMPTY);
|
||||
@ -493,7 +493,7 @@ public class ReUtil {
|
||||
*/
|
||||
public static String delPre(final String regex, final CharSequence content) {
|
||||
if (null == content || null == regex) {
|
||||
return StrUtil.str(content);
|
||||
return StrUtil.toStringOrNull(content);
|
||||
}
|
||||
|
||||
final Pattern pattern = PatternPool.get(regex, Pattern.DOTALL);
|
||||
@ -508,15 +508,14 @@ public class ReUtil {
|
||||
* @return 删除前缀后的新内容
|
||||
*/
|
||||
public static String delPre(final Pattern pattern, final CharSequence content) {
|
||||
if (null == content || null == pattern) {
|
||||
return StrUtil.str(content);
|
||||
}
|
||||
|
||||
if (null != pattern && StrUtil.isNotEmpty(content)) {
|
||||
final Matcher matcher = pattern.matcher(content);
|
||||
if (matcher.find()) {
|
||||
return StrUtil.sub(content, matcher.end(), content.length());
|
||||
}
|
||||
return StrUtil.str(content);
|
||||
}
|
||||
|
||||
return StrUtil.toStringOrNull(content);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -873,13 +872,7 @@ public class ReUtil {
|
||||
* @since 3.0.4
|
||||
*/
|
||||
public static String replaceAll(final CharSequence content, final Pattern pattern, final String replacementTemplate) {
|
||||
if (StrUtil.isEmpty(content)) {
|
||||
return StrUtil.str(content);
|
||||
}
|
||||
|
||||
// replacementTemplate字段不能为null,否则无法抉择如何处理结果
|
||||
Assert.notNull(replacementTemplate, "ReplacementTemplate must be not null !");
|
||||
|
||||
if(null != pattern && StrUtil.isNotEmpty(content) && StrUtil.isNotEmpty(replacementTemplate)){
|
||||
final Matcher matcher = pattern.matcher(content);
|
||||
boolean result = matcher.find();
|
||||
if (result) {
|
||||
@ -898,7 +891,9 @@ public class ReUtil {
|
||||
matcher.appendTail(sb);
|
||||
return sb.toString();
|
||||
}
|
||||
return StrUtil.str(content);
|
||||
}
|
||||
|
||||
return StrUtil.toStringOrNull(replacementTemplate);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -933,9 +928,13 @@ public class ReUtil {
|
||||
* @return 替换后的字符串
|
||||
* @since 4.2.2
|
||||
*/
|
||||
public static String replaceAll(final CharSequence str, final Pattern pattern, final SerFunction<Matcher, String> replaceFun) {
|
||||
if (StrUtil.isEmpty(str)) {
|
||||
return StrUtil.str(str);
|
||||
public static String replaceAll(final CharSequence str, final Pattern pattern, SerFunction<Matcher, String> replaceFun) {
|
||||
if (null == pattern || StrUtil.isEmpty(str)) {
|
||||
return StrUtil.toStringOrNull(str);
|
||||
}
|
||||
|
||||
if(null == replaceFun){
|
||||
replaceFun = Matcher::group;
|
||||
}
|
||||
|
||||
final Matcher matcher = pattern.matcher(str);
|
||||
@ -970,7 +969,7 @@ public class ReUtil {
|
||||
*/
|
||||
public static String escape(final CharSequence content) {
|
||||
if (StrUtil.isBlank(content)) {
|
||||
return StrUtil.str(content);
|
||||
return StrUtil.toStringOrNull(content);
|
||||
}
|
||||
|
||||
final StringBuilder builder = new StringBuilder();
|
||||
|
@ -30,7 +30,6 @@ import org.dromara.hutool.core.text.replacer.SearchReplacer;
|
||||
import org.dromara.hutool.core.text.split.SplitUtil;
|
||||
import org.dromara.hutool.core.util.ByteUtil;
|
||||
import org.dromara.hutool.core.util.CharsetUtil;
|
||||
import org.dromara.hutool.core.util.ObjUtil;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.CharBuffer;
|
||||
@ -94,28 +93,46 @@ public class CharSequenceUtil extends StrValidator {
|
||||
*/
|
||||
public static final String SPACE = " ";
|
||||
|
||||
// region toString
|
||||
|
||||
/**
|
||||
* {@link CharSequence} 转为字符串,null安全
|
||||
* 调用对象的toString方法,null会返回“null”
|
||||
*
|
||||
* @param cs {@link CharSequence}
|
||||
* @param obj 对象
|
||||
* @return 字符串
|
||||
* @since 4.1.3
|
||||
* @see String#valueOf(Object)
|
||||
*/
|
||||
public static String str(final CharSequence cs) {
|
||||
return null == cs ? null : cs.toString();
|
||||
public static String toString(final Object obj) {
|
||||
return String.valueOf(obj);
|
||||
}
|
||||
|
||||
/**
|
||||
* 调用对象的toString方法,{@code null}会返回{@code null}
|
||||
*
|
||||
* @param obj 对象
|
||||
* @return 字符串 or {@code null}
|
||||
* @since 5.7.17
|
||||
*/
|
||||
public static String toStringOrNull(final Object obj) {
|
||||
return null == obj ? null : obj.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 调用对象的toString方法,{@code null}会返回空字符串 ""
|
||||
*
|
||||
* @param obj 对象
|
||||
* @return {@link String }
|
||||
* @author Junwei Xu
|
||||
*/
|
||||
public static String toStringOrEmpty(final Object obj) {
|
||||
// obj为空时, 返回 null 或 "null" 都不适用部分场景, 此处返回 "" 空字符串
|
||||
return null == obj ? EMPTY : obj.toString();
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
// region ----- defaultIf
|
||||
|
||||
/**
|
||||
* 当给定字符串为null时,转换为Empty
|
||||
*
|
||||
* @param str 被转换的字符串
|
||||
* @return 转换后的字符串
|
||||
*/
|
||||
public static String emptyIfNull(final CharSequence str) {
|
||||
return ObjUtil.defaultIfNull(str, EMPTY).toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 当给定字符串为空字符串时,转换为{@code null}
|
||||
*
|
||||
@ -215,9 +232,9 @@ public class CharSequenceUtil extends StrValidator {
|
||||
* 注意,和{@link String#trim()}不同,此方法使用{@link CharUtil#isBlankChar(char)} 来判定空白, 因而可以除去英文字符集之外的其它空白,如中文空格。
|
||||
* <ul>
|
||||
* <li>去除字符串空格罗列相关如下:</li>
|
||||
* <li>{@link StrUtil#trimPrefix(CharSequence)}去除头部空格</li>
|
||||
* <li>{@link StrUtil#trimSuffix(CharSequence)}去除尾部空格</li>
|
||||
* <li>{@link StrUtil#cleanBlank(CharSequence)}去除头部、尾部、中间空格</li>
|
||||
* <li>{@link #trimPrefix(CharSequence)}去除头部空格</li>
|
||||
* <li>{@link #trimSuffix(CharSequence)}去除尾部空格</li>
|
||||
* <li>{@link #cleanBlank(CharSequence)}去除头部、尾部、中间空格</li>
|
||||
* </ul>
|
||||
*
|
||||
* <pre>
|
||||
@ -653,7 +670,7 @@ public class CharSequenceUtil extends StrValidator {
|
||||
* <li>str 不是 null,testChars 是 null,直接返回 false</li>
|
||||
* </ul>
|
||||
* <pre>{@code
|
||||
* StrUtil.containsOnly("asdas", 'a', 'd', 's','l'); --> true
|
||||
* CharSequenceUtil.containsOnly("asdas", 'a', 'd', 's','l'); --> true
|
||||
* }</pre>
|
||||
*
|
||||
* @param str 字符串
|
||||
@ -1035,7 +1052,7 @@ public class CharSequenceUtil extends StrValidator {
|
||||
public static String removeAll(final CharSequence str, final CharSequence strToRemove) {
|
||||
// strToRemove如果为空, 也不用继续后面的逻辑
|
||||
if (isEmpty(str) || isEmpty(strToRemove)) {
|
||||
return str(str);
|
||||
return toStringOrNull(str);
|
||||
}
|
||||
return str.toString().replace(strToRemove, EMPTY);
|
||||
}
|
||||
@ -1050,7 +1067,7 @@ public class CharSequenceUtil extends StrValidator {
|
||||
* @since 5.3.8
|
||||
*/
|
||||
public static String removeAny(final CharSequence str, final CharSequence... strsToRemove) {
|
||||
String result = str(str);
|
||||
String result = toStringOrNull(str);
|
||||
if (isNotEmpty(str)) {
|
||||
for (final CharSequence strToRemove : strsToRemove) {
|
||||
result = removeAll(result, strToRemove);
|
||||
@ -1068,8 +1085,8 @@ public class CharSequenceUtil extends StrValidator {
|
||||
* @since 4.2.2
|
||||
*/
|
||||
public static String removeAll(final CharSequence str, final char... chars) {
|
||||
if (null == str || ArrayUtil.isEmpty(chars)) {
|
||||
return str(str);
|
||||
if (StrUtil.isEmpty(str) || ArrayUtil.isEmpty(chars)) {
|
||||
return toStringOrNull(str);
|
||||
}
|
||||
return filter(str, (c) -> !ArrayUtil.contains(chars, c));
|
||||
}
|
||||
@ -1157,7 +1174,7 @@ public class CharSequenceUtil extends StrValidator {
|
||||
*/
|
||||
public static String removePrefix(final CharSequence str, final CharSequence prefix, final boolean ignoreCase) {
|
||||
if (isEmpty(str) || isEmpty(prefix)) {
|
||||
return str(str);
|
||||
return toStringOrNull(str);
|
||||
}
|
||||
|
||||
final String str2 = str.toString();
|
||||
@ -1176,7 +1193,7 @@ public class CharSequenceUtil extends StrValidator {
|
||||
*/
|
||||
public static String removeSuffix(final CharSequence str, final CharSequence suffix) {
|
||||
if (isEmpty(str) || isEmpty(suffix)) {
|
||||
return str(str);
|
||||
return toStringOrNull(str);
|
||||
}
|
||||
|
||||
final String str2 = str.toString();
|
||||
@ -1206,7 +1223,7 @@ public class CharSequenceUtil extends StrValidator {
|
||||
*/
|
||||
public static String removeSuffixIgnoreCase(final CharSequence str, final CharSequence suffix) {
|
||||
if (isEmpty(str) || isEmpty(suffix)) {
|
||||
return str(str);
|
||||
return toStringOrNull(str);
|
||||
}
|
||||
|
||||
final String str2 = str.toString();
|
||||
@ -1257,7 +1274,7 @@ public class CharSequenceUtil extends StrValidator {
|
||||
*/
|
||||
public static String strip(final CharSequence str, final CharSequence prefix, final CharSequence suffix) {
|
||||
if (isEmpty(str)) {
|
||||
return str(str);
|
||||
return toStringOrNull(str);
|
||||
}
|
||||
|
||||
int from = 0;
|
||||
@ -1297,7 +1314,7 @@ public class CharSequenceUtil extends StrValidator {
|
||||
*/
|
||||
public static String stripIgnoreCase(final CharSequence str, final CharSequence prefix, final CharSequence suffix) {
|
||||
if (isEmpty(str)) {
|
||||
return str(str);
|
||||
return toStringOrNull(str);
|
||||
}
|
||||
int from = 0;
|
||||
int to = str.length();
|
||||
@ -1384,7 +1401,7 @@ public class CharSequenceUtil extends StrValidator {
|
||||
*/
|
||||
public static String sub(final CharSequence str, int fromIndexInclude, int toIndexExclude) {
|
||||
if (isEmpty(str)) {
|
||||
return str(str);
|
||||
return toStringOrNull(str);
|
||||
}
|
||||
final int len = str.length();
|
||||
|
||||
@ -1429,7 +1446,7 @@ public class CharSequenceUtil extends StrValidator {
|
||||
*/
|
||||
public static String subByCodePoint(final CharSequence str, final int fromIndex, final int toIndex) {
|
||||
if (isEmpty(str)) {
|
||||
return str(str);
|
||||
return toStringOrNull(str);
|
||||
}
|
||||
|
||||
if (fromIndex < 0 || fromIndex > toIndex) {
|
||||
@ -1471,7 +1488,7 @@ public class CharSequenceUtil extends StrValidator {
|
||||
*/
|
||||
public static String subPreGbk(final CharSequence str, int len, final boolean halfUp) {
|
||||
if (isEmpty(str)) {
|
||||
return str(str);
|
||||
return toStringOrNull(str);
|
||||
}
|
||||
|
||||
int counterOfDoubleByte = 0;
|
||||
@ -2214,7 +2231,7 @@ public class CharSequenceUtil extends StrValidator {
|
||||
* @return 包装后的字符串
|
||||
*/
|
||||
public static String wrap(final CharSequence str, final CharSequence prefix, final CharSequence suffix) {
|
||||
return emptyIfNull(prefix).concat(emptyIfNull(str)).concat(emptyIfNull(suffix));
|
||||
return toStringOrEmpty(prefix).concat(toStringOrEmpty(str)).concat(toStringOrEmpty(suffix));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -2226,7 +2243,7 @@ public class CharSequenceUtil extends StrValidator {
|
||||
* @return 包装后的字符串
|
||||
*/
|
||||
public static String wrap(final CharSequence str, final char prefix, final char suffix) {
|
||||
return prefix + emptyIfNull(str) + suffix;
|
||||
return prefix + toStringOrEmpty(str) + suffix;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -2347,7 +2364,7 @@ public class CharSequenceUtil extends StrValidator {
|
||||
*/
|
||||
public static String unWrap(final CharSequence str, final char prefix, final char suffix) {
|
||||
if (isEmpty(str)) {
|
||||
return str(str);
|
||||
return toStringOrNull(str);
|
||||
}
|
||||
if (str.charAt(0) == prefix && str.charAt(str.length() - 1) == suffix) {
|
||||
return sub(str, 1, str.length() - 1);
|
||||
@ -2595,7 +2612,7 @@ public class CharSequenceUtil extends StrValidator {
|
||||
*/
|
||||
public static String center(CharSequence str, final int size, final char padChar) {
|
||||
if (str == null || size <= 0) {
|
||||
return str(str);
|
||||
return toStringOrNull(str);
|
||||
}
|
||||
final int strLen = str.length();
|
||||
final int pads = size - strLen;
|
||||
@ -2629,7 +2646,7 @@ public class CharSequenceUtil extends StrValidator {
|
||||
*/
|
||||
public static String center(CharSequence str, final int size, CharSequence padStr) {
|
||||
if (str == null || size <= 0) {
|
||||
return str(str);
|
||||
return toStringOrNull(str);
|
||||
}
|
||||
if (isEmpty(padStr)) {
|
||||
padStr = SPACE;
|
||||
@ -2796,7 +2813,7 @@ public class CharSequenceUtil extends StrValidator {
|
||||
* @since 4.0.2
|
||||
*/
|
||||
public static int compareVersion(final CharSequence version1, final CharSequence version2) {
|
||||
return VersionComparator.INSTANCE.compare(str(version1), str(version2));
|
||||
return VersionComparator.INSTANCE.compare(toStringOrNull(version1), toStringOrNull(version2));
|
||||
}
|
||||
// endregion
|
||||
|
||||
@ -2842,7 +2859,7 @@ public class CharSequenceUtil extends StrValidator {
|
||||
*/
|
||||
public static String appendIfMissing(final CharSequence str, final CharSequence suffix, final boolean ignoreCase, final CharSequence... testSuffixes) {
|
||||
if (str == null || isEmpty(suffix) || endWith(str, suffix, ignoreCase)) {
|
||||
return str(str);
|
||||
return toStringOrNull(str);
|
||||
}
|
||||
if (ArrayUtil.isNotEmpty(testSuffixes)) {
|
||||
for (final CharSequence testSuffix : testSuffixes) {
|
||||
@ -2894,7 +2911,7 @@ public class CharSequenceUtil extends StrValidator {
|
||||
*/
|
||||
public static String prependIfMissing(final CharSequence str, final CharSequence prefix, final boolean ignoreCase, final CharSequence... prefixes) {
|
||||
if (str == null || isEmpty(prefix) || startWith(str, prefix, ignoreCase)) {
|
||||
return str(str);
|
||||
return toStringOrNull(str);
|
||||
}
|
||||
if (ArrayUtil.isNotEmpty(prefixes)) {
|
||||
for (final CharSequence s : prefixes) {
|
||||
@ -2920,11 +2937,11 @@ public class CharSequenceUtil extends StrValidator {
|
||||
*/
|
||||
public static String replaceFirst(final CharSequence str, final CharSequence searchStr, final CharSequence replacedStr, final boolean ignoreCase) {
|
||||
if (isEmpty(str)) {
|
||||
return str(str);
|
||||
return toStringOrNull(str);
|
||||
}
|
||||
final int startInclude = indexOf(str, searchStr, 0, ignoreCase);
|
||||
if (INDEX_NOT_FOUND == startInclude) {
|
||||
return str(str);
|
||||
return toStringOrNull(str);
|
||||
}
|
||||
return replaceByCodePoint(str, startInclude, startInclude + searchStr.length(), replacedStr);
|
||||
}
|
||||
@ -2940,11 +2957,11 @@ public class CharSequenceUtil extends StrValidator {
|
||||
*/
|
||||
public static String replaceLast(final CharSequence str, final CharSequence searchStr, final CharSequence replacedStr, final boolean ignoreCase) {
|
||||
if (isEmpty(str)) {
|
||||
return str(str);
|
||||
return toStringOrNull(str);
|
||||
}
|
||||
final int lastIndex = lastIndexOf(str, searchStr, str.length(), ignoreCase);
|
||||
if (INDEX_NOT_FOUND == lastIndex) {
|
||||
return str(str);
|
||||
return toStringOrNull(str);
|
||||
}
|
||||
return replace(str, lastIndex, searchStr, replacedStr, ignoreCase);
|
||||
}
|
||||
@ -3003,7 +3020,7 @@ public class CharSequenceUtil extends StrValidator {
|
||||
*/
|
||||
public static String replace(final CharSequence str, final int fromIndex, final CharSequence searchStr, final CharSequence replacement, final boolean ignoreCase) {
|
||||
if (isEmpty(str) || isEmpty(searchStr)) {
|
||||
return str(str);
|
||||
return toStringOrNull(str);
|
||||
}
|
||||
return new SearchReplacer(fromIndex, searchStr, replacement, ignoreCase).apply(str);
|
||||
}
|
||||
@ -3108,7 +3125,7 @@ public class CharSequenceUtil extends StrValidator {
|
||||
*/
|
||||
public static String replaceChars(final CharSequence str, final String chars, final CharSequence replacedStr) {
|
||||
if (isEmpty(str) || isEmpty(chars)) {
|
||||
return str(str);
|
||||
return toStringOrNull(str);
|
||||
}
|
||||
return replaceChars(str, chars.toCharArray(), replacedStr);
|
||||
}
|
||||
@ -3124,7 +3141,7 @@ public class CharSequenceUtil extends StrValidator {
|
||||
*/
|
||||
public static String replaceChars(final CharSequence str, final char[] chars, final CharSequence replacedStr) {
|
||||
if (isEmpty(str) || ArrayUtil.isEmpty(chars)) {
|
||||
return str(str);
|
||||
return toStringOrNull(str);
|
||||
}
|
||||
|
||||
final Set<Character> set = new HashSet<>(chars.length);
|
||||
@ -3281,11 +3298,11 @@ public class CharSequenceUtil extends StrValidator {
|
||||
final int factor, final boolean appendDots) {
|
||||
//字符数*速算因子<=最大字节数
|
||||
if (str == null || str.length() * factor <= maxBytesLength) {
|
||||
return str(str);
|
||||
return toStringOrNull(str);
|
||||
}
|
||||
final byte[] sba = ByteUtil.toBytes(str, charset);
|
||||
if (sba.length <= maxBytesLength) {
|
||||
return str(str);
|
||||
return toStringOrNull(str);
|
||||
}
|
||||
//限制字节数
|
||||
final int limitBytes;
|
||||
@ -3430,7 +3447,7 @@ public class CharSequenceUtil extends StrValidator {
|
||||
*/
|
||||
public static String filter(final CharSequence str, final Predicate<Character> predicate) {
|
||||
if (str == null || predicate == null) {
|
||||
return str(str);
|
||||
return toStringOrNull(str);
|
||||
}
|
||||
|
||||
final int len = str.length();
|
||||
@ -3638,7 +3655,7 @@ public class CharSequenceUtil extends StrValidator {
|
||||
public static String concat(final boolean isNullToEmpty, final CharSequence... strs) {
|
||||
final StringBuilder sb = new StringBuilder();
|
||||
for (final CharSequence str : strs) {
|
||||
sb.append(isNullToEmpty ? emptyIfNull(str) : str);
|
||||
sb.append(isNullToEmpty ? toStringOrEmpty(str) : str);
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
@ -3694,6 +3711,7 @@ public class CharSequenceUtil extends StrValidator {
|
||||
}
|
||||
|
||||
// region ----- join
|
||||
|
||||
/**
|
||||
* 以 conjunction 为分隔符将多个对象转换为字符串
|
||||
*
|
||||
@ -3747,7 +3765,7 @@ public class CharSequenceUtil extends StrValidator {
|
||||
*/
|
||||
public static String move(final CharSequence str, final int startInclude, final int endExclude, int moveLength) {
|
||||
if (isEmpty(str)) {
|
||||
return str(str);
|
||||
return toStringOrNull(str);
|
||||
}
|
||||
final int len = str.length();
|
||||
if (Math.abs(moveLength) > len) {
|
||||
@ -3768,7 +3786,7 @@ public class CharSequenceUtil extends StrValidator {
|
||||
.append(str.subSequence(startAfterMove, startInclude))//
|
||||
.append(str.subSequence(endExclude, str.length()));
|
||||
} else {
|
||||
return str(str);
|
||||
return toStringOrNull(str);
|
||||
}
|
||||
return strBuilder.toString();
|
||||
}
|
||||
@ -3865,4 +3883,19 @@ public class CharSequenceUtil extends StrValidator {
|
||||
}
|
||||
return str1.subSequence(str1Index + 1, str1.length());
|
||||
}
|
||||
|
||||
/**
|
||||
* 将字符串转换为字符数组
|
||||
*
|
||||
* @param str 字符串
|
||||
* @param isCodePoint 是否为Unicode码点(即支持emoji等多char字符)
|
||||
* @return 字符数组
|
||||
* @since 6.0.0
|
||||
*/
|
||||
public static int[] toChars(final CharSequence str, final boolean isCodePoint) {
|
||||
if (null == str) {
|
||||
return null;
|
||||
}
|
||||
return (isCodePoint ? str.codePoints() : str.chars()).toArray();
|
||||
}
|
||||
}
|
||||
|
@ -14,6 +14,7 @@ package org.dromara.hutool.core.text;
|
||||
|
||||
import org.dromara.hutool.core.collection.iter.ArrayIter;
|
||||
import org.dromara.hutool.core.collection.iter.IterUtil;
|
||||
import org.dromara.hutool.core.convert.Convert;
|
||||
import org.dromara.hutool.core.io.IORuntimeException;
|
||||
import org.dromara.hutool.core.array.ArrayUtil;
|
||||
import org.dromara.hutool.core.util.ObjUtil;
|
||||
@ -234,7 +235,7 @@ public class StrJoiner implements Appendable, Serializable {
|
||||
final Map.Entry<?, ?> entry = (Map.Entry<?, ?>) obj;
|
||||
append(entry.getKey()).append(entry.getValue());
|
||||
} else {
|
||||
append(ObjUtil.toString(obj));
|
||||
append(Convert.toStr(obj));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
@ -56,7 +56,7 @@ public class StrTrimer implements UnaryOperator<CharSequence>, Serializable {
|
||||
@Override
|
||||
public String apply(final CharSequence str) {
|
||||
if (StrUtil.isEmpty(str)) {
|
||||
return StrUtil.str(str);
|
||||
return StrUtil.toStringOrNull(str);
|
||||
}
|
||||
|
||||
final int length = str.length();
|
||||
|
@ -241,41 +241,6 @@ public class StrUtil extends CharSequenceUtil implements StrPool {
|
||||
}
|
||||
// endregion
|
||||
|
||||
/**
|
||||
* 调用对象的toString方法,null会返回“null”
|
||||
*
|
||||
* @param obj 对象
|
||||
* @return 字符串
|
||||
* @since 4.1.3
|
||||
* @see String#valueOf(Object)
|
||||
*/
|
||||
public static String toString(final Object obj) {
|
||||
return String.valueOf(obj);
|
||||
}
|
||||
|
||||
/**
|
||||
* 调用对象的toString方法,{@code null}会返回{@code null}
|
||||
*
|
||||
* @param obj 对象
|
||||
* @return 字符串 or {@code null}
|
||||
* @since 5.7.17
|
||||
*/
|
||||
public static String toStringOrNull(final Object obj) {
|
||||
return null == obj ? null : obj.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 调用对象的toString方法,{@code null}会返回空字符串 ""
|
||||
*
|
||||
* @param obj 对象
|
||||
* @return {@link String }
|
||||
* @author Junwei Xu
|
||||
*/
|
||||
public static String toStringOrEmpty(final Object obj) {
|
||||
// obj为空时, 返回 null 或 "null" 都不适用部分场景, 此处返回 "" 空字符串
|
||||
return null == obj ? EMPTY : obj.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建StringBuilder对象
|
||||
*
|
||||
|
@ -182,6 +182,68 @@ public class StrValidator {
|
||||
return !isEmpty(str);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>指定字符串数组中,是否包含空字符串。</p>
|
||||
* <p>如果指定的字符串数组的长度为 0,或者其中的任意一个元素是空字符串,则返回 true。</p>
|
||||
* <br>
|
||||
*
|
||||
* <p>例:</p>
|
||||
* <ul>
|
||||
* <li>{@code hasBlank() // true}</li>
|
||||
* <li>{@code hasBlank("", null, " ") // true}</li>
|
||||
* <li>{@code hasBlank("123", " ") // true}</li>
|
||||
* <li>{@code hasBlank("123", "abc") // false}</li>
|
||||
* </ul>
|
||||
*
|
||||
* <p>注意:该方法与 {@link #isAllBlank(CharSequence...)} 的区别在于:</p>
|
||||
* <ul>
|
||||
* <li>hasBlank(CharSequence...) 等价于 {@code isBlank(...) || isBlank(...) || ...}</li>
|
||||
* <li>{@link #isAllBlank(CharSequence...)} 等价于 {@code isBlank(...) && isBlank(...) && ...}</li>
|
||||
* </ul>
|
||||
*
|
||||
* @param strs 字符串列表
|
||||
* @return 是否包含空字符串
|
||||
*/
|
||||
public static boolean hasBlank(final CharSequence... strs) {
|
||||
return ArrayUtil.hasBlank(strs);
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否存都不为{@code null}或空对象或空白符的对象,通过{@link #hasBlank(CharSequence...)} 判断元素
|
||||
*
|
||||
* @param args 被检查的对象,一个或者多个
|
||||
* @return 是否都不为空
|
||||
*/
|
||||
public static boolean isAllNotBlank(final CharSequence... args) {
|
||||
return ArrayUtil.isAllNotBlank(args);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>指定字符串数组中的元素,是否全部为空字符串。</p>
|
||||
* <p>如果指定的字符串数组的长度为 0,或者所有元素都是空字符串,则返回 true。</p>
|
||||
* <br>
|
||||
*
|
||||
* <p>例:</p>
|
||||
* <ul>
|
||||
* <li>{@code isAllBlank() // true}</li>
|
||||
* <li>{@code isAllBlank("", null, " ") // true}</li>
|
||||
* <li>{@code isAllBlank("123", " ") // false}</li>
|
||||
* <li>{@code isAllBlank("123", "abc") // false}</li>
|
||||
* </ul>
|
||||
*
|
||||
* <p>注意:该方法与 {@link #hasBlank(CharSequence...)} 的区别在于:</p>
|
||||
* <ul>
|
||||
* <li>{@link #hasBlank(CharSequence...)} 等价于 {@code isBlank(...) || isBlank(...) || ...}</li>
|
||||
* <li>isAllBlank(CharSequence...) 等价于 {@code isBlank(...) && isBlank(...) && ...}</li>
|
||||
* </ul>
|
||||
*
|
||||
* @param strs 字符串列表
|
||||
* @return 所有字符串是否为空白
|
||||
*/
|
||||
public static boolean isAllBlank(final CharSequence... strs) {
|
||||
return ArrayUtil.isAllBlank(strs);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>是否包含空字符串。</p>
|
||||
* <p>如果指定的字符串数组的长度为 0,或者其中的任意一个元素是空字符串,则返回 true。</p>
|
||||
|
@ -124,7 +124,7 @@ public class EscapeUtil {
|
||||
*/
|
||||
public static String escape(final CharSequence content, final Predicate<Character> filter) {
|
||||
if (StrUtil.isEmpty(content)) {
|
||||
return StrUtil.str(content);
|
||||
return StrUtil.toStringOrNull(content);
|
||||
}
|
||||
|
||||
final StringBuilder tmp = new StringBuilder(content.length() * 6);
|
||||
|
@ -46,11 +46,11 @@ public class RangeReplacerByChar extends StrReplacer {
|
||||
@Override
|
||||
public String apply(final CharSequence str) {
|
||||
if (StrUtil.isEmpty(str)) {
|
||||
return StrUtil.str(str);
|
||||
return StrUtil.toStringOrNull(str);
|
||||
}
|
||||
|
||||
final String originalStr = StrUtil.str(str);
|
||||
final int[] chars = (isCodePoint ? originalStr.codePoints() : originalStr.chars()).toArray();
|
||||
final String originalStr = str.toString();
|
||||
final int[] chars = StrUtil.toChars(originalStr, this.isCodePoint);
|
||||
final int strLength = chars.length;
|
||||
|
||||
final int beginInclude = this.beginInclude;
|
||||
|
@ -46,11 +46,11 @@ public class RangeReplacerByStr extends StrReplacer {
|
||||
@Override
|
||||
public String apply(final CharSequence str) {
|
||||
if (StrUtil.isEmpty(str)) {
|
||||
return StrUtil.str(str);
|
||||
return StrUtil.toStringOrNull(str);
|
||||
}
|
||||
|
||||
final String originalStr = StrUtil.str(str);
|
||||
final int[] chars = (isCodePoint ? originalStr.codePoints() : originalStr.chars()).toArray();
|
||||
final String originalStr = str.toString();
|
||||
final int[] chars = StrUtil.toChars(originalStr, this.isCodePoint);
|
||||
final int strLength = chars.length;
|
||||
|
||||
final int beginInclude = this.beginInclude;
|
||||
|
@ -46,20 +46,20 @@ public class SearchReplacer extends StrReplacer {
|
||||
this.fromIndex = Math.max(fromIndex, 0);
|
||||
this.searchStr = Assert.notEmpty(searchStr, "'searchStr' must be not empty!");
|
||||
this.searchStrLength = searchStr.length();
|
||||
this.replacement = StrUtil.emptyIfNull(replacement);
|
||||
this.replacement = StrUtil.toStringOrEmpty(replacement);
|
||||
this.ignoreCase = ignoreCase;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String apply(final CharSequence str) {
|
||||
if (StrUtil.isEmpty(str)) {
|
||||
return StrUtil.str(str);
|
||||
return StrUtil.toStringOrNull(str);
|
||||
}
|
||||
|
||||
final int strLength = str.length();
|
||||
if (strLength < this.searchStrLength) {
|
||||
// issue#I4M16G@Gitee
|
||||
return StrUtil.str(str);
|
||||
return StrUtil.toStringOrNull(str);
|
||||
}
|
||||
|
||||
final int fromIndex = this.fromIndex;
|
||||
|
@ -15,7 +15,6 @@ package org.dromara.hutool.core.util;
|
||||
import org.dromara.hutool.core.array.ArrayUtil;
|
||||
import org.dromara.hutool.core.collection.CollUtil;
|
||||
import org.dromara.hutool.core.collection.iter.IterUtil;
|
||||
import org.dromara.hutool.core.convert.Convert;
|
||||
import org.dromara.hutool.core.exception.HutoolException;
|
||||
import org.dromara.hutool.core.io.SerializeUtil;
|
||||
import org.dromara.hutool.core.map.MapUtil;
|
||||
@ -500,27 +499,4 @@ public class ObjUtil {
|
||||
public static Class<?> getTypeArgument(final Object obj, final int index) {
|
||||
return ClassUtil.getTypeArgument(obj.getClass(), index);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>将对象转为字符串
|
||||
* <ul>
|
||||
* <li>若对象为{@code null},则返回“null”;</li>
|
||||
* <li>若对象为{@link Map},则返回{@code Map.toString()};</li>
|
||||
* <li>若对象为其他类型,则调用{@link Convert#toStr(Object)}进行转换;</li>
|
||||
* </ul>
|
||||
*
|
||||
* @param obj Bean对象
|
||||
* @return 转换后的字符串
|
||||
* @see Convert#toStr(Object)
|
||||
* @since 3.2.0
|
||||
*/
|
||||
public static String toString(final Object obj) {
|
||||
if (null == obj) {
|
||||
return StrUtil.NULL;
|
||||
}
|
||||
if (obj instanceof Map) {
|
||||
return obj.toString();
|
||||
}
|
||||
return Convert.toStr(obj);
|
||||
}
|
||||
}
|
||||
|
@ -972,12 +972,12 @@ public class XmlUtil extends XmlConstants {
|
||||
*
|
||||
* @param doc {@link Document}
|
||||
* @param node 节点
|
||||
* @param text 文本内容
|
||||
* @param text 文本内容,{@code null}表示空节点
|
||||
* @return 增加的子节点,即Text节点
|
||||
* @since 5.3.0
|
||||
*/
|
||||
private static Node appendText(final Document doc, final Node node, final CharSequence text) {
|
||||
return node.appendChild(doc.createTextNode(StrUtil.str(text)));
|
||||
return node.appendChild(doc.createTextNode(StrUtil.toStringOrEmpty(text)));
|
||||
}
|
||||
// ---------------------------------------------------------------------------------------- Private method end
|
||||
|
||||
|
@ -15,6 +15,7 @@ package org.dromara.hutool.core.util;
|
||||
import org.dromara.hutool.core.collection.ListUtil;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.dromara.hutool.core.convert.Convert;
|
||||
import org.dromara.hutool.core.util.ObjUtil;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@ -223,8 +224,8 @@ public class ObjUtilTest {
|
||||
|
||||
@Test
|
||||
public void toStringTest() {
|
||||
Assertions.assertEquals("null", ObjUtil.toString(null));
|
||||
Assertions.assertEquals(Collections.emptyMap().toString(), ObjUtil.toString(Collections.emptyMap()));
|
||||
Assertions.assertEquals("null", Convert.toStrOrNullStr(null));
|
||||
Assertions.assertEquals(Collections.emptyMap().toString(), Convert.toStrOrNullStr(Collections.emptyMap()));
|
||||
Assertions.assertEquals("[1, 2]", Arrays.asList("1", "2").toString());
|
||||
}
|
||||
|
||||
|
@ -75,7 +75,7 @@ public class SettingConfigParser implements ConfigParser {
|
||||
@Override
|
||||
public DbConfig parse(final String group) {
|
||||
final Setting setting = this.setting;
|
||||
final Setting subSetting = setting.getSetting(StrUtil.emptyIfNull(group));
|
||||
final Setting subSetting = setting.getSetting(StrUtil.toStringOrEmpty(group));
|
||||
if (MapUtil.isEmpty(subSetting)) {
|
||||
throw new DbException("No config for group: [{}]", group);
|
||||
}
|
||||
|
@ -167,7 +167,7 @@ public class DSPool implements Closeable {
|
||||
* @return {@link DSWrapper} 数据源包装
|
||||
*/
|
||||
private DSWrapper createDSWrapper(final String group) {
|
||||
final DbConfig dbConfig = this.configParser.parse(StrUtil.emptyIfNull(group));
|
||||
final DbConfig dbConfig = this.configParser.parse(StrUtil.toStringOrEmpty(group));
|
||||
return DSWrapper.wrap(factory.createDataSource(dbConfig), dbConfig);
|
||||
}
|
||||
}
|
||||
|
@ -46,7 +46,7 @@ public class AnalysisEngine implements TokenizerEngine {
|
||||
public Result parse(final CharSequence text) {
|
||||
final TokenStream stream;
|
||||
try {
|
||||
stream = analyzer.tokenStream("text", StrUtil.str(text));
|
||||
stream = analyzer.tokenStream("text", StrUtil.toStringOrEmpty(text));
|
||||
stream.reset();
|
||||
} catch (final IOException e) {
|
||||
throw new TokenizerException(e);
|
||||
|
@ -48,7 +48,7 @@ public class AnsjEngine implements TokenizerEngine {
|
||||
|
||||
@Override
|
||||
public Result parse(final CharSequence text) {
|
||||
return new AnsjResult(analysis.parseStr(StrUtil.str(text)));
|
||||
return new AnsjResult(analysis.parseStr(StrUtil.toStringOrEmpty(text)));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -48,7 +48,7 @@ public class HanLPEngine implements TokenizerEngine {
|
||||
|
||||
@Override
|
||||
public Result parse(final CharSequence text) {
|
||||
return new HanLPResult(this.seg.seg(StrUtil.str(text)));
|
||||
return new HanLPResult(this.seg.seg(StrUtil.toStringOrEmpty(text)));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -60,7 +60,7 @@ public class JcsegEngine implements TokenizerEngine {
|
||||
// 依据给定的ADictionary和SegmenterConfig来创建ISegment
|
||||
final ISegment segment = ISegment.COMPLEX.factory.create(config, dic);
|
||||
try {
|
||||
segment.reset(new StringReader(StrUtil.str(text)));
|
||||
segment.reset(new StringReader(StrUtil.toStringOrEmpty(text)));
|
||||
} catch (final IOException e) {
|
||||
throw new TokenizerException(e);
|
||||
}
|
||||
|
@ -51,7 +51,7 @@ public class JiebaEngine implements TokenizerEngine {
|
||||
|
||||
@Override
|
||||
public Result parse(final CharSequence text) {
|
||||
return new JiebaResult(jiebaSegmenter.process(StrUtil.str(text), mode));
|
||||
return new JiebaResult(jiebaSegmenter.process(StrUtil.toStringOrEmpty(text), mode));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -51,7 +51,7 @@ public class MynlpEngine implements TokenizerEngine {
|
||||
|
||||
@Override
|
||||
public Result parse(final CharSequence text) {
|
||||
final Sentence sentence = this.lexer.scan(StrUtil.str(text));
|
||||
final Sentence sentence = this.lexer.scan(StrUtil.toStringOrEmpty(text));
|
||||
return new MynlpResult(sentence);
|
||||
}
|
||||
|
||||
|
@ -59,7 +59,7 @@ public class WordEngine implements TokenizerEngine {
|
||||
|
||||
@Override
|
||||
public Result parse(final CharSequence text) {
|
||||
return new WordResult(this.segmentation.seg(StrUtil.str(text)));
|
||||
return new WordResult(this.segmentation.seg(StrUtil.toStringOrEmpty(text)));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -199,7 +199,7 @@ public enum GlobalHeaders {
|
||||
for (final Entry<String, List<String>> entry : headers.entrySet()) {
|
||||
name = entry.getKey();
|
||||
for (final String value : entry.getValue()) {
|
||||
this.header(name, StrUtil.emptyIfNull(value), false);
|
||||
this.header(name, StrUtil.toStringOrEmpty(value), false);
|
||||
}
|
||||
}
|
||||
return this;
|
||||
|
@ -126,7 +126,7 @@ public interface HeaderOperation<T extends HeaderOperation<T>> {
|
||||
for (final Map.Entry<String, ? extends Collection<String>> entry : headerMap.entrySet()) {
|
||||
name = entry.getKey();
|
||||
for (final String value : entry.getValue()) {
|
||||
this.header(name, StrUtil.emptyIfNull(value), isOverride);
|
||||
this.header(name, StrUtil.toStringOrEmpty(value), isOverride);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -14,6 +14,7 @@ package org.dromara.hutool.json;
|
||||
|
||||
import org.dromara.hutool.core.io.IoUtil;
|
||||
import org.dromara.hutool.core.io.ReaderWrapper;
|
||||
import org.dromara.hutool.core.lang.Assert;
|
||||
import org.dromara.hutool.core.text.StrUtil;
|
||||
import org.dromara.hutool.json.mapper.JSONValueMapper;
|
||||
|
||||
@ -76,7 +77,7 @@ public class JSONTokener extends ReaderWrapper {
|
||||
* @param config JSON配置
|
||||
*/
|
||||
public JSONTokener(final CharSequence s, final JSONConfig config) {
|
||||
this(new StringReader(StrUtil.str(s)), config);
|
||||
this(new StringReader(Assert.notBlank(s).toString()), config);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -86,7 +87,7 @@ public class JSONTokener extends ReaderWrapper {
|
||||
* @param config JSON配置
|
||||
*/
|
||||
public JSONTokener(final Reader reader, final JSONConfig config) {
|
||||
super(IoUtil.toMarkSupport(reader));
|
||||
super(IoUtil.toMarkSupport(Assert.notNull(reader)));
|
||||
this.eof = false;
|
||||
this.usePrevious = false;
|
||||
this.previous = 0;
|
||||
|
@ -35,7 +35,7 @@ public class EscapeStrCellSetter extends CharSequenceCellSetter {
|
||||
* @param value 值
|
||||
*/
|
||||
public EscapeStrCellSetter(final CharSequence value) {
|
||||
super(escape(StrUtil.str(value)));
|
||||
super(escape(StrUtil.toStringOrNull(value)));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -50,9 +50,9 @@ public class GroupedMap extends LinkedHashMap<String, LinkedHashMap<String, Stri
|
||||
public String get(final CharSequence group, final CharSequence key) {
|
||||
readLock.lock();
|
||||
try {
|
||||
final LinkedHashMap<String, String> map = this.get(StrUtil.emptyIfNull(group));
|
||||
final LinkedHashMap<String, String> map = this.get(StrUtil.toStringOrEmpty(group));
|
||||
if (MapUtil.isNotEmpty(map)) {
|
||||
return map.get(StrUtil.str(key));
|
||||
return map.get(StrUtil.toStringOrNull(key));
|
||||
}
|
||||
} finally {
|
||||
readLock.unlock();
|
||||
@ -100,7 +100,7 @@ public class GroupedMap extends LinkedHashMap<String, LinkedHashMap<String, Stri
|
||||
* @return 此key之前存在的值,如果没有返回null
|
||||
*/
|
||||
public String put(String group, final String key, final String value) {
|
||||
group = StrUtil.emptyIfNull(group).trim();
|
||||
group = StrUtil.toStringOrEmpty(group).trim();
|
||||
writeLock.lock();
|
||||
try {
|
||||
final LinkedHashMap<String, String> valueMap = this.computeIfAbsent(group, k -> new LinkedHashMap<>());
|
||||
@ -133,7 +133,7 @@ public class GroupedMap extends LinkedHashMap<String, LinkedHashMap<String, Stri
|
||||
* @return 被删除的值,如果值不存在,返回null
|
||||
*/
|
||||
public String remove(String group, final String key) {
|
||||
group = StrUtil.emptyIfNull(group).trim();
|
||||
group = StrUtil.toStringOrEmpty(group).trim();
|
||||
writeLock.lock();
|
||||
try {
|
||||
final LinkedHashMap<String, String> valueMap = this.get(group);
|
||||
@ -153,7 +153,7 @@ public class GroupedMap extends LinkedHashMap<String, LinkedHashMap<String, Stri
|
||||
* @return 是否为空
|
||||
*/
|
||||
public boolean isEmpty(String group) {
|
||||
group = StrUtil.emptyIfNull(group).trim();
|
||||
group = StrUtil.toStringOrEmpty(group).trim();
|
||||
readLock.lock();
|
||||
try {
|
||||
final LinkedHashMap<String, String> valueMap = this.get(group);
|
||||
@ -184,7 +184,7 @@ public class GroupedMap extends LinkedHashMap<String, LinkedHashMap<String, Stri
|
||||
* @return 是否包含key
|
||||
*/
|
||||
public boolean containsKey(String group, final String key) {
|
||||
group = StrUtil.emptyIfNull(group).trim();
|
||||
group = StrUtil.toStringOrEmpty(group).trim();
|
||||
readLock.lock();
|
||||
try {
|
||||
final LinkedHashMap<String, String> valueMap = this.get(group);
|
||||
@ -205,7 +205,7 @@ public class GroupedMap extends LinkedHashMap<String, LinkedHashMap<String, Stri
|
||||
* @return 是否包含值
|
||||
*/
|
||||
public boolean containsValue(String group, final String value) {
|
||||
group = StrUtil.emptyIfNull(group).trim();
|
||||
group = StrUtil.toStringOrEmpty(group).trim();
|
||||
readLock.lock();
|
||||
try {
|
||||
final LinkedHashMap<String, String> valueMap = this.get(group);
|
||||
@ -225,7 +225,7 @@ public class GroupedMap extends LinkedHashMap<String, LinkedHashMap<String, Stri
|
||||
* @return this
|
||||
*/
|
||||
public GroupedMap clear(String group) {
|
||||
group = StrUtil.emptyIfNull(group).trim();
|
||||
group = StrUtil.toStringOrEmpty(group).trim();
|
||||
writeLock.lock();
|
||||
try {
|
||||
final LinkedHashMap<String, String> valueMap = this.get(group);
|
||||
@ -255,7 +255,7 @@ public class GroupedMap extends LinkedHashMap<String, LinkedHashMap<String, Stri
|
||||
* @return 键Set
|
||||
*/
|
||||
public Set<String> keySet(String group) {
|
||||
group = StrUtil.emptyIfNull(group).trim();
|
||||
group = StrUtil.toStringOrEmpty(group).trim();
|
||||
readLock.lock();
|
||||
try {
|
||||
final LinkedHashMap<String, String> valueMap = this.get(group);
|
||||
@ -275,7 +275,7 @@ public class GroupedMap extends LinkedHashMap<String, LinkedHashMap<String, Stri
|
||||
* @return 值
|
||||
*/
|
||||
public Collection<String> values(String group) {
|
||||
group = StrUtil.emptyIfNull(group).trim();
|
||||
group = StrUtil.toStringOrEmpty(group).trim();
|
||||
readLock.lock();
|
||||
try {
|
||||
final LinkedHashMap<String, String> valueMap = this.get(group);
|
||||
@ -305,7 +305,7 @@ public class GroupedMap extends LinkedHashMap<String, LinkedHashMap<String, Stri
|
||||
* @return 键值对
|
||||
*/
|
||||
public Set<Entry<String, String>> entrySet(String group) {
|
||||
group = StrUtil.emptyIfNull(group).trim();
|
||||
group = StrUtil.toStringOrEmpty(group).trim();
|
||||
readLock.lock();
|
||||
try {
|
||||
final LinkedHashMap<String, String> valueMap = this.get(group);
|
||||
|
@ -149,7 +149,7 @@ public class Profile implements Serializable {
|
||||
*/
|
||||
private String fixNameForProfile(final String name) {
|
||||
Assert.notBlank(name, "Setting name must be not blank !");
|
||||
final String actralProfile = StrUtil.emptyIfNull(this.profile);
|
||||
final String actralProfile = StrUtil.toStringOrEmpty(this.profile);
|
||||
if (!name.contains(StrUtil.DOT)) {
|
||||
return StrUtil.format("{}/{}.setting", actralProfile, name);
|
||||
}
|
||||
|
@ -247,7 +247,8 @@ public final class Props extends Properties implements TypeGetter<CharSequence>
|
||||
|
||||
@Override
|
||||
public Object getObj(final CharSequence key, final Object defaultValue) {
|
||||
return ObjUtil.defaultIfNull(getProperty(StrUtil.str(key)), defaultValue);
|
||||
Assert.notNull(key, "Key must be not null!");
|
||||
return ObjUtil.defaultIfNull(getProperty(key.toString()), defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -408,7 +409,7 @@ public final class Props extends Properties implements TypeGetter<CharSequence>
|
||||
* @since 4.6.3
|
||||
*/
|
||||
public <T> T toBean(final T bean, String prefix) {
|
||||
prefix = StrUtil.emptyIfNull(StrUtil.addSuffixIfNot(prefix, StrUtil.DOT));
|
||||
prefix = StrUtil.toStringOrEmpty(StrUtil.addSuffixIfNot(prefix, StrUtil.DOT));
|
||||
|
||||
String key;
|
||||
for (final java.util.Map.Entry<Object, Object> entry : this.entrySet()) {
|
||||
|
@ -200,7 +200,7 @@ public class TomlWriter {
|
||||
indent();
|
||||
writeKey(name);
|
||||
write(" = ");
|
||||
writeString(StrUtil.emptyIfNull(ArrayUtil.toString(array)));
|
||||
writeString(StrUtil.toStringOrEmpty(ArrayUtil.toString(array)));
|
||||
}
|
||||
} else if (value instanceof Map) {// table
|
||||
if (simpleValues) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user