Class Equivalence<T>
java.lang.Object
cloud.opencode.base.collections.Equivalence<T>
- Type Parameters:
T- the type of objects compared by this equivalence | 此等价关系比较的对象类型
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected abstract booleandoEquivalent(T a, T b) Implementation of equivalence check for non-null values.protected abstract intImplementation of hash code for non-null values.static <T> Equivalence<T> equals()Returns the default equivalence, usingObject.equals(Object).final booleanequivalent(T a, T b) Determines whether the two given objects are equivalent.static <T> Equivalence<T> from(BiPredicate<? super T, ? super T> predicate, ToIntFunction<? super T> hashFunction) Creates an equivalence from a predicate and hash function.final intReturns a hash code for the given object.static <T> Equivalence<T> identity()Returns the identity equivalence, using==.
-
Constructor Details
-
Equivalence
public Equivalence()
-
-
Method Details
-
equals
Returns the default equivalence, usingObject.equals(Object). 返回默认等价关系,使用Object.equals(Object)。- Type Parameters:
T- the type | 类型- Returns:
- the equals-based equivalence | 基于 equals 的等价关系
-
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
-
doEquivalent
-
hash
Returns a hash code for the given object. 返回给定对象的哈希码。- Parameters:
t- the object (may be null) | 对象(可为 null)- Returns:
- the hash code | 哈希码
-
doHash
Implementation of hash code for non-null values. 非空值哈希码的实现。- Parameters:
t- the non-null object | 非空对象- Returns:
- the hash code | 哈希码
-