1616package org .springframework .data .cassandra .core .query ;
1717
1818import java .nio .ByteBuffer ;
19+ import java .util .Objects ;
1920
2021import org .springframework .data .domain .PageRequest ;
2122import org .springframework .data .domain .Pageable ;
3940 */
4041public class CassandraPageRequest extends PageRequest {
4142
42- private final @ Nullable ByteBuffer pagingState ;
43+ private final CassandraScrollPosition scrollPosition ;
4344
4445 private final boolean nextAllowed ;
4546
46- private CassandraPageRequest (int page , int size , Sort sort , @ Nullable ByteBuffer pagingState , boolean nextAllowed ) {
47+ private CassandraPageRequest (int page , int size , Sort sort , CassandraScrollPosition scrollPosition ,
48+ boolean nextAllowed ) {
4749
4850 super (page , size , sort );
4951
50- this .pagingState = pagingState ;
52+ Assert .notNull (scrollPosition , "ScrollPosition must not be null" );
53+
54+ this .scrollPosition = scrollPosition ;
5155 this .nextAllowed = nextAllowed ;
5256 }
5357
@@ -79,7 +83,7 @@ public static CassandraPageRequest of(int page, int size, Sort sort) {
7983 Assert .isTrue (page == 0 ,
8084 "Cannot create a Cassandra page request for an indexed page other than the first page (0)" );
8185
82- return new CassandraPageRequest (page , size , sort , null , false );
86+ return new CassandraPageRequest (page , size , sort , CassandraScrollPosition . initial () , false );
8387 }
8488
8589 /**
@@ -100,14 +104,15 @@ public static CassandraPageRequest of(int page, int size, Direction direction, S
100104 }
101105
102106 /**
103- * Creates a a {@link PageRequest} with sort direction and properties applied.
107+ * Creates a {@link PageRequest} with sort direction and properties applied.
104108 *
105109 * @param current the current {@link Pageable}, must not be {@literal null}.
106110 * @param pagingState the paging state associated with the current {@link Pageable}. Can be {@literal null} if there
107111 * is no paging state associated.
108112 */
109113 public static CassandraPageRequest of (Pageable current , @ Nullable ByteBuffer pagingState ) {
110- return new CassandraPageRequest (current .getPageNumber (), current .getPageSize (), current .getSort (), pagingState ,
114+ return new CassandraPageRequest (current .getPageNumber (), current .getPageSize (), current .getSort (),
115+ pagingState != null ? CassandraScrollPosition .of (pagingState ) : CassandraScrollPosition .initial (),
111116 pagingState != null );
112117 }
113118
@@ -127,7 +132,7 @@ public static CassandraPageRequest first(int size) {
127132 * @param sort must not be {@literal null}.
128133 */
129134 public static CassandraPageRequest first (int size , Sort sort ) {
130- return new CassandraPageRequest (0 , size , sort , null , false );
135+ return new CassandraPageRequest (0 , size , sort , CassandraScrollPosition . initial () , false );
131136 }
132137
133138 /**
@@ -178,11 +183,11 @@ public static void validatePageable(Pageable pageable) {
178183 @ Nullable
179184 public ByteBuffer getPagingState () {
180185
181- if (this .pagingState == null ) {
186+ if (this .scrollPosition . isInitial () ) {
182187 return null ;
183188 }
184189
185- return this .pagingState . asReadOnlyBuffer ();
190+ return this .scrollPosition . getPagingState ();
186191 }
187192
188193 /**
@@ -200,7 +205,7 @@ public CassandraPageRequest next() {
200205
201206 Assert .state (hasNext (), "Cannot create a next page request without a PagingState" );
202207
203- return new CassandraPageRequest (getPageNumber () + 1 , getPageSize (), getSort (), getPagingState () , false );
208+ return new CassandraPageRequest (getPageNumber () + 1 , getPageSize (), getSort (), this . scrollPosition , false );
204209 }
205210
206211 /**
@@ -214,7 +219,8 @@ public CassandraPageRequest withSort(Sort sort) {
214219
215220 Assert .notNull (sort , "Sort must not be null" );
216221
217- return new CassandraPageRequest (this .getPageNumber (), this .getPageSize (), sort , getPagingState (), this .nextAllowed );
222+ return new CassandraPageRequest (this .getPageNumber (), this .getPageSize (), sort , this .scrollPosition ,
223+ this .nextAllowed );
218224 }
219225
220226 @ Override
@@ -225,6 +231,16 @@ public PageRequest previous() {
225231 return super .previous ();
226232 }
227233
234+ /**
235+ * Returns the underlying {@link CassandraScrollPosition}.
236+ *
237+ * @return the underlying {@link CassandraScrollPosition}.
238+ * @since 4.2
239+ */
240+ public CassandraScrollPosition getScrollPosition () {
241+ return this .scrollPosition ;
242+ }
243+
228244 @ Override
229245 public boolean equals (@ Nullable Object obj ) {
230246
@@ -244,15 +260,15 @@ public boolean equals(@Nullable Object obj) {
244260 return false ;
245261 }
246262
247- return ( pagingState != null ? pagingState .equals (that . pagingState ) : that .pagingState == null );
263+ return Objects .equals (this . scrollPosition , that .scrollPosition );
248264 }
249265
250266 @ Override
251267 public int hashCode () {
252268
253269 int result = super .hashCode ();
254270
255- result = 31 * result + (pagingState != null ? pagingState .hashCode () : 0 );
271+ result = 31 * result + (scrollPosition != null ? scrollPosition .hashCode () : 0 );
256272 result = 31 * result + (nextAllowed ? 1 : 0 );
257273
258274 return result ;
0 commit comments