3131 * @author Oliver Gierke
3232 * @author Christoph Strobl
3333 * @author Jens Schauder
34+ * @author Jay Lee
3435 * @since 2.4
3536 */
3637public abstract class PageableExecutionUtils {
@@ -54,19 +55,30 @@ public static <T> Page<T> getPage(List<T> content, Pageable pageable, LongSuppli
5455 Assert .notNull (pageable , "Pageable must not be null" );
5556 Assert .notNull (totalSupplier , "TotalSupplier must not be null" );
5657
57- if (pageable .isUnpaged () || pageable .getOffset () == 0 ) {
58-
59- if (pageable .isUnpaged () || pageable .getPageSize () > content .size ()) {
60- return new PageImpl <>(content , pageable , content .size ());
61- }
58+ if (pageable .isUnpaged ()) {
59+ return new PageImpl <>(content , pageable , content .size ());
60+ }
6261
63- return new PageImpl <>(content , pageable , totalSupplier .getAsLong ());
62+ if (isFirstPage (pageable ) && isPartialPage (content , pageable )) {
63+ return new PageImpl <>(content , pageable , content .size ());
6464 }
6565
66- if (content . size () != 0 && pageable . getPageSize () > content . size ( )) {
66+ if (isSubsequentPage ( pageable ) && ! content . isEmpty () && isPartialPage ( content , pageable )) {
6767 return new PageImpl <>(content , pageable , pageable .getOffset () + content .size ());
6868 }
6969
7070 return new PageImpl <>(content , pageable , totalSupplier .getAsLong ());
7171 }
72+
73+ private static <T > boolean isPartialPage (List <T > content , Pageable pageable ) {
74+ return pageable .getPageSize () > content .size ();
75+ }
76+
77+ private static boolean isFirstPage (Pageable pageable ) {
78+ return pageable .getOffset () == 0 ;
79+ }
80+
81+ private static boolean isSubsequentPage (Pageable pageable ) {
82+ return !isFirstPage (pageable );
83+ }
7284}
0 commit comments