Record Class Sort

java.lang.Object
java.lang.Record
cloud.opencode.base.web.page.Sort
Record Components:
orders - the sort orders | 排序顺序列表

public record Sort(List<Sort.Order> orders) extends Record
Sort 排序

Represents sorting criteria for queries.

表示查询的排序条件。

Features | 主要功能:

  • Multi-property sort support - 多属性排序支持
  • SQL ORDER BY clause generation - SQL ORDER BY 子句生成
  • Sort string parsing - 排序字符串解析
  • SQL injection prevention via identifier validation - 通过标识符验证防止 SQL 注入

Usage Examples | 使用示例:

// Simple sort
Sort sort = Sort.asc("name");
Sort sort = Sort.desc("createdAt");

// Multi-property sort
Sort sort = Sort.asc("name").andDesc("createdAt");

// Parse sort string
Sort sort = Sort.parse("name,asc;age,desc");

// Generate SQL
String sql = sort.toSql(); // "name ASC, age DESC"

Security | 安全性:

  • Thread-safe: Yes (immutable record) - 线程安全: 是(不可变记录)
  • Null-safe: Yes (null orders default to empty list) - 空值安全: 是(null 排序默认为空列表)
  • SQL injection safe: Yes (identifier validation and keyword blocking) - SQL 注入安全: 是(标识符验证和关键字阻止)

Performance | 性能特性:

  • Time complexity: O(k) for parse() and toSql() where k is the number of sort orders - 时间复杂度: O(k),k 为排序条件数 - parse() 和 toSql() 均线性遍历排序条件
  • Space complexity: O(k) - order list proportional to the number of sort criteria - 空间复杂度: O(k) - 排序条件列表与条件数成正比
Since:
JDK 25, opencode-base-web V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static enum 
    Sort Direction 排序方向
    static final record 
    Sort Order 排序顺序
  • Constructor Summary

    Constructors
    Constructor
    Description
    Sort(List<Sort.Order> orders)
    Compact constructor 紧凑构造函数
  • Method Summary

    Modifier and Type
    Method
    Description
    and(Sort.Order order)
    Add and return new sort 添加并返回新排序
    andAsc(String property)
    Add ascending sort 添加升序
    andDesc(String property)
    Add descending sort 添加降序
    static Sort
    asc(String property)
    Create sort by property ascending 按属性升序创建排序
    static Sort
    by(Sort.Order... orders)
    Create sort by multiple properties 按多个属性创建排序
    static Sort
    by(List<Sort.Order> orders)
    Create sort by multiple properties 按多个属性创建排序
    static Sort
    desc(String property)
    Create sort by property descending 按属性降序创建排序
    final boolean
    Indicates whether some other object is "equal to" this one.
    Get first order 获取第一个排序
    final int
    Returns a hash code value for this object.
    boolean
    Check if sorted 检查是否有排序
    boolean
    Check if unsorted 检查是否无排序
    Returns the value of the orders record component.
    static Sort
    parse(String sortString)
    Parse sort string 解析排序字符串
    Convert to SQL ORDER BY clause 转换为SQL ORDER BY子句
    final String
    Returns a string representation of this record class.
    static Sort
    Create unsorted 创建无排序

    Methods inherited from class Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • Sort

      public Sort(List<Sort.Order> orders)
      Compact constructor 紧凑构造函数
  • Method Details

    • unsorted

      public static Sort unsorted()
      Create unsorted 创建无排序
      Returns:
      the sort | 排序
    • asc

      public static Sort asc(String property)
      Create sort by property ascending 按属性升序创建排序
      Parameters:
      property - the property | 属性
      Returns:
      the sort | 排序
    • desc

      public static Sort desc(String property)
      Create sort by property descending 按属性降序创建排序
      Parameters:
      property - the property | 属性
      Returns:
      the sort | 排序
    • by

      public static Sort by(Sort.Order... orders)
      Create sort by multiple properties 按多个属性创建排序
      Parameters:
      orders - the orders | 排序列表
      Returns:
      the sort | 排序
    • by

      public static Sort by(List<Sort.Order> orders)
      Create sort by multiple properties 按多个属性创建排序
      Parameters:
      orders - the orders | 排序列表
      Returns:
      the sort | 排序
    • parse

      public static Sort parse(String sortString)
      Parse sort string 解析排序字符串

      Format: "property1,asc;property2,desc"

      Parameters:
      sortString - the sort string | 排序字符串
      Returns:
      the sort | 排序
    • and

      public Sort and(Sort.Order order)
      Add and return new sort 添加并返回新排序
      Parameters:
      order - the order to add | 要添加的排序
      Returns:
      the new sort | 新排序
    • andAsc

      public Sort andAsc(String property)
      Add ascending sort 添加升序
      Parameters:
      property - the property | 属性
      Returns:
      the new sort | 新排序
    • andDesc

      public Sort andDesc(String property)
      Add descending sort 添加降序
      Parameters:
      property - the property | 属性
      Returns:
      the new sort | 新排序
    • isSorted

      public boolean isSorted()
      Check if sorted 检查是否有排序
      Returns:
      true if sorted | 如果有排序返回true
    • isUnsorted

      public boolean isUnsorted()
      Check if unsorted 检查是否无排序
      Returns:
      true if unsorted | 如果无排序返回true
    • getFirst

      public Sort.Order getFirst()
      Get first order 获取第一个排序
      Returns:
      the first order or null | 第一个排序或null
    • toSql

      public String toSql()
      Convert to SQL ORDER BY clause 转换为SQL ORDER BY子句
      Returns:
      the SQL clause | SQL子句
    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared with Objects::equals(Object,Object).
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • orders

      public List<Sort.Order> orders()
      Returns the value of the orders record component.
      Returns:
      the value of the orders record component