Interface CheckedConsumer<T>
- Type Parameters:
T- input type - 输入类型
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
CheckedConsumer - Consumer that may throw checked exceptions
CheckedConsumer - 可能抛出受检异常的消费者
Extends the concept of JDK Consumer to allow throwing checked exceptions.
Provides safe conversion to standard Consumer via unchecked() and
silent execution via acceptQuietly(Object).
扩展 JDK Consumer 的概念,支持抛出受检异常。
通过 unchecked() 安全转换为标准 Consumer,
通过 acceptQuietly(Object) 静默执行。
Features | 主要功能:
- Throw checked exceptions in lambda - 在 lambda 中抛出受检异常
- Convert to standard Consumer (unchecked) - 转换为标准 Consumer
- Silent execution - 静默执行
- Chaining support (andThen) - 支持链式调用
Usage Examples | 使用示例:
CheckedConsumer<Path> deletePath = path -> Files.delete(path);
Consumer<Path> wrapped = deletePath.unchecked();
deletePath.acceptQuietly(path);
// Chaining
deletePath.andThen(p -> log.info("Deleted " + p))
.accept(path);
Security | 安全性:
- Thread-safe: Yes (stateless) - 线程安全: 是 (无状态)
- Null-safe: No - 空值安全: 否
- Since:
- JDK 25, opencode-base-functional V1.0.3
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionvoidAccept input, may throw checked exception 接受输入,可能抛出受检异常default voidacceptQuietly(T t) default CheckedConsumer<T> andThen(CheckedConsumer<? super T> after) Chain with another consumer (execute this, then after) 与另一个消费者链接(先执行本函数,再执行 after)static <T> CheckedConsumer<T> Wrap a standard Consumer as CheckedConsumer 将标准 Consumer 包装为 CheckedConsumerConvert to standard Consumer, wrapping checked exceptions in OpenFunctionalException 转换为标准 Consumer,将受检异常包装为 OpenFunctionalException
-
Method Details
-
accept
-
unchecked
Convert to standard Consumer, wrapping checked exceptions in OpenFunctionalException 转换为标准 Consumer,将受检异常包装为 OpenFunctionalExceptionRuntime exceptions are rethrown as-is; checked exceptions are wrapped in
OpenFunctionalException.运行时异常原样抛出;受检异常被包装为
OpenFunctionalException。- Returns:
- Consumer that wraps checked exceptions - 包装受检异常的 Consumer
-
acceptQuietly
-
andThen
Chain with another consumer (execute this, then after) 与另一个消费者链接(先执行本函数,再执行 after)- Parameters:
after- consumer to execute after this - 在本函数之后执行的消费者- Returns:
- chained consumer - 链式消费者
- Throws:
NullPointerException- if after is null - 如果 after 为 null
-
of
Wrap a standard Consumer as CheckedConsumer 将标准 Consumer 包装为 CheckedConsumer- Type Parameters:
T- input type - 输入类型- Parameters:
consumer- standard Consumer - 标准 Consumer- Returns:
- CheckedConsumer wrapper - CheckedConsumer 包装器
-