Interface CheckedFunction<T,R>

Type Parameters:
T - input type - 输入类型
R - return 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 CheckedFunction<T,R>
CheckedFunction - Function that can throw checked exceptions CheckedFunction - 可抛出受检异常的函数

Extends the concept of JDK Function to allow throwing checked exceptions. This is a local interface for the functional module that mirrors Core's CheckedFunction.

扩展 JDK Function 的概念,支持抛出受检异常。 这是函数式模块的本地接口,与 Core 的 CheckedFunction 对应。

Features | 主要功能:

  • Throw checked exceptions in lambda - 在 lambda 中抛出受检异常
  • Convert to standard Function (unchecked) - 转换为标准 Function
  • Silent execution with defaults - 静默执行并返回默认值
  • Composition support (andThen/compose) - 支持函数组合

Usage Examples | 使用示例:

CheckedFunction<Path, String> readFile = path -> Files.readString(path);

Function<Path, String> wrapped = readFile.unchecked();
String content = readFile.applyOrDefault(path, "fallback");

Security | 安全性:

  • Thread-safe: Yes (stateless) - 线程安全: 是 (无状态)
  • Null-safe: No - 空值安全: 否
Since:
JDK 25, opencode-base-functional V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    default <V> CheckedFunction<T,V>
    andThen(CheckedFunction<? super R, ? extends V> after)
    Compose with another function (execute this, then after) 与另一个函数组合(先执行本函数,再执行 after)
    apply(T t)
    Apply function to argument, may throw checked exception 应用函数到参数,可能抛出受检异常
    default R
    applyOrDefault(T t, R defaultValue)
    Apply silently, returning default value on exception 静默应用,异常时返回默认值
    default R
    Apply silently, returning null on exception 静默应用,异常时返回 null
    default <V> CheckedFunction<V,R>
    compose(CheckedFunction<? super V, ? extends T> before)
    Compose with another function (execute before, then this) 与另一个函数组合(先执行 before,再执行本函数)
    static <T> CheckedFunction<T,T>
    Create identity function 创建恒等函数
    static <T,R> CheckedFunction<T,R>
    of(Function<T,R> function)
    Wrap a standard Function as CheckedFunction 将标准 Function 包装为 CheckedFunction
    default Function<T,R>
    Convert to standard Function, wrapping checked exceptions 转换为标准 Function,包装受检异常
  • Method Details

    • apply

      R apply(T t) throws Exception
      Apply function to argument, may throw checked exception 应用函数到参数,可能抛出受检异常
      Parameters:
      t - input - 输入
      Returns:
      result - 结果
      Throws:
      Exception - if operation fails - 如果操作失败
    • unchecked

      default Function<T,R> unchecked()
      Convert to standard Function, wrapping checked exceptions 转换为标准 Function,包装受检异常
      Returns:
      Function that wraps checked exceptions - 包装受检异常的 Function
    • applyQuietly

      default R applyQuietly(T t)
      Apply silently, returning null on exception 静默应用,异常时返回 null
      Parameters:
      t - input - 输入
      Returns:
      result or null - 结果或 null
    • applyOrDefault

      default R applyOrDefault(T t, R defaultValue)
      Apply silently, returning default value on exception 静默应用,异常时返回默认值
      Parameters:
      t - input - 输入
      defaultValue - default value on error - 错误时的默认值
      Returns:
      result or default value - 结果或默认值
    • andThen

      default <V> CheckedFunction<T,V> andThen(CheckedFunction<? super R, ? extends V> after)
      Compose with another function (execute this, then after) 与另一个函数组合(先执行本函数,再执行 after)
      Type Parameters:
      V - result type of after function - after 函数的结果类型
      Parameters:
      after - function to apply after this - 在本函数之后应用的函数
      Returns:
      composed function - 组合后的函数
    • compose

      default <V> CheckedFunction<V,R> compose(CheckedFunction<? super V, ? extends T> before)
      Compose with another function (execute before, then this) 与另一个函数组合(先执行 before,再执行本函数)
      Type Parameters:
      V - input type of before function - before 函数的输入类型
      Parameters:
      before - function to apply before this - 在本函数之前应用的函数
      Returns:
      composed function - 组合后的函数
    • identity

      static <T> CheckedFunction<T,T> identity()
      Create identity function 创建恒等函数
      Type Parameters:
      T - type - 类型
      Returns:
      identity function - 恒等函数
    • of

      static <T,R> CheckedFunction<T,R> of(Function<T,R> function)
      Wrap a standard Function as CheckedFunction 将标准 Function 包装为 CheckedFunction
      Type Parameters:
      T - input type - 输入类型
      R - return type - 返回值类型
      Parameters:
      function - standard Function - 标准 Function
      Returns:
      CheckedFunction wrapper - CheckedFunction 包装器