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) | 当前页记录(不可修改)
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 -
Method Summary
Modifier and TypeMethodDescriptionlongcurrent()Returns the value of thecurrentrecord component.static <T> Page<T> empty(long size) Creates an empty Page with no records and zero total.final booleanIndicates whether some other object is "equal to" this one.final inthashCode()Returns a hash code value for this object.booleanhasNext()Returnstrueif there is a next page.booleanReturnstrueif there is a previous page (i.e., current > 1).<U> Page<U> Maps the records of this page using the given function, preserving pagination metadata.static <T> Page<T> Creates a new Page with the given parameters.longoffset()Computes the zero-based offset for the current page.longpages()Computes the total number of pages.records()Returns the value of therecordsrecord component.longsize()Returns the value of thesizerecord component.toString()Returns a string representation of this record class.longtotal()Returns the value of thetotalrecord component.
-
Constructor Details
-
Page
-
-
Method Details
-
of
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
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()Returnstrueif there is a next page. 如果有下一页则返回true。- Returns:
- whether a next page exists | 是否存在下一页
-
hasPrevious
public boolean hasPrevious()Returnstrueif 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
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
-
hashCode
-
equals
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 withObjects::equals(Object,Object); primitive components are compared with thecomparemethod from their corresponding wrapper classes. -
current
-
size
-
total
-
records
-