Skip to content

Commit 47109d4

Browse files
erie0210schauder
authored andcommitted
Refactor PageableExecutionUtils.
Closes #3103 Original pull request #3113
1 parent ec3ba10 commit 47109d4

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

src/main/java/org/springframework/data/support/PageableExecutionUtils.java

+19-7
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
* @author Oliver Gierke
3232
* @author Christoph Strobl
3333
* @author Jens Schauder
34+
* @author Jay Lee
3435
* @since 2.4
3536
*/
3637
public 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

Comments
 (0)