| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| Pageable |
|
| 2.076923076923077;2.077 |
| 1 | /* | |
| 2 | * Copyright (c) 2003-2011, Simon Brown | |
| 3 | * All rights reserved. | |
| 4 | * | |
| 5 | * Redistribution and use in source and binary forms, with or without | |
| 6 | * modification, are permitted provided that the following conditions are met: | |
| 7 | * | |
| 8 | * - Redistributions of source code must retain the above copyright | |
| 9 | * notice, this list of conditions and the following disclaimer. | |
| 10 | * | |
| 11 | * - Redistributions in binary form must reproduce the above copyright | |
| 12 | * notice, this list of conditions and the following disclaimer in | |
| 13 | * the documentation and/or other materials provided with the | |
| 14 | * distribution. | |
| 15 | * | |
| 16 | * - Neither the name of Pebble nor the names of its contributors may | |
| 17 | * be used to endorse or promote products derived from this software | |
| 18 | * without specific prior written permission. | |
| 19 | * | |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
| 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
| 23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | |
| 24 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
| 25 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
| 26 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
| 27 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
| 28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
| 29 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
| 30 | * POSSIBILITY OF SUCH DAMAGE. | |
| 31 | */ | |
| 32 | package net.sourceforge.pebble.util; | |
| 33 | ||
| 34 | import java.util.List; | |
| 35 | ||
| 36 | /** | |
| 37 | * Helper class that implements paging over a collection. | |
| 38 | * | |
| 39 | * @author Simon Brown | |
| 40 | */ | |
| 41 | public class Pageable<T> { | |
| 42 | ||
| 43 | /** the default page size */ | |
| 44 | public static final int DEFAULT_PAGE_SIZE = 10; | |
| 45 | ||
| 46 | private static final int PAGE_WINDOW = 10; | |
| 47 | ||
| 48 | /** the list over which this class is paging */ | |
| 49 | private List<T> list; | |
| 50 | ||
| 51 | /** the page size */ | |
| 52 | 44 | private int pageSize = DEFAULT_PAGE_SIZE; |
| 53 | ||
| 54 | /** the current page */ | |
| 55 | private int page; | |
| 56 | ||
| 57 | /** the starting index */ | |
| 58 | private int startingIndex; | |
| 59 | ||
| 60 | /** the ending index */ | |
| 61 | private int endingIndex; | |
| 62 | ||
| 63 | /** the maximum number of pages */ | |
| 64 | private int maxPages; | |
| 65 | ||
| 66 | /** | |
| 67 | * Creates a new instance with the specified list. | |
| 68 | * | |
| 69 | * @param list a List | |
| 70 | */ | |
| 71 | 44 | public Pageable(List<T> list) { |
| 72 | 44 | this.list = list; |
| 73 | 44 | this.page = 1; |
| 74 | 44 | this.maxPages = 1; |
| 75 | ||
| 76 | 44 | calculatePages(); |
| 77 | 44 | } |
| 78 | ||
| 79 | private void calculatePages() { | |
| 80 | 84 | if (pageSize > 0) { |
| 81 | // calculate how many pages there are | |
| 82 | 84 | if (list.size() % pageSize == 0) { |
| 83 | 68 | maxPages = list.size() / pageSize; |
| 84 | } else { | |
| 85 | 16 | maxPages = (list.size() / pageSize) + 1; |
| 86 | } | |
| 87 | } | |
| 88 | 84 | } |
| 89 | ||
| 90 | /** | |
| 91 | * Gets the list that this instance is paging over. | |
| 92 | * | |
| 93 | * @return a List | |
| 94 | */ | |
| 95 | public List<T> getList() { | |
| 96 | 12 | return this.list; |
| 97 | } | |
| 98 | ||
| 99 | /** | |
| 100 | * Gets the subset of the list for the current page. | |
| 101 | * | |
| 102 | * @return a List | |
| 103 | */ | |
| 104 | public List<T> getListForPage() { | |
| 105 | 40 | return list.subList(startingIndex, endingIndex); |
| 106 | } | |
| 107 | ||
| 108 | /** | |
| 109 | * Gets the page size. | |
| 110 | * | |
| 111 | * @return the page size as an int | |
| 112 | */ | |
| 113 | public int getPageSize() { | |
| 114 | 4 | return this.pageSize; |
| 115 | } | |
| 116 | ||
| 117 | /** | |
| 118 | * Sets the page size. | |
| 119 | * | |
| 120 | * @param pageSize the page size as an int | |
| 121 | */ | |
| 122 | public void setPageSize(int pageSize) { | |
| 123 | 40 | this.pageSize = pageSize; |
| 124 | 40 | calculatePages(); |
| 125 | 40 | } |
| 126 | ||
| 127 | /** | |
| 128 | * Gets the page. | |
| 129 | * | |
| 130 | * @return the page as an int | |
| 131 | */ | |
| 132 | public int getPage() { | |
| 133 | 120 | return this.page; |
| 134 | } | |
| 135 | ||
| 136 | /** | |
| 137 | * Sets the page size. | |
| 138 | * | |
| 139 | * @param p the page as an int | |
| 140 | */ | |
| 141 | public void setPage(int p) { | |
| 142 | 104 | if (p >= maxPages) { |
| 143 | 36 | this.page = maxPages; |
| 144 | 68 | } else if (p <= 1) { |
| 145 | 24 | this.page = 1; |
| 146 | } else { | |
| 147 | 44 | this.page = p; |
| 148 | } | |
| 149 | ||
| 150 | // now work out where the sub-list should start and end | |
| 151 | 104 | startingIndex = pageSize * (page-1); |
| 152 | 104 | if (startingIndex < 0) { |
| 153 | 8 | startingIndex = 0; |
| 154 | } | |
| 155 | 104 | endingIndex = startingIndex + pageSize; |
| 156 | 104 | if (endingIndex > list.size()) { |
| 157 | 20 | endingIndex = list.size(); |
| 158 | } | |
| 159 | 104 | } |
| 160 | ||
| 161 | /** | |
| 162 | * Gets the maximum number of pages. | |
| 163 | * | |
| 164 | * @return the maximum number of pages as an int | |
| 165 | */ | |
| 166 | public int getMaxPages() { | |
| 167 | 84 | return this.maxPages; |
| 168 | } | |
| 169 | ||
| 170 | /** | |
| 171 | * Determines whether there is a previous page and gets the page number. | |
| 172 | * | |
| 173 | * @return the previous page number, or zero | |
| 174 | */ | |
| 175 | public int getPreviousPage() { | |
| 176 | 44 | if (page > 1) { |
| 177 | 12 | return page-1; |
| 178 | } else { | |
| 179 | 32 | return 0; |
| 180 | } | |
| 181 | } | |
| 182 | ||
| 183 | /** | |
| 184 | * Determines whether there is a next page and gets the page number. | |
| 185 | * | |
| 186 | * @return the next page number, or 0 | |
| 187 | */ | |
| 188 | public int getNextPage() { | |
| 189 | 44 | if (page < maxPages) { |
| 190 | 16 | return page+1; |
| 191 | } else { | |
| 192 | 28 | return 0; |
| 193 | } | |
| 194 | } | |
| 195 | ||
| 196 | /** | |
| 197 | * Gets the minimum page in the window. | |
| 198 | * | |
| 199 | * @return the page number | |
| 200 | */ | |
| 201 | public int getMinPageRange() { | |
| 202 | 28 | if (getPage() > PAGE_WINDOW) { |
| 203 | 12 | return getPage() - PAGE_WINDOW; |
| 204 | } else { | |
| 205 | 16 | return 1; |
| 206 | } | |
| 207 | } | |
| 208 | ||
| 209 | /** | |
| 210 | * Gets the maximum page in the window. | |
| 211 | * | |
| 212 | * @return the page number | |
| 213 | */ | |
| 214 | public int getMaxPageRange() { | |
| 215 | 24 | if (getPage() < (getMaxPages() - PAGE_WINDOW)) { |
| 216 | 8 | return getPage() + PAGE_WINDOW; |
| 217 | } else { | |
| 218 | 16 | return getMaxPages(); |
| 219 | } | |
| 220 | } | |
| 221 | ||
| 222 | } |