Framework-agnostic pagination for offset, cursor, and keyset patterns
implementation com.philiprehberger:paginationFramework-agnostic pagination for offset, cursor, and keyset patterns.
implementation("com.philiprehberger:pagination:0.2.0")
<dependency>
<groupId>com.philiprehberger</groupId>
<artifactId>pagination</artifactId>
<version>0.2.0</version>
</dependency>
import com.philiprehberger.pagination.*
// Offset-based
val page = paginate(totalItems = 250, page = 3, pageSize = 20)
page.totalPages // 13
page.hasNext // true
page.offset // 40
// Cursor-based
val encoded = Cursor.encode("user:123")
val decoded = Cursor.decode(encoded) // "user:123"
// Keyset
val result = keysetPaginate(items, pageSize = 20)
result.hasMore // true if more items available
// Serialize metadata to a map (for JSON responses)
val map = page.toMap() // Map<String, Any>
// { "currentPage": 3, "pageSize": 20, "totalItems": 250, ... }
| Function / Class | Description |
|---|---|
paginate(totalItems, page, pageSize) | Calculate page metadata |
pageResponse(items, page, pageSize, totalItems) | Page of items with metadata |
CursorPage<T> | Cursor-based page with items, cursor, hasMore |
Cursor.encode(value) / Cursor.decode(encoded) | Opaque cursor encoding |
PageInfo.toMap() | Serialize pagination metadata to Map<String, Any> |
keysetPaginate(items, pageSize) | Keyset-based pagination |
./gradlew test # Run tests
./gradlew build # Build JAR
If you find this project useful: