Opt增加ifFail

This commit is contained in:
Looly 2024-07-30 09:43:43 +08:00
parent 1e469e6dbf
commit f5f96b66f5
2 changed files with 23 additions and 0 deletions

View File

@ -11,6 +11,7 @@
* 【core 】 CharSequenceUtil增加removeAllPrefix和removeAllSuffix方法pr#3655@Github
* 【core 】 CharSequenceUtil增加stripAll方法pr#3659@Github
* 【crypto 】 支持"RSA/ECB/OAEPWithSHA-1AndMGF1Padding"的RSA加解密pr#3675@Github
* 【core 】 Opt增加ifFailpr#1239@Gitee
### 🐞Bug修复
* 【core 】 修复因RFC3986理解有误导致的UrlPath处理冒号转义问题issue#IAAE88@Gitee

View File

@ -165,6 +165,28 @@ public class Opt<T> {
return null != this.exception;
}
/**
* 如果包裹内容失败了则执行传入的操作({@link Consumer#accept})
*
* <p> 例如执行有异常就打印结果
* <pre>{@code
* Opt.ofTry(() -> 1 / 0).ifFail(Console::log);
* }</pre>
*
* @param action 你想要执行的操作
* @return this
* @throws NullPointerException 如果包裹里的值存在但你传入的操作为{@code null}时抛出
*/
public Opt<T> ifFail(final Consumer<? super Throwable> action) throws NullPointerException {
Objects.requireNonNull(action, "action is null");
if (isFail()) {
action.accept(this.exception);
}
return this;
}
/**
* 判断包裹里元素的值是否存在存在为 {@code true}否则为{@code false}
*