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>
Checked Function - Function that can throw checked exceptions 可抛出受检异常的 Function - 扩展 JDK Function 支持受检异常

Extends JDK Function to allow throwing checked exceptions in lambdas.

扩展 JDK Function,支持在 lambda 中抛出受检异常。

Features | 主要功能:

  • Throw checked exceptions in lambda - 在 lambda 中抛出受检异常
  • Convert to standard Function (unchecked) - 转换为标准 Function
  • Silent execution (applyQuietly/applyOrDefault) - 静默执行
  • Composition (andThen/compose) - 函数组合

Usage Examples | 使用示例:

CheckedFunction<Path, String> reader = path -> Files.readString(path);
paths.stream().map(reader.unchecked()).toList();
String result = reader.applyOrDefault(path, "fallback");

Security | 安全性:

  • Thread-safe: Yes (stateless) - 线程安全: 是 (无状态)
  • Null-safe: No - 空值安全: 否
Since:
JDK 25, opencode-base-core 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)
    Composes functions (executes this function first, then after) 组合函数(先执行本函数,再执行 after)
    apply(T t)
    Applies the function, may throw a checked exception 应用函数,可能抛出受检异常
    default R
    applyOrDefault(T t, R defaultValue)
    Silently applies, returning the default value on exception 静默应用,异常时返回默认值
    default R
    Silently applies, returning null on exception 静默应用,异常时返回 null
    default <V> CheckedFunction<V,R>
    compose(CheckedFunction<? super V, ? extends T> before)
    Composes functions (executes before first, then this function) 组合函数(先执行 before,再执行本函数)
    static <T> CheckedFunction<T,T>
    Creates 创建恒等函数
    static <T,R> CheckedFunction<T,R>
    of(Function<T,R> function)
    Wraps a standard Function as a CheckedFunction 将普通 Function 包装为 CheckedFunction
    default Function<T,R>
    Converts 转换为标准 Function,受检异常包装为 RuntimeException
  • Method Details

    • apply

      R apply(T t) throws Exception
      Applies the function, may throw a checked exception 应用函数,可能抛出受检异常
      Parameters:
      t - the value | 输入值
      Returns:
      the result | 结果
      Throws:
      Exception - if the condition is not met | 如果操作失败
    • unchecked

      default Function<T,R> unchecked()
      Converts 转换为标准 Function,受检异常包装为 RuntimeException
      Returns:
      Function
    • applyQuietly

      default R applyQuietly(T t)
      Silently applies, returning null on exception 静默应用,异常时返回 null
      Parameters:
      t - the value | 输入值
      Returns:
      the result | 结果或 null
    • applyOrDefault

      default R applyOrDefault(T t, R defaultValue)
      Silently applies, returning the default value on exception 静默应用,异常时返回默认值
      Parameters:
      t - the value | 输入值
      defaultValue - the default value | 默认值
      Returns:
      the result | 结果或默认值
    • andThen

      default <V> CheckedFunction<T,V> andThen(CheckedFunction<? super R, ? extends V> after)
      Composes functions (executes this function first, then after) 组合函数(先执行本函数,再执行 after)
      Type Parameters:
      V - the value | 最终返回类型
      Parameters:
      after - the value | 后续函数
      Returns:
      the result | 组合后的函数
    • compose

      default <V> CheckedFunction<V,R> compose(CheckedFunction<? super V, ? extends T> before)
      Composes functions (executes before first, then this function) 组合函数(先执行 before,再执行本函数)
      Type Parameters:
      V - the value | 输入类型
      Parameters:
      before - the value | 前置函数
      Returns:
      the result | 组合后的函数
    • of

      static <T,R> CheckedFunction<T,R> of(Function<T,R> function)
      Wraps a standard Function as a CheckedFunction 将普通 Function 包装为 CheckedFunction
      Type Parameters:
      T - the value | 输入类型
      R - the value | 返回值类型
      Parameters:
      function - the value | 普通 Function
      Returns:
      CheckedFunction
    • identity

      static <T> CheckedFunction<T,T> identity()
      Creates 创建恒等函数
      Type Parameters:
      T - the type | 类型
      Returns:
      the result | 恒等函数