Record Class PageResult<T>

java.lang.Object
java.lang.Record
cloud.opencode.base.web.page.PageResult<T>
Type Parameters:
T - the item type | 项类型
Record Components:
items - the items | 项列表
pageInfo - the page info | 分页信息

public record PageResult<T>(List<T> items, PageInfo pageInfo) extends Record
Page Result 分页结果

Paginated list result.

分页列表结果。

Features | 主要功能:

  • Immutable paginated result container - 不可变的分页结果容器
  • Page navigation helpers (hasNext, hasPrevious) - 分页导航辅助方法
  • Functional mapping support - 函数式映射支持
  • Delegate methods for page metadata - 分页元数据的委托方法

Usage Examples | 使用示例:

// Create page result
PageResult<User> result = PageResult.of(userList, 100, 1, 10);

// Map items
PageResult<UserDTO> dtoResult = result.map(user -> toDTO(user));

// Check navigation
boolean hasMore = result.hasNext();

Security | 安全性:

  • Thread-safe: Yes (immutable record) - 线程安全: 是(不可变记录)
  • Null-safe: Yes (null items default to empty list) - 空值安全: 是(null 项默认为空列表)
Since:
JDK 25, opencode-base-web V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    PageResult(List<T> items, PageInfo pageInfo)
    Compact constructor 紧凑构造函数
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T> PageResult<T>
    Create empty result 创建空结果
    static <T> PageResult<T>
    empty(int page, int size)
    Create empty result with page info 创建带分页信息的空结果
    final boolean
    Indicates whether some other object is "equal to" this one.
    int
    Get item count 获取项数量
    Get first item 获取第一项
    Get last item 获取最后一项
    int
    Get page number 获取页码
    int
    Get page size 获取页大小
    long
    Get total count 获取总数
    int
    Get total pages 获取总页数
    final int
    Returns a hash code value for this object.
    boolean
    Check if has next page 检查是否有下一页
    boolean
    Check if has previous page 检查是否有上一页
    boolean
    Check if empty 检查是否为空
    boolean
    Check if first page 检查是否第一页
    boolean
    Check if last page 检查是否最后一页
    Returns the value of the items record component.
    <R> PageResult<R>
    map(Function<T,R> mapper)
    Map items to another type 将项映射为另一种类型
    <R> PageResult<R>
    mapList(Function<List<T>,List<R>> mapper)
    Map entire list to another type 转换整个列表为另一种类型
    static <T> PageResult<T>
    of(List<T> items, long total, int page, int size)
    Create page result 创建分页结果
    static <T> PageResult<T>
    of(List<T> items, long total, PageRequest request)
    Create page result from request 从请求创建分页结果
    static <T> PageResult<T>
    of(List<T> items, PageInfo pageInfo)
    Create page result 创建分页结果
    Returns the value of the pageInfo record component.
    static <T> PageResult<T>
    single(List<T> list)
    Create single item result from list (takes first item) 从列表创建单项结果(取第一项)
    static <T> PageResult<T>
    single(T item)
    Create single item result 创建单项结果
    final String
    Returns a string representation of this record class.

    Methods inherited from class Object

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

    • PageResult

      public PageResult(List<T> items, PageInfo pageInfo)
      Compact constructor 紧凑构造函数
  • Method Details

    • of

      public static <T> PageResult<T> of(List<T> items, PageInfo pageInfo)
      Create page result 创建分页结果
      Type Parameters:
      T - the item type | 项类型
      Parameters:
      items - the items | 项列表
      pageInfo - the page info | 分页信息
      Returns:
      the result | 结果
    • of

      public static <T> PageResult<T> of(List<T> items, long total, int page, int size)
      Create page result 创建分页结果
      Type Parameters:
      T - the item type | 项类型
      Parameters:
      items - the items | 项列表
      total - the total count | 总数
      page - the page number | 页码
      size - the page size | 页大小
      Returns:
      the result | 结果
    • of

      public static <T> PageResult<T> of(List<T> items, long total, PageRequest request)
      Create page result from request 从请求创建分页结果
      Type Parameters:
      T - the item type | 项类型
      Parameters:
      items - the items | 项列表
      total - the total count | 总数
      request - the page request | 分页请求
      Returns:
      the result | 结果
    • empty

      public static <T> PageResult<T> empty()
      Create empty result 创建空结果
      Type Parameters:
      T - the item type | 项类型
      Returns:
      the result | 结果
    • empty

      public static <T> PageResult<T> empty(int page, int size)
      Create empty result with page info 创建带分页信息的空结果
      Type Parameters:
      T - the item type | 项类型
      Parameters:
      page - the page number | 页码
      size - the page size | 页大小
      Returns:
      the result | 结果
    • single

      public static <T> PageResult<T> single(T item)
      Create single item result 创建单项结果
      Type Parameters:
      T - the item type | 项类型
      Parameters:
      item - the single item | 单个项
      Returns:
      the result with single item | 包含单项的结果
    • single

      public static <T> PageResult<T> single(List<T> list)
      Create single item result from list (takes first item) 从列表创建单项结果(取第一项)
      Type Parameters:
      T - the item type | 项类型
      Parameters:
      list - the list | 列表
      Returns:
      the result with single item | 包含单项的结果
    • getPage

      public int getPage()
      Get page number 获取页码
      Returns:
      the page number | 页码
    • getSize

      public int getSize()
      Get page size 获取页大小
      Returns:
      the page size | 页大小
    • getTotal

      public long getTotal()
      Get total count 获取总数
      Returns:
      the total count | 总数
    • getTotalPages

      public int getTotalPages()
      Get total pages 获取总页数
      Returns:
      the total pages | 总页数
    • hasNext

      public boolean hasNext()
      Check if has next page 检查是否有下一页
      Returns:
      true if has next | 如果有下一页返回true
    • hasPrevious

      public boolean hasPrevious()
      Check if has previous page 检查是否有上一页
      Returns:
      true if has previous | 如果有上一页返回true
    • isFirst

      public boolean isFirst()
      Check if first page 检查是否第一页
      Returns:
      true if first | 如果是第一页返回true
    • isLast

      public boolean isLast()
      Check if last page 检查是否最后一页
      Returns:
      true if last | 如果是最后一页返回true
    • isEmpty

      public boolean isEmpty()
      Check if empty 检查是否为空
      Returns:
      true if empty | 如果为空返回true
    • getCount

      public int getCount()
      Get item count 获取项数量
      Returns:
      the item count | 项数量
    • map

      public <R> PageResult<R> map(Function<T,R> mapper)
      Map items to another type 将项映射为另一种类型
      Type Parameters:
      R - the result type | 结果类型
      Parameters:
      mapper - the mapper function | 映射函数
      Returns:
      the new page result | 新分页结果
    • mapList

      public <R> PageResult<R> mapList(Function<List<T>,List<R>> mapper)
      Map entire list to another type 转换整个列表为另一种类型
      Type Parameters:
      R - the result type | 结果类型
      Parameters:
      mapper - the list mapper function | 列表映射函数
      Returns:
      the new page result | 新分页结果
    • getFirst

      public T getFirst()
      Get first item 获取第一项
      Returns:
      the first item or null | 第一项或null
    • getLast

      public T getLast()
      Get last item 获取最后一项
      Returns:
      the last item or null | 最后一项或null
    • 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.
    • items

      public List<T> items()
      Returns the value of the items record component.
      Returns:
      the value of the items record component
    • pageInfo

      public PageInfo pageInfo()
      Returns the value of the pageInfo record component.
      Returns:
      the value of the pageInfo record component