This commit is contained in:
Looly 2021-11-21 04:50:48 +08:00
parent 147cc683a5
commit 42fbf36042
6 changed files with 34 additions and 38 deletions

View File

@ -110,6 +110,21 @@ public class Opt<T> {
return CollectionUtil.isEmpty(value) ? empty() : new Opt<>(value); return CollectionUtil.isEmpty(value) ? empty() : new Opt<>(value);
} }
/**
* 执行一系列操作如果途中发生 {@code NPE} {@code IndexOutOfBoundsException}返回一个空的{@code Opt}
*
* @param supplier 操作
* @param <T> 类型
* @return 操作执行后的值
*/
public static <T> Opt<T> exec(Supplier<T> supplier) {
try {
return Opt.ofNullable(supplier.get());
} catch (NullPointerException | IndexOutOfBoundsException e) {
return empty();
}
}
/** /**
* 包裹里实际的元素 * 包裹里实际的元素
*/ */
@ -411,10 +426,7 @@ public class Opt<T> {
* @throws NoSuchElementException 如果包裹里的值不存在则抛出该异常 * @throws NoSuchElementException 如果包裹里的值不存在则抛出该异常
*/ */
public T orElseThrow() { public T orElseThrow() {
if (value == null) { return orElseThrow(NoSuchElementException::new, "No value present");
throw new NoSuchElementException("No value present");
}
return value;
} }
/** /**
@ -469,22 +481,6 @@ public class Opt<T> {
return Optional.ofNullable(this.value); return Optional.ofNullable(this.value);
} }
/**
* 执行一系列操作如果途中发生 {@code NPE} {@code IndexOutOfBoundsException}返回一个空的{@code Opt}
*
* @param supplier 操作
* @param <T> 类型
* @return 操作执行后的值
*/
public static <T> Opt<T> exec(Supplier<T> supplier) {
try {
return Opt.ofNullable(supplier.get());
} catch (NullPointerException | IndexOutOfBoundsException e) {
return empty();
}
}
/** /**
* 判断传入参数是否与 {@code Opt}相等 * 判断传入参数是否与 {@code Opt}相等
* 在以下情况下返回true * 在以下情况下返回true

View File

@ -2471,21 +2471,22 @@ public class CharSequenceUtil {
* *
* @param str 被重复的字符串 * @param str 被重复的字符串
* @param count 数量 * @param count 数量
* @param conjunction 分界符 * @param delimiter 分界符
* @return 连接后的字符串 * @return 连接后的字符串
* @since 4.0.1 * @since 4.0.1
*/ */
public static String repeatAndJoin(CharSequence str, int count, CharSequence conjunction) { public static String repeatAndJoin(CharSequence str, int count, CharSequence delimiter) {
if (count <= 0) { if (count <= 0) {
return EMPTY; return EMPTY;
} }
final StrBuilder builder = StrBuilder.create(); final StringBuilder builder = new StringBuilder(str.length() * count);
boolean isFirst = true; builder.append(str);
count--;
final boolean isAppendDelimiter = isNotEmpty(delimiter);
while (count-- > 0) { while (count-- > 0) {
if (isFirst) { if (isAppendDelimiter) {
isFirst = false; builder.append(delimiter);
} else if (isNotEmpty(conjunction)) {
builder.append(conjunction);
} }
builder.append(str); builder.append(str);
} }
@ -3521,7 +3522,7 @@ public class CharSequenceUtil {
fromIndex = 0; fromIndex = 0;
} }
final StrBuilder result = StrBuilder.create(strLength + 16); final StringBuilder result = new StringBuilder(strLength - searchStrLength + replacement.length());
if (0 != fromIndex) { if (0 != fromIndex) {
result.append(str.subSequence(0, fromIndex)); result.append(str.subSequence(0, fromIndex));
} }
@ -4285,7 +4286,7 @@ public class CharSequenceUtil {
// 循环位移当越界时循环 // 循环位移当越界时循环
moveLength = moveLength % len; moveLength = moveLength % len;
} }
final StrBuilder strBuilder = StrBuilder.create(len); final StringBuilder strBuilder = new StringBuilder(len);
if (moveLength > 0) { if (moveLength > 0) {
int endAfterMove = Math.min(endExclude + moveLength, str.length()); int endAfterMove = Math.min(endExclude + moveLength, str.length());
strBuilder.append(str.subSequence(0, startInclude))// strBuilder.append(str.subSequence(0, startInclude))//

View File

@ -191,11 +191,10 @@ public class StrJoiner implements Appendable, Serializable {
/** /**
* 追加对象到拼接器中 * 追加对象到拼接器中
* *
* @param <T> 元素类型
* @param obj 对象支持数组集合等 * @param obj 对象支持数组集合等
* @return this * @return this
*/ */
public <T> StrJoiner append(Object obj) { public StrJoiner append(Object obj) {
if (null == obj) { if (null == obj) {
append((CharSequence) null); append((CharSequence) null);
} else if (ArrayUtil.isArray(obj)) { } else if (ArrayUtil.isArray(obj)) {

View File

@ -25,7 +25,7 @@ public class UnicodeUtil {
} }
final int len = unicode.length(); final int len = unicode.length();
StrBuilder sb = StrBuilder.create(len); StringBuilder sb = new StringBuilder(len);
int i; int i;
int pos = 0; int pos = 0;
while ((i = StrUtil.indexOfIgnoreCase(unicode, "\\u", pos)) != -1) { while ((i = StrUtil.indexOfIgnoreCase(unicode, "\\u", pos)) != -1) {
@ -101,7 +101,7 @@ public class UnicodeUtil {
} }
final int len = str.length(); final int len = str.length();
final StrBuilder unicode = StrBuilder.create(str.length() * 6); final StringBuilder unicode = new StringBuilder(str.length() * 6);
char c; char c;
for (int i = 0; i < len; i++) { for (int i = 0; i < len; i++) {
c = str.charAt(i); c = str.charAt(i);

View File

@ -177,7 +177,7 @@ public class StrUtil extends CharSequenceUtil implements StrPool {
* @return 字符串 * @return 字符串
*/ */
public static String str(byte[] bytes, String charset) { public static String str(byte[] bytes, String charset) {
return str(bytes, isBlank(charset) ? Charset.defaultCharset() : Charset.forName(charset)); return str(bytes, CharsetUtil.charset(charset));
} }
/** /**
@ -206,7 +206,7 @@ public class StrUtil extends CharSequenceUtil implements StrPool {
* @return 字符串 * @return 字符串
*/ */
public static String str(Byte[] bytes, String charset) { public static String str(Byte[] bytes, String charset) {
return str(bytes, isBlank(charset) ? Charset.defaultCharset() : Charset.forName(charset)); return str(bytes, CharsetUtil.charset(charset));
} }
/** /**
@ -243,7 +243,7 @@ public class StrUtil extends CharSequenceUtil implements StrPool {
return null; return null;
} }
return str(data, Charset.forName(charset)); return str(data, CharsetUtil.charset(charset));
} }
/** /**