JAVA List集合轉Page(分頁對象)

/**
 * @version 1.0
 * @author: fwjia
 */
import java.util.List;

public class PageModel<T> {
    /***
     * 當前頁
     */
    private int page = 1;

    /***
     * 總頁數
     */
    public int totalPages = 0;

    /***
     * 每頁數據條數
     */
    private int pageRecorders;

    /***
     * 總頁數
     */
    private int totalRows = 0;

    /***
     * 每頁的起始數
     */
    private int pageStartRow = 0;

    /***
     * 每頁顯示數據的終止數
     */
    private int pageEndRow = 0;

    /***
     * 是否有下一頁
     */
    private boolean hasNextPage = false;

    /***
     * 是否有前一頁
     */
    private boolean hasPreviousPage = false;

    /***
     * 數據集合
     */
    private List<T> list;


    public PageModel(List<T> list, int pageRecorders) {
        // 經過對象集,記錄總數劃分
        init(list, pageRecorders);
    }

    /** *//**
     * 初始化list,並告之該list每頁的記錄數
     * @param list 數據幾個
     * @param pageRecorders 一頁顯示多少數據
     */
    public void init(List<T> list, int pageRecorders) {
        this.pageRecorders = pageRecorders;
        this.list = list;
        totalRows = list.size();
        hasPreviousPage = false;
        if ((totalRows % pageRecorders) == 0) {
            totalPages = totalRows / pageRecorders;
        } else {
            totalPages = totalRows / pageRecorders + 1;
        }

        if (page >= totalPages) {
            hasNextPage = false;
        } else {
            hasNextPage = true;
        }

        if (totalRows < pageRecorders) {
            this.pageStartRow = 0;
            this.pageEndRow = totalRows;
        } else {
            this.pageStartRow = 0;
            this.pageEndRow = pageRecorders;
        }
    }


    // 判斷要不要分頁
    public boolean isNext() {
        return list.size() > 5;
    }

    public void setHasPreviousPage(boolean hasPreviousPage) {
        this.hasPreviousPage = hasPreviousPage;
    }

    public String toString(int temp) {
        String str = Integer.toString(temp);
        return str;
    }

    public void description() {

        String description = "共有數據數:" + this.getTotalRows() +

                "共有頁數: " + this.getTotalPages() +

                "當前頁數爲:" + this.getPage() +

                " 是否有前一頁: " + this.isHasPreviousPage() +

                " 是否有下一頁:" + this.isHasNextPage() +

                " 開始行數:" + this.getPageStartRow() +

                " 終止行數:" + this.getPageEndRow();

        System.out.println(description);
    }

    public List getNextPage() {
        page = page + 1;

        disposePage();

        System.out.println("用戶凋用的是第" + page + "頁");
        this.description();
        return getObjects(page);
    }

    /**
     * 處理分頁
     */
    private void disposePage() {

        if (page == 0) {
            page = 1;
        }

        if ((page - 1) > 0) {
            hasPreviousPage = true;
        } else {
            hasPreviousPage = false;
        }

        if (page >= totalPages) {
            hasNextPage = false;
        } else {
            hasNextPage = true;
        }
    }

    public List getPreviousPage() {

        page = page - 1;

        if ((page - 1) > 0) {
            hasPreviousPage = true;
        } else {
            hasPreviousPage = false;
        }
        if (page >= totalPages) {
            hasNextPage = false;
        } else {
            hasNextPage = true;
        }
        this.description();
        return getObjects(page);
    }

    /**
     * 獲取第幾頁的內容
     *
     * @param page 當前頁面
     * @return
     */
    public List<T> getObjects(int page) {
        if(page == 0){
            this.setPage(1);
        }
        else{
            this.setPage(page);
        }
        this.disposePage();
        if (page * pageRecorders < totalRows) {
            // 判斷是否爲最後一頁
            pageEndRow = page * pageRecorders;
            pageStartRow = pageEndRow - pageRecorders;
        } else {
            pageEndRow = totalRows;
            pageStartRow = pageRecorders * (totalPages - 1);
        }

        List<T> objects = null;
        if (!list.isEmpty()) {
            objects = list.subList(pageStartRow, pageEndRow);
        }
        //this.description();
        return objects;
    }

    public List<T> getFistPage() {
        if (this.isNext()) {
            return list.subList(0, pageRecorders);
        } else {
            return list;
        }
    }

    public boolean isHasNextPage() {
        return hasNextPage;
    }


    public void setHasNextPage(boolean hasNextPage) {
        this.hasNextPage = hasNextPage;
    }


    public List getList() {
        return list;
    }


    public void setList(List list) {
        this.list = list;
    }


    public int getPage() {
        return page;
    }


    public void setPage(int page) {
        this.page = page;
    }


    public int getPageEndRow() {
        return pageEndRow;
    }


    public void setPageEndRow(int pageEndRow) {
        this.pageEndRow = pageEndRow;
    }


    public int getPageRecorders() {
        return pageRecorders;
    }


    public void setPageRecorders(int pageRecorders) {
        this.pageRecorders = pageRecorders;
    }


    public int getPageStartRow() {
        return pageStartRow;
    }


    public void setPageStartRow(int pageStartRow) {
        this.pageStartRow = pageStartRow;
    }


    public int getTotalPages() {
        return totalPages;
    }


    public void setTotalPages(int totalPages) {
        this.totalPages = totalPages;
    }


    public int getTotalRows() {
        return totalRows;
    }


    public void setTotalRows(int totalRows) {
        this.totalRows = totalRows;
    }


    public boolean isHasPreviousPage() {
        return hasPreviousPage;
    }

}

  測試類TestPagerModel java

import java.util.ArrayList;
import java.util.List;

/**
 * @version 1.0
 * @author: fwjia
 */
public class TestPagerModel {

    public static void main(String args[]) {
        List<String> list = new ArrayList<String>();
        list.add("a");
        list.add("b");
        list.add("c");
        list.add("d");
        list.add("e");
        list.add("f");
        list.add("g");
        list.add("h");
        list.add("h");
        list.add("i");
        list.add("j");
        list.add("k");
        list.add("l");
        list.add("m");
        PageModel<String> pm = new PageModel(list, 5);

        List<String> sublist = pm.getObjects(2);
        for(int i = 0; i < sublist.size(); i++) {
            System.out.println(sublist.get(i));
        }
    }

}
相關文章
相關標籤/搜索