KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > javabb > infra > PagedList


1 /*
2  * Copyright 2004 JavaFree.org
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.javabb.infra;
18
19 import java.util.AbstractList JavaDoc;
20 import java.util.List JavaDoc;
21
22 import org.apache.commons.lang.builder.ToStringBuilder;
23
24 /**
25  * @author Ronald Tetsuo Miura
26  * @since 16/02/2005
27  */

28 public class PagedList extends AbstractList JavaDoc {
29     private final List JavaDoc _pageItems;
30     private final int _pageNumber;
31     private final int _itemsPerPage;
32     private final int _totalItemCount;
33
34     /**
35      * @param pageItems
36      * @param pageNumber
37      * @param itemsPerPage
38      * @param totalItemCount
39      */

40     public PagedList(List JavaDoc pageItems, int pageNumber, int itemsPerPage, int totalItemCount) {
41         this._pageItems = pageItems;
42         this._pageNumber = pageNumber;
43         this._itemsPerPage = itemsPerPage;
44         this._totalItemCount = totalItemCount;
45     }
46
47     /**
48      * @see java.util.AbstractList#get(int)
49      */

50     public Object JavaDoc get(int index) {
51         return this._pageItems.get(index);
52     }
53
54     /**
55      * @see java.util.AbstractCollection#size()
56      */

57     public int size() {
58         return this._pageItems.size();
59     }
60
61     /**
62      * @return the list of items for this page
63      */

64     public List JavaDoc getPageItems() {
65         return _pageItems;
66     }
67
68     /**
69      * @return number of the current page
70      */

71     public int getPageNumber() {
72         return _pageNumber;
73     }
74
75     /**
76      * @return the number of items per page
77      */

78     private int getItemsPerPage() {
79         return _itemsPerPage;
80     }
81
82     /**
83      * @return total count of items
84      */

85     public int getTotalItemCount() {
86         return _totalItemCount;
87     }
88
89     /**
90      * @return total count of pages
91      */

92     public int getTotalPageCount() {
93         return (int) Math.ceil((double) getTotalItemCount() / getItemsPerPage());
94     }
95
96     /**
97      * @return true if this is the first page
98      */

99     public boolean isFirstPage() {
100         return isFirstPage(getPageNumber());
101     }
102
103     /**
104      * @return true if this is the last page
105      */

106     public boolean isLastPage() {
107         return isLastPage(getPageNumber());
108     }
109
110     /**
111      * @param page
112      * @return true if the page is the first page
113      */

114     public boolean isFirstPage(int page) {
115         return page <= 1;
116     }
117
118     /**
119      * @param page
120      * @return true if the page is the last page
121      */

122     public boolean isLastPage(int page) {
123         return page >= getTotalPageCount();
124     }
125
126     /**
127      * @see java.util.AbstractCollection#toString()
128      */

129     public String JavaDoc toString() {
130         return new ToStringBuilder(this).append(getPageItems())
131             .append(getPageNumber())
132             .append(getItemsPerPage())
133             .append(getTotalItemCount())
134             .toString();
135     }
136 }
137
Popular Tags