11package bangmaple .jdbc .paging ;
22
33import java .io .Serializable ;
4+ import java .util .Arrays ;
45
56public class PageRequest extends AbstractPageRequest implements Serializable {
67
78 private static final long serialVersionUID = -4541509938956089562L ;
89
9- private final Sort sort ;
10+ private boolean ordering = true ;
11+ private String [] properties = {};
1012
1113 /**
1214 * Creates a new {@link PageRequest} with sort parameters applied.
1315 *
1416 * @param page zero-based page index, must not be negative.
1517 * @param size the size of the page to be returned, must be greater than 0.
16- * @param sort must not be {@literal null}, use {@link Sort#unsorted()} instead.
1718 */
18- protected PageRequest (int page , int size , Sort sort ) {
19+ protected PageRequest (int page , int size , boolean ordering , String ... properties ) {
1920
2021 super (page , size );
2122
22- this .sort = sort ;
23+ this .ordering = ordering ;
24+ this .properties = properties ;
2325 }
2426
2527 /**
@@ -29,30 +31,30 @@ protected PageRequest(int page, int size, Sort sort) {
2931 * @param size the size of the page to be returned, must be greater than 0.
3032 */
3133 public static PageRequest of (int page , int size ) {
32- return of (page , size , Sort . unsorted () );
34+ return new PageRequest (page , size , Pageable . SORT_ASC , "" );
3335 }
3436
3537 /**
3638 * Creates a new {@link PageRequest} with sort parameters applied.
3739 *
38- * @param page zero-based page index.
39- * @param size the size of the page to be returned.
40- * @param sort must not be {@literal null}, use {@link Sort#unsorted()} instead .
41- */
42- public static PageRequest of (int page , int size , Sort sort ) {
43- return new PageRequest (page , size , sort );
40+ * @param page zero-based page index.
41+ * @param size the size of the page to be returned.
42+ * @param ordering Pageable.ASC (true) for Ascending order, Pageable.DESC (false) for Descending order .
43+ */
44+ public static PageRequest of (int page , int size , boolean ordering ) {
45+ return new PageRequest (page , size , ordering , "" );
4446 }
4547
4648 /**
4749 * Creates a new {@link PageRequest} with sort direction and properties applied.
4850 *
49- * @param page zero-based page index, must not be negative.
50- * @param size the size of the page to be returned, must be greater than 0.
51- * @param direction must not be {@literal null} .
51+ * @param page zero-based page index, must not be negative.
52+ * @param size the size of the page to be returned, must be greater than 0.
53+ * @param ordering Pageable.ASC (true) for Ascending order, Pageable.DESC (false) for Descending order .
5254 * @param properties must not be {@literal null}.
5355 */
54- public static PageRequest of (int page , int size , Sort . Direction direction , String ... properties ) {
55- return of (page , size , Sort . by ( direction , properties ) );
56+ public static PageRequest of (int page , int size , boolean ordering , String ... properties ) {
57+ return new PageRequest (page , size , ordering , properties );
5658 }
5759
5860 /**
@@ -65,85 +67,31 @@ public static PageRequest ofSize(int pageSize) {
6567 return PageRequest .of (0 , pageSize );
6668 }
6769
68- public Sort getSort () {
69- return sort ;
70- }
71-
72- @ Override
73- public Pageable next () {
74- return (Pageable ) new PageRequest (getPageNumber () + 1 , getPageSize (), getSort ());
75- }
76-
77- /*
78- * (non-Javadoc)
79- */
8070 @ Override
81- public Pageable previous () {
82- return ( Pageable ) ( getPageNumber () == 0 ? this : new PageRequest ( getPageNumber () - 1 , getPageSize (), getSort ())) ;
71+ public boolean isAscending () {
72+ return ordering == Pageable . SORT_ASC ;
8373 }
8474
8575 @ Override
86- public Pageable first () {
87- return ( Pageable ) new PageRequest ( 0 , getPageSize (), getSort ()) ;
76+ public boolean isDescending () {
77+ return ordering == Pageable . SORT_DESC ;
8878 }
8979
90- /*
91- * (non-Javadoc)
92- * @see java.lang.Object#equals(java.lang.Object)
93- */
94- @ Override
95- public boolean equals ( Object obj ) {
96-
97- if (this == obj ) {
98- return true ;
99- }
100-
101- if (!(obj instanceof PageRequest )) {
102- return false ;
80+ public String getProperties () {
81+ StringBuilder result = new StringBuilder ();
82+ for (int i = 0 ; i < properties .length ; i ++) {
83+ if (i == properties .length - 1 ) {
84+ result .append (properties [i ]);
85+ break ;
86+ }
87+ result .append (properties [i ]).append (", " );
10388 }
104-
105- PageRequest that = (PageRequest ) obj ;
106-
107- return super .equals (that ) && this .sort .equals (that .sort );
108- }
109-
110- /**
111- * Creates a new {@link PageRequest} with {@link Sort.Direction} and {@code properties} applied.
112- *
113- * @param direction must not be {@literal null}.
114- * @param properties must not be {@literal null}.
115- * @return a new {@link PageRequest}.
116- */
117- public PageRequest withSort (Sort .Direction direction , String ... properties ) {
118- return new PageRequest (getPageNumber (), getPageSize (), Sort .by (direction , properties ));
89+ return result .toString ();
11990 }
12091
121- /**
122- * Creates a new {@link PageRequest} with {@link Sort} applied.
123- *
124- * @param sort must not be {@literal null}.
125- * @return a new {@link PageRequest}.
126- */
127- public PageRequest withSort (Sort sort ) {
128- return new PageRequest (getPageNumber (), getPageSize (), sort );
129- }
130-
131- /*
132- * (non-Javadoc)
133- * @see java.lang.Object#hashCode()
134- */
135- @ Override
136- public int hashCode () {
137- return 31 * super .hashCode () + sort .hashCode ();
138- }
139-
140- /*
141- * (non-Javadoc)
142- * @see java.lang.Object#toString()
143- */
14492 @ Override
14593 public String toString () {
146- return String .format ("Page request [number: %d, size %d, sort: %s ]" , getPageNumber (), getPageSize (), sort );
94+ return String .format ("Page request [number: %d, size %d]" , getPageNumber (), getPageSize ());
14795 }
14896
14997}
0 commit comments