Record Class Page<T>

java.lang.Object
java.lang.Record
cloud.opencode.base.core.page.Page<T>
Type Parameters:
T - the record type | 记录类型
Record Components:
current - the page number (1-based) | 页码(从 1 开始)
size - the page size | 每页大小
total - the total record count | 总记录数
records - the current page records (unmodifiable) | 当前页记录(不可修改)

public record Page<T>(long current, long size, long total, List<T> records) extends Record
Page - Immutable pagination result container 分页 - 不可变分页结果容器

Holds paginated query results with metadata including current page, page size, total count, and computed properties like total pages. This is an immutable record — all fields are set at construction time and the records list is defensively copied.

保存分页查询结果及元数据,包括当前页、页大小、总数以及总页数等计算属性。 这是一个不可变记录——所有字段在构造时设置,记录列表进行防御性复制。

Usage Examples | 使用示例:

Page<User> page = Page.of(1, 10, 100, userList);
long totalPages = page.pages(); // 10
Page<UserDto> dtoPage = page.map(UserDto::from);
Page<User> empty = Page.empty(10);

Features | 主要功能:

  • Immutable pagination result container with metadata - 带元数据的不可变分页结果容器
  • Computed properties: total pages, hasNext, hasPrevious, offset - 计算属性: 总页数、是否有下一页/上一页、偏移量
  • Type-safe mapping via map(Function) - 通过 map(Function) 进行类型安全映射
  • Defensive copy of records list - 记录列表的防御性复制
  • Generic type parameter for record type - 记录类型的泛型参数

Security | 安全性:

  • Thread-safe: Yes (immutable record) - 线程安全: 是(不可变记录)
  • Null-safe: Yes, records must not be null - 空值安全: 是,记录列表不能为 null
Since:
JDK 25, opencode-base-core V1.0.3
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    Page(long current, long size, long total, List<T> records)
    Compact constructor with validation and defensive copy.
  • Method Summary

    Modifier and Type
    Method
    Description
    long
    Returns the value of the current record component.
    static <T> Page<T>
    empty(long size)
    Creates an empty Page with no records and zero total.
    final boolean
    Indicates whether some other object is "equal to" this one.
    final int
    Returns a hash code value for this object.
    boolean
    Returns true if there is a next page.
    boolean
    Returns true if there is a previous page (i.e., current > 1).
    <U> Page<U>
    map(Function<T,U> mapper)
    Maps the records of this page using the given function, preserving pagination metadata.
    static <T> Page<T>
    of(long current, long size, long total, List<T> records)
    Creates a new Page with the given parameters.
    long
    Computes the zero-based offset for the current page.
    long
    Computes the total number of pages.
    Returns the value of the records record component.
    long
    Returns the value of the size record component.
    Returns a string representation of this record class.
    long
    Returns the value of the total record component.

    Methods inherited from class Object

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

    • Page

      public Page(long current, long size, long total, List<T> records)
      Compact constructor with validation and defensive copy. 紧凑构造器,包含验证和防御性复制。
  • Method Details

    • of

      public static <T> Page<T> of(long current, long size, long total, List<T> records)
      Creates a new Page with the given parameters. 使用给定参数创建新的分页对象。
      Type Parameters:
      T - the record type | 记录类型
      Parameters:
      current - the page number (1-based) | 页码(从 1 开始)
      size - the page size | 每页大小
      total - the total record count | 总记录数
      records - the current page records | 当前页记录
      Returns:
      a new Page instance | 新的分页实例
      Since:
      JDK 25, opencode-base-core V1.0.3
    • empty

      public static <T> Page<T> empty(long size)
      Creates an empty Page with no records and zero total. 创建一个没有记录且总数为零的空分页对象。
      Type Parameters:
      T - the record type | 记录类型
      Parameters:
      size - the page size | 每页大小
      Returns:
      an empty Page instance | 空分页实例
      Since:
      JDK 25, opencode-base-core V1.0.3
    • pages

      public long pages()
      Computes the total number of pages. 计算总页数。
      Returns:
      the total number of pages, or 0 if total is 0 | 总页数,若总数为 0 则返回 0
      Since:
      JDK 25, opencode-base-core V1.0.3
    • hasNext

      public boolean hasNext()
      Returns true if there is a next page. 如果有下一页则返回 true
      Returns:
      whether a next page exists | 是否存在下一页
    • hasPrevious

      public boolean hasPrevious()
      Returns true if there is a previous page (i.e., current > 1). 如果有上一页(即 current > 1)则返回 true
      Returns:
      whether a previous page exists | 是否存在上一页
    • offset

      public long offset()
      Computes the zero-based offset for the current page. 计算当前页的零基偏移量。
      Returns:
      the offset: (current - 1) * size | 偏移量
    • map

      public <U> Page<U> map(Function<T,U> mapper)
      Maps the records of this page using the given function, preserving pagination metadata. 使用给定函数映射本页记录,保留分页元数据。
      Type Parameters:
      U - the target record type | 目标记录类型
      Parameters:
      mapper - the mapping function | 映射函数
      Returns:
      a new Page with mapped records | 包含映射后记录的新分页对象
      Throws:
      NullPointerException - if mapper is null | 如果 mapper 为 null
      Since:
      JDK 25, opencode-base-core V1.0.3
    • toString

      public 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. Reference components are compared with Objects::equals(Object,Object); primitive components are compared with the compare method from their corresponding wrapper classes.
      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.
    • current

      public long current()
      Returns the value of the current record component.
      Returns:
      the value of the current record component
    • size

      public long size()
      Returns the value of the size record component.
      Returns:
      the value of the size record component
    • total

      public long total()
      Returns the value of the total record component.
      Returns:
      the value of the total record component
    • records

      public List<T> records()
      Returns the value of the records record component.
      Returns:
      the value of the records record component