Skip to content

Instantly share code, notes, and snippets.

@daemin-hwang
Created January 28, 2016 05:54
Show Gist options
  • Save daemin-hwang/abfc1098bda329e34360 to your computer and use it in GitHub Desktop.
Save daemin-hwang/abfc1098bda329e34360 to your computer and use it in GitHub Desktop.
JAVA 페이징 처리 클래스
//* @Getter, @Setter 사용 - lombock 디펜던시 있음
@ToString
public class Paging {
private static final int DEFAULT_COUNT_PER_PAGE = 10; //한화면에 보여질 item row 수
private static final int DEFAULT_PAGE_PER_BLOCK = 10; //페이지블럭에 보일 페이지 수 [1] [2] [3] == '3'
@Getter @Setter
private int countPerPage = DEFAULT_COUNT_PER_PAGE;
@Getter @Setter
private int pagePerBlock = DEFAULT_PAGE_PER_BLOCK;
@Getter
private int totalCount;
@Getter
private int currentPage;
@Getter
private int prevBlockPage;
@Getter
private int beginPage;
@Getter
private int endPage;
@Getter
private int nextBlockPage;
@Getter
private int totalPage;
public void setTotalCount(int totalCount) {
init(totalCount, this.currentPage);
calculatePaging();
}
public void setCurrentPage(int currentPage) {
init(this.totalCount, currentPage);
calculatePaging();
}
public boolean hasPrevBlockPage() {
return this.prevBlockPage > 0;
}
public boolean hasNextBlockPage() {
return this.nextBlockPage > 0;
}
//생성자
public Paging(int totalCount, int currentPage) {
init(totalCount, currentPage);
calculatePaging();
}
private void init(int totalCount, int currentPage) {
this.totalCount = totalCount;
this.currentPage = currentPage;
}
private void calculatePaging() {
if (this.totalCount % this.countPerPage == 0) {
this.totalPage = this.totalCount / this.countPerPage;
} else {
this.totalPage = this.totalCount / this.countPerPage + 1;
}
if (currentPage > totalPage) {
return;
}
int totalBlock = 1;
if (this.totalPage % this.pagePerBlock == 0) {
totalBlock = this.totalPage / this.pagePerBlock;
} else {
totalBlock = this.totalPage / this.pagePerBlock + 1;
}
int block = 1;
if (this.currentPage % this.pagePerBlock == 0) {
block = this.currentPage / this.pagePerBlock;
} else {
block = this.currentPage / this.pagePerBlock + 1;
}
this.beginPage = (block - 1) * this.pagePerBlock + 1;
this.endPage = block * this.pagePerBlock;
if (block >= totalBlock) {
this.endPage = this.totalPage;
}
if (block > 1) {
this.prevBlockPage = this.beginPage - 1;
}
if (block < totalBlock) {
this.nextBlockPage = this.endPage + 1;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment