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

@@ -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}
*