import java.util.ArrayList; import java.util.List; public class Pager { public int currentPage = 0; private int nextPage; private int previousPage; private int fristPage; public int lastPage; private int pageSize = 10; private int record_cnt = 67; public List<String> currentRecords; public List<String> allRecords; public Pager(List<String> _allRecords, int _pageSize) { this.pageSize = _pageSize; allRecords = _allRecords; record_cnt = allRecords.size(); lastPage = this.record_cnt / this.pageSize + 1; } public int getCurrentPage() { return currentPage; } private int getNextPageNum() { if (currentPage < lastPage) { nextPage = currentPage + 1; } else { nextPage = lastPage; } return nextPage; } public List<String> getJumpPageRecords(int jumpPage) { if (jumpPage >= this.lastPage) { return this.getLastPageRecords(); } else if (jumpPage <= this.fristPage) { return this.getFirstPageRecords(); } else { this.currentRecords = new ArrayList<String>(); for (int i = (jumpPage - 1) * pageSize; i < jumpPage * pageSize; i++) { this.currentRecords.add(this.allRecords.get(i)); } this.currentPage = jumpPage; return currentRecords; } } public List<String> getNextPageRecords() { getNextPageNum(); if (this.currentPage == lastPage) { System.out.println("Already was the last page!!!"); return null; } if (nextPage == this.lastPage) { return this.getLastPageRecords(); } else { this.currentRecords = new ArrayList<String>(); for (int i = currentPage * pageSize; i < nextPage * pageSize; i++) { this.currentRecords.add(this.allRecords.get(i)); } currentPage = nextPage; return currentRecords; } } public List<String> getPreviousPageRecords() { this.getPreviousPage(); if (this.currentPage == 1) { System.out.println("Already was the first page!!!"); return null; } if (previousPage == 1) { return this.getFirstPageRecords(); } else { this.currentRecords = new ArrayList<String>(); for (int i = (previousPage - 1) * pageSize; i < previousPage * pageSize; i++) { this.currentRecords.add(this.allRecords.get(i)); } currentPage = previousPage; return currentRecords; } } public List<String> getFirstPageRecords() { if (lastPage == 1) { return this.allRecords; } else { this.currentRecords = new ArrayList<String>(); for (int i = 0; i < this.pageSize; i++) { this.currentRecords.add(allRecords.get(i)); } this.currentPage = 1; return currentRecords; } } public List<String> getLastPageRecords() { if (lastPage == 1) { return this.allRecords; } else { this.currentRecords = new ArrayList<String>(); for (int i = (lastPage - 1) * pageSize; i < this.record_cnt; i++) { this.currentRecords.add(allRecords.get(i)); } this.currentPage = lastPage; return currentRecords; } } private int getPreviousPage() { if (this.currentPage > 1) { previousPage = currentPage - 1; } else { previousPage = 1; } return previousPage; } public int getFristPage() { fristPage = 0; return fristPage; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } // public List<String> public static void main(String[] args) { List<String> pageRecords = new ArrayList<String>(); for (int i = 1; i <= 59; i++) { pageRecords.add("-+-" + i + "---"); } Pager p = new Pager(pageRecords, 5); System.out.println("all: " + p.lastPage); List<String> r = p.getJumpPageRecords(2); if (r != null) { for (String a : r) { System.out.print(a + " | "); } System.out.println(); System.out.println("--------current page is :" + p.currentPage + " --------"); } List<String> r2 = p.getNextPageRecords(); if (r2 != null) { for (String a : r2) { System.out.print(a + " | "); } System.out.println(); System.out.println("--------current page is :" + p.currentPage + " --------"); } List<String> r3 = p.getNextPageRecords(); if (r3 != null) { for (String a : r3) { System.out.print(a + " | "); } System.out.println(); System.out.println("--------current page is :" + p.currentPage + " --------"); } List<String> r4 = p.getPreviousPageRecords(); if (r4 != null) { for (String a : r4) { System.out.print(a + " | "); } System.out.println(); System.out.println("--------current page is :" + p.currentPage + " --------"); } List<String> r5 = p.getFirstPageRecords(); if (r5 != null) { for (String a : r5) { System.out.print(a + " | "); } System.out.println(); System.out.println("--------current page is :" + p.currentPage + " --------"); } List<String> r6 = p.getLastPageRecords(); if (r6 != null) { for (String a : r6) { System.out.print(a + " | "); } System.out.println(); System.out.println("--------current page is :" + p.currentPage + " --------"); } List<String> r7 = p.getNextPageRecords(); if (r7 != null) { for (String a : r7) { System.out.print(a + " | "); } System.out.println(); System.out.println("--------current page is :" + p.currentPage + " --------"); } List<String> r8 = p.getFirstPageRecords(); if (r8 != null) { for (String a : r8) { System.out.print(a + " | "); } System.out.println(); System.out.println("--------current page is :" + p.currentPage + " --------"); } List<String> r9 = p.getPreviousPageRecords(); if (r9 != null) { for (String a : r9) { System.out.print(a + " | "); } System.out.println(); System.out.println("--------current page is :" + p.currentPage + " --------"); } for (int i = 0; i < 17; i++) { List<String> r12 = p.getNextPageRecords(); if (r12 != null) { for (String a : r12) { System.out.print(a + " | "); } System.out.println(); System.out.println("--------current page is :" + p.currentPage + " --------"); } } for (int i = 0; i < 12; i++) { List<String> r12 = p.getPreviousPageRecords(); if (r12 != null) { for (String a : r12) { System.out.print(a + " | "); } System.out.println(); System.out.println("--------current page is :" + p.currentPage + " --------"); } } List<String> r11 = p.getJumpPageRecords(9); if (r11 != null) { for (String a : r11) { System.out.print(a + " | "); } System.out.println(); System.out.println("--------current page is :" + p.currentPage + " --------"); } for (int i = 0; i < 19; i++) { List<String> r12 = p.getPreviousPageRecords(); if (r12 != null) { for (String a : r12) { System.out.print(a + " | "); } System.out.println(); System.out.println("--------current page is :" + p.currentPage + " --------"); } } } }