Class ComparisonChain
java.lang.Object
cloud.opencode.base.collections.ComparisonChain
ComparisonChain - Fluent comparator chain for multi-field comparisons
ComparisonChain - 用于多字段比较的流式比较器链
A utility for performing chained comparisons within compareTo() methods.
Once a non-zero comparison result is found, all subsequent comparisons are short-circuited.
用于在 compareTo() 方法中执行链式比较的工具类。
一旦找到非零比较结果,所有后续比较将被短路跳过。
Features | 主要功能:
- Short-circuit evaluation on first difference - 第一个差异处短路求值
- Supports all primitives (int, long, float, double, boolean) - 支持所有基本类型
- Custom Comparator support - 支持自定义 Comparator
- Boolean ordering: true-first or false-first - 布尔排序: true 优先或 false 优先
Usage Examples | 使用示例:
// Multi-field comparison in compareTo() - compareTo() 中的多字段比较
public int compareTo(Person other) {
return ComparisonChain.start()
.compare(this.lastName, other.lastName)
.compare(this.firstName, other.firstName)
.compare(this.age, other.age)
.result();
}
Performance | 性能特性:
- Time complexity: O(1) per chained call after short-circuit - 短路后每次链式调用 O(1)
- No heap allocations after short-circuit (singleton reuse) - 短路后无堆分配(单例复用)
Security | 安全性:
- Thread-safe: Yes (immutable singletons) - 线程安全: 是(不可变单例)
- Null-safe: No (null arguments cause NullPointerException) - 空值安全: 否(null 参数导致 NullPointerException)
- Since:
- JDK 25, opencode-base-collections V1.0.3
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionabstract ComparisonChaincompare(double left, double right) Compares twodoublevalues.abstract ComparisonChaincompare(float left, float right) Compares twofloatvalues.abstract ComparisonChaincompare(int left, int right) Compares twointvalues.abstract ComparisonChaincompare(long left, long right) Compares twolongvalues.abstract ComparisonChaincompare(Comparable<?> left, Comparable<?> right) Compares twoComparablevalues using their natural ordering.abstract <T> ComparisonChaincompare(T left, T right, Comparator<T> comparator) Compares two values using the specifiedComparator.abstract ComparisonChaincompareFalseFirst(boolean left, boolean right) Compares two booleans, sortingfalsebeforetrue.abstract ComparisonChaincompareTrueFirst(boolean left, boolean right) Compares two booleans, sortingtruebeforefalse.abstract intresult()Returns the final comparison result.static ComparisonChainstart()Starts a new comparison chain.
-
Method Details
-
start
Starts a new comparison chain. 启动一个新的比较链。- Returns:
- the active comparison chain - 活动的比较链
-
compare
Compares twoComparablevalues using their natural ordering. 使用自然排序比较两个Comparable值。- Parameters:
left- the left value - 左值right- the right value - 右值- Returns:
- this chain for fluent chaining - 用于流式链接的此链
-
compare
Compares two values using the specifiedComparator. 使用指定的Comparator比较两个值。- Type Parameters:
T- the type of values being compared - 被比较值的类型- Parameters:
left- the left value - 左值right- the right value - 右值comparator- the comparator to use - 使用的比较器- Returns:
- this chain for fluent chaining - 用于流式链接的此链
-
compare
Compares twointvalues. 比较两个int值。- Parameters:
left- the left value - 左值right- the right value - 右值- Returns:
- this chain for fluent chaining - 用于流式链接的此链
-
compare
Compares twolongvalues. 比较两个long值。- Parameters:
left- the left value - 左值right- the right value - 右值- Returns:
- this chain for fluent chaining - 用于流式链接的此链
-
compare
Compares twodoublevalues. 比较两个double值。- Parameters:
left- the left value - 左值right- the right value - 右值- Returns:
- this chain for fluent chaining - 用于流式链接的此链
-
compare
Compares twofloatvalues. 比较两个float值。- Parameters:
left- the left value - 左值right- the right value - 右值- Returns:
- this chain for fluent chaining - 用于流式链接的此链
-
compareTrueFirst
Compares two booleans, sortingtruebeforefalse. 比较两个布尔值,true排在false之前。- Parameters:
left- the left value - 左值right- the right value - 右值- Returns:
- this chain for fluent chaining - 用于流式链接的此链
-
compareFalseFirst
Compares two booleans, sortingfalsebeforetrue. 比较两个布尔值,false排在true之前。- Parameters:
left- the left value - 左值right- the right value - 右值- Returns:
- this chain for fluent chaining - 用于流式链接的此链
-
result
public abstract int result()Returns the final comparison result. 返回最终比较结果。- Returns:
- a negative integer, zero, or a positive integer as the first differing comparison was less than, equal to, or greater than 负整数、零或正整数,取决于第一个不同比较的结果
-