Class Equivalence<T>

java.lang.Object
cloud.opencode.base.collections.Equivalence<T>
Type Parameters:
T - the type of objects compared by this equivalence | 此等价关系比较的对象类型

public abstract class Equivalence<T> extends Object
Equivalence - Strategy for determining equivalence between objects Equivalence - 确定对象等价性的策略

An equivalence determines whether two objects are considered equivalent. This is a generalization of Object.equals(Object), allowing custom equivalence relations.

等价关系确定两个对象是否被视为等价。这是 Object.equals(Object) 的泛化,允许自定义等价关系。

Usage Examples | 使用示例:

// Natural equivalence (using equals)
Equivalence<String> natural = Equivalence.equals();
natural.equivalent("a", "a"); // true

// Identity equivalence (using ==)
Equivalence<String> identity = Equivalence.identity();

// Custom equivalence
Equivalence<String> caseInsensitive = Equivalence.from(
    String::equalsIgnoreCase,
    s -> s.toLowerCase().hashCode()
);

Features | 主要功能:

  • Custom equivalence relations - 自定义等价关系
  • Natural equals and identity equivalence - 自然equals和引用相等
  • Custom hash function support - 自定义哈希函数支持

Security | 安全性:

  • Thread-safe: Yes (immutable) - 是(不可变)
  • Null-safe: Yes (handles null elements) - 是(处理null元素)
Since:
JDK 25, opencode-base-collections V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Details

    • Equivalence

      public Equivalence()
  • Method Details

    • equals

      public static <T> Equivalence<T> equals()
      Returns the default equivalence, using Object.equals(Object). 返回默认等价关系,使用 Object.equals(Object)
      Type Parameters:
      T - the type | 类型
      Returns:
      the equals-based equivalence | 基于 equals 的等价关系
    • identity

      public static <T> Equivalence<T> identity()
      Returns the identity equivalence, using ==. 返回身份等价关系,使用 ==
      Type Parameters:
      T - the type | 类型
      Returns:
      the identity equivalence | 身份等价关系
    • from

      public static <T> Equivalence<T> from(BiPredicate<? super T, ? super T> predicate, ToIntFunction<? super T> hashFunction)
      Creates an equivalence from a predicate and hash function. 从谓词和哈希函数创建等价关系。
      Type Parameters:
      T - the type | 类型
      Parameters:
      predicate - the equivalence predicate | 等价谓词
      hashFunction - the hash function | 哈希函数
      Returns:
      a new equivalence | 新的等价关系
    • equivalent

      public final boolean equivalent(T a, T b)
      Determines whether the two given objects are equivalent. 确定两个给定对象是否等价。
      Parameters:
      a - first object (may be null) | 第一个对象(可为 null)
      b - second object (may be null) | 第二个对象(可为 null)
      Returns:
      true if equivalent | 如果等价则返回 true
    • doEquivalent

      protected abstract boolean doEquivalent(T a, T b)
      Implementation of equivalence check for non-null values. 非空值等价检查的实现。
      Parameters:
      a - first non-null object | 第一个非空对象
      b - second non-null object | 第二个非空对象
      Returns:
      true if equivalent | 如果等价则返回 true
    • hash

      public final int hash(T t)
      Returns a hash code for the given object. 返回给定对象的哈希码。
      Parameters:
      t - the object (may be null) | 对象(可为 null)
      Returns:
      the hash code | 哈希码
    • doHash

      protected abstract int doHash(T t)
      Implementation of hash code for non-null values. 非空值哈希码的实现。
      Parameters:
      t - the non-null object | 非空对象
      Returns:
      the hash code | 哈希码