ページングインタフェースと実装

3221 ワード

機能:
ページングインタフェース
ソース:
package com.dotions.utils;

/**
 * 

* @author Dotions 2017-05-02 */ public interface Pageable { /** * * */ int getPageNumber(); /** * * */ int getPageSize(); /** * * */ int getTotalPages(); /** * * */ int getTotalElements(); /** * * */ boolean hasNext(); /** * * */ Pageable next(); }

機能:
ページング・オブジェクト
ソース:
package com.dotions.utils;

import java.util.Collections;
import java.util.List;

/**
 * 

* *

* * @author dotions 2017-05-02 */ public class Page implements Pageable { private int pageNo = 1; private int pageSize = 50; private int totalElements = 0; private List elements = Collections.emptyList(); public Page() { super(); } public Page(int totalElements) { super(); this.totalElements = totalElements; } @Override public int getPageNumber() { return pageNo; } @Override public int getPageSize() { return pageSize; } @Override public int getTotalPages() { return totalElements / pageSize + 1; } @Override public int getTotalElements() { return totalElements; } public int getStart() { return (getPageNumber() - 1) * getPageSize(); } public int getEnd() { return getPageNumber() * getPageSize(); } public void setPageNo(int pageNo) { if (pageNo > 0 && pageNo <= getTotalPages()) { this.pageNo = pageNo; } // , if (pageNo > 0 && pageNo > getTotalPages()) { this.pageNo = getTotalPages(); } } public void setPageSize(int pageSize) { if (pageSize > 0) { this.pageSize = pageSize; } } public void setTotalElements(int totalElements) { if (totalElements >= 0) { this.totalElements = totalElements; } } public void setElements(List elements) { this.elements = elements; } public List getElements() { return elements; } @Override public boolean hasNext() { return getTotalElements() - getPageNumber() * getPageSize() > 0; } @Override public Page next() { this.setPageNo(pageNo + 1); this.elements = Collections.emptyList(); return this; } @Override public String toString() { StringBuilder builder = new StringBuilder(); builder.append("Page [pageNo="); builder.append(pageNo); builder.append(", pageSize="); builder.append(pageSize); builder.append(", totalElements="); builder.append(totalElements); builder.append(", elements="); builder.append(elements); builder.append(", hasNext()="); builder.append(hasNext()); builder.append("]"); return builder.toString(); } }