mirror of
https://gitee.com/dromara/hutool.git
synced 2025-06-28 13:34:09 +08:00
Opt增加ifFail
This commit is contained in:
parent
1e469e6dbf
commit
f5f96b66f5
@ -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增加ifFail(pr#1239@Gitee)
|
||||
|
||||
### 🐞Bug修复
|
||||
* 【core 】 修复因RFC3986理解有误导致的UrlPath处理冒号转义问题(issue#IAAE88@Gitee)
|
||||
|
@ -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}
|
||||
*
|
||||
|
Loading…
Reference in New Issue
Block a user