java.lang.Object
java.lang.Record
st.orm.Scrollable<T>
- Record Components:
key- the unique key field used for cursor positioning and ordering.keyCursor- the cursor value for the key field, ornullfor the first page.sort- the non-unique sort field, ornullfor single-key scrolling.sortCursor- the cursor value for the sort field, ornullfor single-key scrolling.size- the maximum number of results per window (must be positive).isForward-truefor forward scrolling (ascending key order),falsefor backward scrolling (descending key order).
public record Scrollable<T extends Data>(@Nonnull Metamodel.Key<T extends Data,?> key, @Nullable Object keyCursor, @Nullable Metamodel<T extends Data,?> sort, @Nullable Object sortCursor, int size, boolean isForward)
extends Record
Represents a scroll request that captures the cursor state needed to fetch a window of results.
A Scrollable is the scrolling counterpart of Pageable. While a Pageable navigates by
page number, a Scrollable navigates by cursor position. Scrollable instances are typically obtained from
Window.next() or Window.previous(), but can also be created directly using
the factory methods.
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, Object keyCursor, Metamodel<T, ?> sort, Object sortCursor, int size, boolean isForward) Creates an instance of aScrollablerecord class. -
Method Summary
Modifier and TypeMethodDescriptionbackward()Returns a new scrollable with backward direction.final booleanIndicates whether some other object is "equal to" this one.forward()Returns a new scrollable with forward direction.static <T extends Data,E>
Scrollable<T> fromCursor(Metamodel.Key<T, E> key, String cursor) Deserializes a cursor string (produced bytoCursor()) into aScrollablefor single-key scrolling.static <T extends Data,E, S>
Scrollable<T> fromCursor(Metamodel.Key<T, E> key, Metamodel<T, S> sort, String cursor) Deserializes a cursor string (produced bytoCursor()) into aScrollablefor composite scrolling with a sort field.booleanReturnstrueif this scrollable has a cursor position (i.e., is not a first-page request).final inthashCode()Returns a hash code value for this object.booleanReturnstrueif this scrollable uses a composite cursor with a separate sort field.booleanReturns the value of theisForwardrecord component.Metamodel.Key<T, ?> key()Returns the value of thekeyrecord component.Returns the value of thekeyCursorrecord component.static <T extends Data,E>
Scrollable<T> of(Metamodel.Key<T, E> key, int size) Creates a scrollable request for the first page in ascending key order.static <T extends Data,E>
Scrollable<T> of(Metamodel.Key<T, E> key, E keyCursor, int size) Creates a scrollable request starting after the given cursor value, in ascending key order.static <T extends Data,E, S>
Scrollable<T> of(Metamodel.Key<T, E> key, E keyCursor, Metamodel<T, S> sort, S sortCursor, int size) Creates a scrollable request starting after the given cursor values, in ascending key order, sorted by the given field.static <T extends Data,E, S>
Scrollable<T> of(Metamodel.Key<T, E> key, Metamodel<T, S> sort, int size) Creates a scrollable request for the first page in ascending key order, sorted by the given field.reverse()Returns a new scrollable with the direction reversed.intsize()Returns the value of thesizerecord component.sort()Returns the value of thesortrecord component.Returns the value of thesortCursorrecord component.toCursor()Serializes the cursor state of this scrollable into an opaque, URL-safe string.final StringtoString()Returns a string representation of this record class.
-
Constructor Details
-
Scrollable
public Scrollable(@Nonnull Metamodel.Key<T, ?> key, @Nullable Object keyCursor, @Nullable Metamodel<T, ?> sort, @Nullable Object sortCursor, int size, boolean isForward) Creates an instance of aScrollablerecord class.- Parameters:
key- the value for thekeyrecord componentkeyCursor- the value for thekeyCursorrecord componentsort- the value for thesortrecord componentsortCursor- the value for thesortCursorrecord componentsize- the value for thesizerecord componentisForward- the value for theisForwardrecord component
-
-
Method Details
-
of
Creates a scrollable request for the first page in ascending key order.- Type Parameters:
T- the entity type.E- the key field type.- Parameters:
key- the unique key field.size- the maximum number of results per window.- Returns:
- a scrollable for the first page.
-
of
public static <T extends Data,E> Scrollable<T> of(@Nonnull Metamodel.Key<T, E> key, @Nonnull E keyCursor, int size) Creates a scrollable request starting after the given cursor value, in ascending key order.- Type Parameters:
T- the entity type.E- the key field type.- Parameters:
key- the unique key field.keyCursor- the cursor value to start after.size- the maximum number of results per window.- Returns:
- a scrollable starting after the cursor.
-
of
public static <T extends Data,E, Scrollable<T> ofS> (@Nonnull Metamodel.Key<T, E> key, @Nonnull Metamodel<T, S> sort, int size) Creates a scrollable request for the first page in ascending key order, sorted by the given field.- Type Parameters:
T- the entity type.E- the key field type.S- the sort field type.- Parameters:
key- the unique key field (tiebreaker).sort- the non-unique sort field.size- the maximum number of results per window.- Returns:
- a scrollable for the first page.
-
of
public static <T extends Data,E, Scrollable<T> ofS> (@Nonnull Metamodel.Key<T, E> key, @Nonnull E keyCursor, @Nonnull Metamodel<T, S> sort, @Nonnull S sortCursor, int size) Creates a scrollable request starting after the given cursor values, in ascending key order, sorted by the given field.- Type Parameters:
T- the entity type.E- the key field type.S- the sort field type.- Parameters:
key- the unique key field (tiebreaker).keyCursor- the cursor value for the key field.sort- the non-unique sort field.sortCursor- the cursor value for the sort field.size- the maximum number of results per window.- Returns:
- a scrollable starting after the cursor values.
-
forward
Returns a new scrollable with forward direction. Returnsthisif already forward.- Returns:
- a scrollable with forward direction.
-
backward
Returns a new scrollable with backward direction. Returnsthisif already backward.// First page from the end (descending) var window = repo.scroll(Scrollable.of(User_.id, 20).backward());- Returns:
- a scrollable with backward direction.
-
reverse
Returns a new scrollable with the direction reversed.- Returns:
- a new scrollable with the opposite direction.
-
hasCursor
public boolean hasCursor()Returnstrueif this scrollable has a cursor position (i.e., is not a first-page request).- Returns:
trueif a cursor is set.
-
isComposite
public boolean isComposite()Returnstrueif this scrollable uses a composite cursor with a separate sort field.- Returns:
trueif a sort field is set.
-
toCursor
Serializes the cursor state of this scrollable into an opaque, URL-safe string. The cursor encodes the cursor values, size, direction, a metamodel fingerprint, and a registry fingerprint.This is useful for REST APIs where the cursor is passed as a query parameter:
// Server: include cursor in response String cursor = window.nextCursor(); // Client sends cursor back as query parameter // Server: reconstruct scrollable var scrollable = Scrollable.fromCursor(User_.id, cursor); var next = repo.scroll(scrollable);- Returns:
- a URL-safe Base64-encoded cursor string.
- Throws:
IllegalStateException- if a cursor value type is unsupported or serialization fails.- Since:
- 1.11
-
fromCursor
public static <T extends Data,E> Scrollable<T> fromCursor(@Nonnull Metamodel.Key<T, E> key, @Nonnull String cursor) Deserializes a cursor string (produced bytoCursor()) into aScrollablefor single-key scrolling.- Type Parameters:
T- the entity type.E- the key field type.- Parameters:
key- the unique key field (must match the key used when the cursor was created).cursor- the cursor string.- Returns:
- a scrollable reconstructed from the cursor.
- Throws:
IllegalArgumentException- if the cursor string is invalid.- Since:
- 1.11
-
fromCursor
public static <T extends Data,E, Scrollable<T> fromCursorS> (@Nonnull Metamodel.Key<T, E> key, @Nullable Metamodel<T, S> sort, @Nonnull String cursor) Deserializes a cursor string (produced bytoCursor()) into aScrollablefor composite scrolling with a sort field.- Type Parameters:
T- the entity type.E- the key field type.S- the sort field type.- Parameters:
key- the unique key field (must match the key used when the cursor was created).sort- the sort field, ornullfor single-key scrolling.cursor- the cursor string.- Returns:
- a scrollable reconstructed from the cursor.
- Throws:
IllegalArgumentException- if the cursor string is invalid.- 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
-
keyCursor
Returns the value of thekeyCursorrecord component.- Returns:
- the value of the
keyCursorrecord component
-
sort
Returns the value of thesortrecord component.- Returns:
- the value of the
sortrecord component
-
sortCursor
Returns the value of thesortCursorrecord component.- Returns:
- the value of the
sortCursorrecord component
-
size
public int size()Returns the value of thesizerecord component.- Returns:
- the value of the
sizerecord component
-
isForward
public boolean isForward()Returns the value of theisForwardrecord component.- Returns:
- the value of the
isForwardrecord component
-