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.

@FunctionalInterface public interface CheckedConsumer<T>
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 Type
    Method
    Description
    void
    accept(T t)
    Accept input, may throw checked exception 接受输入,可能抛出受检异常
    default void
    Accept silently, ignoring any checked or unchecked exception (but not Error) 静默接受,忽略任何受检或非受检异常(不包括 Error
    andThen(CheckedConsumer<? super T> after)
    Chain with another consumer (execute this, then after) 与另一个消费者链接(先执行本函数,再执行 after)
    static <T> CheckedConsumer<T>
    of(Consumer<T> consumer)
    Wrap a standard Consumer as CheckedConsumer 将标准 Consumer 包装为 CheckedConsumer
    default Consumer<T>
    Convert to standard Consumer, wrapping checked exceptions in OpenFunctionalException 转换为标准 Consumer,将受检异常包装为 OpenFunctionalException
  • Method Details

    • accept

      void accept(T t) throws Exception
      Accept input, may throw checked exception 接受输入,可能抛出受检异常
      Parameters:
      t - input - 输入
      Throws:
      Exception - if operation fails - 如果操作失败
    • unchecked

      default Consumer<T> unchecked()
      Convert to standard Consumer, wrapping checked exceptions in OpenFunctionalException 转换为标准 Consumer,将受检异常包装为 OpenFunctionalException

      Runtime exceptions are rethrown as-is; checked exceptions are wrapped in OpenFunctionalException.

      运行时异常原样抛出;受检异常被包装为 OpenFunctionalException

      Returns:
      Consumer that wraps checked exceptions - 包装受检异常的 Consumer
    • acceptQuietly

      default void acceptQuietly(T t)
      Accept silently, ignoring any checked or unchecked exception (but not Error) 静默接受,忽略任何受检或非受检异常(不包括 Error
      Parameters:
      t - input - 输入
    • andThen

      default CheckedConsumer<T> andThen(CheckedConsumer<? super T> after)
      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

      static <T> CheckedConsumer<T> of(Consumer<T> consumer)
      Wrap a standard Consumer as CheckedConsumer 将标准 Consumer 包装为 CheckedConsumer
      Type Parameters:
      T - input type - 输入类型
      Parameters:
      consumer - standard Consumer - 标准 Consumer
      Returns:
      CheckedConsumer wrapper - CheckedConsumer 包装器