Skip to content

Commit

Permalink
feat($Pagination): create functional pagination response composer
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnymillergh committed Feb 1, 2022
1 parent fd29a5e commit 6d4dbbb
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.jmsoftware.maf.common.bean;

import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import org.hibernate.validator.constraints.Range;
Expand Down Expand Up @@ -54,10 +54,10 @@ public class PaginationBase {

@JsonIgnore
public String getOrderByStatement() {
if (!StrUtil.isBlank(orderBy)) {
return String.format("%s `%s` %s", "ORDER BY", orderBy, orderRule);
if (!CharSequenceUtil.isBlank(this.orderBy)) {
return String.format("%s `%s` %s", "ORDER BY", this.orderBy, this.orderRule);
}
return orderByStatement;
return this.orderByStatement;
}

/**
Expand All @@ -68,8 +68,8 @@ public String getOrderByStatement() {
* @author Johnny Miller (锺俊), email: johnnysviva@outlook.com, date: 6/27/2021 4:37 PM
*/
public boolean hasNextPage(@NotNull PageResponseBodyBean<?> pageResponseBodyBean) {
if (NumberUtil.compare(pageResponseBodyBean.getTotal(), (long) currentPage * pageSize) > 0) {
currentPage += 1;
if (NumberUtil.compare(pageResponseBodyBean.getTotal(), (long) this.currentPage * this.pageSize) > 0) {
this.currentPage += 1;
return true;
}
return false;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.jmsoftware.maf.springcloudstarter.function;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.PageUtil;
import com.jmsoftware.maf.common.bean.PageResponseBodyBean;
import com.jmsoftware.maf.common.bean.PaginationBase;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import lombok.val;

import java.util.List;
import java.util.function.Function;

import static java.lang.Math.toIntExact;

/**
* <h1>FunctionalPaginationResponseComposer</h1>
* <p>
* Change description here.
*
* @author Johnny Miller (鍾俊), email: johnnysviva@outlook.com, 2/1/22 1:13 AM
**/
@Slf4j
@SuppressWarnings("unused")
public class FunctionalPaginationResponseComposer {
private FunctionalPaginationResponseComposer() {
}

public static <T extends PaginationBase, R> List<R> composePages(
@NonNull T paginationBase,
@NonNull Function<T, PageResponseBodyBean<R>> callback
) {
val result = CollUtil.<R>newArrayList();
while (true) {
val page = callback.apply(paginationBase);
if (CollUtil.isEmpty(page.getList())) {
CollUtil.addAll(result, page.getList());
}
if (!paginationBase.hasNextPage(page)) {
val totalPage = PageUtil.totalPage(toIntExact(page.getTotal()), paginationBase.getPageSize());
log.warn("Reached the end of the page. totalPage: {}, currentPage: {}, pageSize: {}", totalPage,
paginationBase.getCurrentPage(), paginationBase.getPageSize());
break;
}
}
return result;
}

private static <T extends PaginationBase, R> boolean hasNextPage(
@NonNull T paginationBase,
@NonNull PageResponseBodyBean<R> pageResponse
) {
val totalPage = PageUtil.totalPage(toIntExact(pageResponse.getTotal()), paginationBase.getPageSize());
if (paginationBase.getCurrentPage() < totalPage) {
paginationBase.setCurrentPage(paginationBase.getCurrentPage() + 1);
return true;
}
log.warn("Reached the end of the page. totalPage: {}, currentPage: {}, pageSize: {}", totalPage,
paginationBase.getCurrentPage(), paginationBase.getPageSize());
return false;
}
}

0 comments on commit 6d4dbbb

Please sign in to comment.