- Record Components:
key- the unique key that breaks ties and addresses a row; it orders last.keyDescending-trueto order the key descending.sort- the sort fields that order before the key, in precedence order; each carries its own direction.size- the maximum number of results per window (must be positive).position- the row to continue after or before, ornullto start at the beginning.
A Scrollable is the scrolling counterpart of Pageable. Where a Pageable navigates by
page number, a Scrollable navigates by keyset: it names the sort fields, a unique key that breaks ties
and makes every row addressable, and optionally the row to continue after or before. The ordering and the size
belong to the request; the position is what a Window hands back as Window.next() and
Window.previous(), and what a cursor string carries across a network boundary.
// Newest first, tiebreak on id, twenty per window
var latest = Scrollable.of(Post_.id, 20).sortByDescending(Post_.createdAt);
// Last name, then first name, then id
var byName = Scrollable.of(User_.id, 20).sortBy(User_.lastName).sortBy(User_.firstName);
// The next request from a client's cursor string, with the size the client asks for
var next = Scrollable.of(User_.id, size).sortBy(User_.lastName).from(cursor);
The serialized cursor is opaque and URL-safe, but it is not tamper-proof. If the cursor is exposed to untrusted clients, sign or wrap it at a higher layer.
- Since:
- 1.11
-
Constructor Summary
ConstructorsConstructorDescriptionScrollable(Metamodel.Key<T, ?> key, boolean keyDescending, List<Order> sort, int size, @Nullable Position position) Creates an instance of aScrollablerecord class. -
Method Summary
Modifier and TypeMethodDescriptionReturns this request continuing after the row with the given values.Returns this request continuing before the row with the given values.Returns this request with the key ordered descending.final booleanIndicates whether some other object is "equal to" this one.Returns this request at the position a cursor string carries, as produced bytoCursor(),Window.nextCursor()orWindow.previousCursor().final inthashCode()Returns a hash code value for this object.Metamodel.Key<T, ?> key()Returns the value of thekeyrecord component.booleanReturns the value of thekeyDescendingrecord component.static <T extends Data>
Scrollable<T> of(Metamodel.Key<T, ?> key, int size) Creates a request for the first window, ordered by the key ascending.orders()Returns the complete ordering: the sort fields, then the key with its direction.@Nullable Positionposition()Returns the value of thepositionrecord component.intsize()Returns the value of thesizerecord component.size(int size) Returns this request with a different window size.sort()Returns the value of thesortrecord component.Returns this request with an ascending sort field appended before the key.sortByDescending(Metamodel<T, ?> field) Returns this request with a descending sort field appended before the key.toCursor()Serializes the position of this request into an opaque, URL-safe string.final StringtoString()Returns a string representation of this record class.
-
Constructor Details
-
Scrollable
public Scrollable(Metamodel.Key<T, ?> key, boolean keyDescending, List<Order> sort, int size, @Nullable Position position) Creates an instance of aScrollablerecord class.- Parameters:
key- the value for thekeyrecord componentkeyDescending- the value for thekeyDescendingrecord componentsort- the value for thesortrecord componentsize- the value for thesizerecord componentposition- the value for thepositionrecord component
-
-
Method Details
-
of
Creates a request for the first window, ordered by the key ascending.- Type Parameters:
T- the entity type.- Parameters:
key- the unique key field.size- the maximum number of results per window.- Returns:
- the request.
-
descending
Returns this request with the key ordered descending. Sort fields keep their own direction.// Newest ids first var latest = users.scroll(Scrollable.of(User_.id, 20).descending());- Returns:
- the request with the key descending.
- Since:
- 1.14
-
sortBy
Returns this request with an ascending sort field appended before the key.- Parameters:
field- the field to sort by; must not allow NULL values.- Returns:
- the request with the sort field added.
- Since:
- 1.14
-
sortByDescending
Returns this request with a descending sort field appended before the key.- Parameters:
field- the field to sort by; must not allow NULL values.- Returns:
- the request with the sort field added.
- Since:
- 1.14
-
size
Returns this request with a different window size.- Parameters:
size- the maximum number of results per window.- Returns:
- the request with the size.
- Since:
- 1.14
-
after
Returns this request continuing after the row with the given values.- Parameters:
values- one value per sort field in sort order, then the key value.- Returns:
- the request positioned after that row.
- Since:
- 1.14
-
before
Returns this request continuing before the row with the given values. The window comes back in sort order, the same as a window reached byafter(Object...).- Parameters:
values- one value per sort field in sort order, then the key value.- Returns:
- the request positioned before that row.
- Since:
- 1.14
-
from
Returns this request at the position a cursor string carries, as produced bytoCursor(),Window.nextCursor()orWindow.previousCursor().The cursor was issued for one ordering, and this request must state the same key, sort fields and directions; a cursor from another ordering is refused.
- Parameters:
cursor- the cursor string.- Returns:
- the request at the cursor's position.
- Throws:
InvalidCursorException- if the cursor is malformed, from an earlier format, issued for another ordering or codec registry, or carries a value of the wrong type.- Since:
- 1.14
-
orders
Returns the complete ordering: the sort fields, then the key with its direction.- Returns:
- the orders, in precedence.
- Since:
- 1.14
-
toCursor
Serializes the position of this request into an opaque, URL-safe string. The cursor carries the position only: a fingerprint of the ordering, whether to continue after or before the row, and the row's values. The size stays with the request, so a client may ask for another size on the next request.- Returns:
- a URL-safe Base64-encoded cursor string.
- Throws:
IllegalStateException- if this request has no position, or a value type is unsupported.- Since:
- 1.11
-
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. -
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. -
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 '=='. -
key
Returns the value of thekeyrecord component.- Returns:
- the value of the
keyrecord component
-
keyDescending
public boolean keyDescending()Returns the value of thekeyDescendingrecord component.- Returns:
- the value of the
keyDescendingrecord component
-
sort
Returns the value of thesortrecord component.- Returns:
- the value of the
sortrecord component
-
size
public int size()Returns the value of thesizerecord component.- Returns:
- the value of the
sizerecord component
-
position
Returns the value of thepositionrecord component.- Returns:
- the value of the
positionrecord component
-