1 5 package org.exoplatform.commons.utils; 6 7 import java.util.* ; 8 import org.exoplatform.commons.exception.ExoMessageException; 9 14 abstract public class PageList { 15 final static public PageList EMPTY_LIST = new ObjectPageList(new ArrayList(), 10) ; 16 17 private int pageSize_ ; 18 private int available_ = 0; 19 private int availablePage_ = 1; 20 protected int currentPage_ = -1 ; 21 protected List currentListPage_ ; 22 23 public PageList(int pageSize) { 24 pageSize_ = pageSize ; 25 } 26 27 public int getPageSize() { return pageSize_ ; } 28 public void setPageSize(int pageSize) { 29 pageSize_ = pageSize ; 30 setAvailablePage(available_) ; 31 } 32 33 public int getCurrentPage() { return currentPage_ ; } 34 public int getAvailable() { return available_ ; } 35 36 public int getAvailablePage() { return availablePage_ ; } 37 38 public List currentPage() throws Exception { 39 if(currentListPage_ == null) { 40 populateCurrentPage(currentPage_) ; 41 } 42 return currentListPage_ ; 43 } 44 45 abstract protected void populateCurrentPage(int page) throws Exception ; 46 47 public List getPage(int page) throws Exception { 48 checkAndSetPage(page) ; 49 populateCurrentPage(page) ; 50 return currentListPage_ ; 51 } 52 53 abstract public List getAll() throws Exception ; 54 55 protected void checkAndSetPage(int page) throws Exception { 56 if(page < 1 || page > availablePage_) { 57 Object [] args = { Integer.toString(page), Integer.toString(availablePage_) } ; 58 throw new ExoMessageException("PageList.page-out-of-range", args) ; 59 } 60 currentPage_ = page ; 61 } 62 63 protected void setAvailablePage(int available) { 64 available_ = available ; 65 if (available == 0) { 66 availablePage_ = 1 ; 67 currentPage_ = 1 ; 68 } else { 69 int pages = available / pageSize_ ; 70 if ( available % pageSize_ > 0) pages++ ; 71 availablePage_ = pages ; 72 currentPage_ = 1 ; 73 } 74 } 75 76 public int getFrom() { 77 return (currentPage_ - 1) * pageSize_ ; 78 } 79 80 public int getTo() { 81 int to = currentPage_ * pageSize_ ; 82 if (to > available_ ) to = available_ ; 83 return to ; 84 } 85 } | Popular Tags |