Class Ordering<T>

java.lang.Object
cloud.opencode.base.core.Ordering<T>
Type Parameters:
T - the type being compared - 被比较的类型
All Implemented Interfaces:
Comparator<T>

public abstract class Ordering<T> extends Object implements Comparator<T>
Ordering - Fluent comparator builder 排序器 - 流式比较器构建器

Provides a fluent API for building complex comparators with null handling, reverse ordering, and chaining support.

提供用于构建复杂比较器的流式 API,支持空值处理、反向排序和链式调用。

Usage Examples | 使用示例:

// Natural ordering
Ordering<String> natural = Ordering.natural();
natural.compare("a", "b");  // -1

// Reverse ordering
Ordering<String> reversed = Ordering.<String>natural().reversed();
reversed.compare("a", "b");  // 1

// Nulls first/last
Ordering<String> nullsFirst = Ordering.<String>natural().nullsFirst();
nullsFirst.compare(null, "a");  // -1

// By key extraction
Ordering<Person> byAge = Ordering.from(Person::getAge);
Ordering<Person> byName = Ordering.from(Person::getName);

// Compound ordering
Ordering<Person> compound = byAge.thenComparing(byName);

// Min/Max operations
String min = Ordering.<String>natural().min("apple", "banana");  // "apple"
List<String> top3 = Ordering.<String>natural().leastOf(list, 3);

Features | 主要功能:

  • Natural and custom comparator ordering - 自然和自定义比较器排序
  • Null handling: nullsFirst/nullsLast - 空值处理: nullsFirst/nullsLast
  • Compound ordering with thenComparing - 通过thenComparing复合排序
  • Min/Max and top-k element selection - 最小/最大和Top-K元素选择

Security | 安全性:

  • Thread-safe: Yes (immutable after creation) - 线程安全: 是(创建后不可变)
  • Null-safe: Yes, with nullsFirst()/nullsLast() - 空值安全: 是,通过nullsFirst()/nullsLast()
Since:
JDK 25, opencode-base-core V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Details

    • Ordering

      public Ordering()
  • Method Details

    • natural

      public static <T extends Comparable<? super T>> Ordering<T> natural()
      Returns an ordering that uses the natural order of the values. 返回使用值的自然顺序的排序器。
      Type Parameters:
      T - the comparable type - 可比较类型
      Returns:
      the natural ordering - 自然排序器
    • from

      public static <T> Ordering<T> from(Comparator<T> comparator)
      Creates an ordering from an existing comparator. 从现有比较器创建排序器。
      Type Parameters:
      T - the type being compared - 被比较的类型
      Parameters:
      comparator - the comparator - 比较器
      Returns:
      the ordering - 排序器
    • from

      public static <T, U extends Comparable<? super U>> Ordering<T> from(Function<? super T, ? extends U> keyExtractor)
      Creates an ordering by extracting a comparable key. 通过提取可比较的键创建排序器。
      Type Parameters:
      T - the type being compared - 被比较的类型
      U - the key type - 键类型
      Parameters:
      keyExtractor - the key extractor - 键提取器
      Returns:
      the ordering - 排序器
    • from

      public static <T,U> Ordering<T> from(Function<? super T, ? extends U> keyExtractor, Comparator<? super U> keyComparator)
      Creates an ordering by extracting a key and using a comparator. 通过提取键并使用比较器创建排序器。
      Type Parameters:
      T - the type being compared - 被比较的类型
      U - the key type - 键类型
      Parameters:
      keyExtractor - the key extractor - 键提取器
      keyComparator - the key comparator - 键比较器
      Returns:
      the ordering - 排序器
    • allEqual

      public static <T> Ordering<T> allEqual()
      Returns an ordering that treats all values as equal. 返回将所有值视为相等的排序器。
      Type Parameters:
      T - the type - 类型
      Returns:
      the all-equal ordering - 全等排序器
    • explicit

      @SafeVarargs public static <T> Ordering<T> explicit(T... valuesInOrder)
      Returns an ordering based on the iteration order of explicit values. 返回基于显式值迭代顺序的排序器。
      Type Parameters:
      T - the type - 类型
      Parameters:
      valuesInOrder - the values in order - 按顺序排列的值
      Returns:
      the explicit ordering - 显式排序器
    • explicit

      public static <T> Ordering<T> explicit(List<T> valuesInOrder)
      Returns an ordering based on the iteration order of explicit values. 返回基于显式值迭代顺序的排序器。
      Type Parameters:
      T - the type - 类型
      Parameters:
      valuesInOrder - the values in order - 按顺序排列的值
      Returns:
      the explicit ordering - 显式排序器
    • reversed

      public Ordering<T> reversed()
      Returns the reverse ordering. 返回反向排序器。
      Specified by:
      reversed in interface Comparator<T>
      Returns:
      the reversed ordering - 反向排序器
    • nullsFirst

      public Ordering<T> nullsFirst()
      Returns an ordering that treats null as less than all other values. 返回将 null 视为小于所有其他值的排序器。
      Returns:
      the ordering with nulls first - 空值优先的排序器
    • nullsLast

      public Ordering<T> nullsLast()
      Returns an ordering that treats null as greater than all other values. 返回将 null 视为大于所有其他值的排序器。
      Returns:
      the ordering with nulls last - 空值最后的排序器
    • thenComparing

      public Ordering<T> thenComparing(Comparator<? super T> secondary)
      Returns a compound ordering with a secondary comparator. 返回具有次要比较器的复合排序器。
      Specified by:
      thenComparing in interface Comparator<T>
      Parameters:
      secondary - the secondary comparator - 次要比较器
      Returns:
      the compound ordering - 复合排序器
    • thenComparing

      public <U extends Comparable<? super U>> Ordering<T> thenComparing(Function<? super T, ? extends U> keyExtractor)
      Returns a compound ordering using a key extractor. 使用键提取器返回复合排序器。
      Specified by:
      thenComparing in interface Comparator<T>
      Type Parameters:
      U - the key type - 键类型
      Parameters:
      keyExtractor - the key extractor - 键提取器
      Returns:
      the compound ordering - 复合排序器
    • onResultOf

      public <F> Ordering<F> onResultOf(Function<F, ? extends T> function)
      Returns an ordering that applies a function before comparing. 返回在比较前应用函数的排序器。
      Type Parameters:
      F - the input type - 输入类型
      Parameters:
      function - the function to apply - 要应用的函数
      Returns:
      the new ordering - 新排序器
    • min

      public T min(T a, T b)
      Returns the minimum of two values. 返回两个值中的最小值。
    • min

      @SafeVarargs public final T min(T first, T second, T... rest)
      Returns the minimum of the given values. 返回给定值中的最小值。
    • min

      public T min(Iterable<T> iterable)
      Returns the minimum value in an iterable. 返回可迭代对象中的最小值。
    • max

      public T max(T a, T b)
      Returns the maximum of two values. 返回两个值中的最大值。
    • max

      @SafeVarargs public final T max(T first, T second, T... rest)
      Returns the maximum of the given values. 返回给定值中的最大值。
    • max

      public T max(Iterable<T> iterable)
      Returns the maximum value in an iterable. 返回可迭代对象中的最大值。
    • leastOf

      public List<T> leastOf(Iterable<T> iterable, int k)
      Returns the k smallest elements in the given iterable. 返回给定可迭代对象中最小的 k 个元素。
      Parameters:
      iterable - the iterable - 可迭代对象
      k - the number of elements - 元素数量
      Returns:
      the k smallest elements in sorted order - 按排序顺序排列的最小 k 个元素
    • greatestOf

      public List<T> greatestOf(Iterable<T> iterable, int k)
      Returns the k greatest elements in the given iterable. 返回给定可迭代对象中最大的 k 个元素。
      Parameters:
      iterable - the iterable - 可迭代对象
      k - the number of elements - 元素数量
      Returns:
      the k greatest elements in descending order - 按降序排列的最大 k 个元素
    • sortedCopy

      public List<T> sortedCopy(Iterable<T> iterable)
      Returns a sorted copy of the given iterable. 返回给定可迭代对象的排序副本。
    • immutableSortedCopy

      public List<T> immutableSortedCopy(Iterable<T> iterable)
      Returns an immutable sorted copy of the given iterable. 返回给定可迭代对象的不可变排序副本。
    • isOrdered

      public boolean isOrdered(Iterable<T> iterable)
      Returns true if the iterable is sorted according to this ordering. 如果可迭代对象按此排序器排序返回 true。
    • isStrictlyOrdered

      public boolean isStrictlyOrdered(Iterable<T> iterable)
      Returns true if the iterable is strictly sorted according to this ordering. 如果可迭代对象按此排序器严格排序返回 true。