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

Extends JDK Predicate to allow throwing checked exceptions in lambdas.

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

Features | 主要功能:

  • Throw checked exceptions in lambda - 在 lambda 中抛出受检异常
  • Convert to standard Predicate (unchecked) - 转换为标准 Predicate
  • Silent execution (testQuietly/testOrDefault) - 静默执行
  • Logical operations (and/or/negate) - 逻辑操作

Usage Examples | 使用示例:

CheckedPredicate<Path> isLarge = path -> Files.size(path) > 1024;
paths.stream().filter(isLarge.unchecked()).toList();
boolean result = isLarge.testOrDefault(path, false);

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
    and(CheckedPredicate<? super T> other)
    Logical AND 逻辑与
    Negates 取反
    static <T> CheckedPredicate<T>
    of(Predicate<T> predicate)
    Wraps a standard Predicate as a CheckedPredicate 将普通 Predicate 包装为 CheckedPredicate
    or(CheckedPredicate<? super T> other)
    Logical OR 逻辑或
    boolean
    test(T t)
    Tests a condition that may throw a checked exception 测试条件,可能抛出受检异常
    default boolean
    testOrDefault(T t, boolean defaultValue)
    Silently tests, returning the specified default on exception 静默测试,异常时返回指定默认值
    default boolean
    Silently tests, returning false on exception 静默测试,异常时返回 false
    default Predicate<T>
    Converts 转换为标准 Predicate,受检异常包装为 RuntimeException
  • Method Details

    • test

      boolean test(T t) throws Exception
      Tests a condition that may throw a checked exception 测试条件,可能抛出受检异常
      Parameters:
      t - the value | 输入值
      Returns:
      the result | 测试结果
      Throws:
      Exception - if the condition is not met | 如果操作失败
    • unchecked

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

      default boolean testQuietly(T t)
      Silently tests, returning false on exception 静默测试,异常时返回 false
      Parameters:
      t - the value | 输入值
      Returns:
      the result | 测试结果,异常时返回 false
    • testOrDefault

      default boolean testOrDefault(T t, boolean defaultValue)
      Silently tests, returning the specified default on exception 静默测试,异常时返回指定默认值
      Parameters:
      t - the value | 输入值
      defaultValue - the default value | 默认值
      Returns:
      the result | 测试结果或默认值
    • negate

      default CheckedPredicate<T> negate()
      Negates 取反
      Returns:
      the result | 取反后的 Predicate
    • and

      default CheckedPredicate<T> and(CheckedPredicate<? super T> other)
      Logical AND 逻辑与
      Parameters:
      other - the value | 另一个 Predicate
      Returns:
      the result | 组合后的 Predicate
    • or

      default CheckedPredicate<T> or(CheckedPredicate<? super T> other)
      Logical OR 逻辑或
      Parameters:
      other - the value | 另一个 Predicate
      Returns:
      the result | 组合后的 Predicate
    • of

      static <T> CheckedPredicate<T> of(Predicate<T> predicate)
      Wraps a standard Predicate as a CheckedPredicate 将普通 Predicate 包装为 CheckedPredicate
      Type Parameters:
      T - the value | 输入类型
      Parameters:
      predicate - the value | 普通 Predicate
      Returns:
      CheckedPredicate