Interface CheckedSupplier<T>

Type Parameters:
T - return type - 返回值类型
All Known Subinterfaces:
ExceptionUtil.CheckedSupplier<T>
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 CheckedSupplier<T>
Checked Supplier - Supplier that can throw checked exceptions 可抛出受检异常的 Supplier - 扩展 JDK Supplier 支持受检异常

Extends JDK Supplier to allow throwing checked exceptions in lambdas.

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

Features | 主要功能:

  • Throw checked exceptions in lambda - 在 lambda 中抛出受检异常
  • Convert to standard Supplier (unchecked) - 转换为标准 Supplier
  • Silent execution (getQuietly/getOrDefault) - 静默执行

Usage Examples | 使用示例:

CheckedSupplier<String> supplier = () -> Files.readString(path);
Supplier<String> wrapped = supplier.unchecked();
String result = supplier.getOrDefault("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
    get()
    Gets 获取结果,可能抛出受检异常
    default T
    getOrDefault(T defaultValue)
    Silently executes, returning the default value on exception 静默执行,异常时返回默认值
    default T
    Silently executes, returning null on exception 静默执行,异常时返回 null
    static <T> CheckedSupplier<T>
    of(Supplier<T> supplier)
    Wraps a standard Supplier as a CheckedSupplier 将普通 Supplier 包装为 CheckedSupplier
    default Supplier<T>
    Converts 转换为标准 Supplier,受检异常包装为 RuntimeException
  • Method Details

    • get

      T get() throws Exception
      Gets 获取结果,可能抛出受检异常
      Returns:
      the result | 结果
      Throws:
      Exception - if the condition is not met | 如果操作失败
    • unchecked

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

      default T getQuietly()
      Silently executes, returning null on exception 静默执行,异常时返回 null
      Returns:
      the result | 结果或 null
    • getOrDefault

      default T getOrDefault(T defaultValue)
      Silently executes, returning the default value on exception 静默执行,异常时返回默认值
      Parameters:
      defaultValue - the default value | 默认值
      Returns:
      the result | 结果或默认值
    • of

      static <T> CheckedSupplier<T> of(Supplier<T> supplier)
      Wraps a standard Supplier as a CheckedSupplier 将普通 Supplier 包装为 CheckedSupplier
      Type Parameters:
      T - the value | 返回值类型
      Parameters:
      supplier - the value | 普通 Supplier
      Returns:
      CheckedSupplier