pageable.java

### File Source: pageable.java

             /*
        P/P   *  Method: net.sourceforge.pebble.util.Pageable__static_init
              */
         1  /*
         2   * Copyright (c) 2003-2006, 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    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     */
               /*
        P/P     *  Method: void net.sourceforge.pebble.util.Pageable(List)
                *
                *  Preconditions:
                *    (soft) list != null
                *
                *  Postconditions:
                *    this.list == list
                *    (soft) this.list != null
                *    init'ed(this.maxPages)
                *    this.page == 1
                *    this.pageSize == 10
                */
        71    public Pageable(List<T> list) {
        72      this.list = list;
        73      this.page = 1;
        74      this.maxPages = 1;
        75
        76      calculatePages();
        77    }
        78
        79    private void calculatePages() {
                 /*
        P/P       *  Method: void calculatePages()
                  *
                  *  Preconditions:
                  *    init'ed(this.pageSize)
                  *    (soft) this.list != null
                  *
                  *  Presumptions:
                  *    java.util.List:size(...)@85/this.pageSize in -232+1..232-2
                  *
                  *  Postconditions:
                  *    possibly_updated(this.maxPages)
                  *
                  *  Test Vectors:
                  *    this.pageSize: {-231..0}, {1..232-1}
                  */
        80      if (pageSize > 0) {
        81        // calculate how many pages there are
        82        if (list.size() % pageSize == 0) {
        83          maxPages = list.size() / pageSize;
        84        } else {
        85          maxPages = (list.size() / pageSize) + 1;
        86        }
        87      }
        88    }
        89
        90    /**
        91     * Gets the list that this instance is paging over.
        92     *
        93     * @return  a List
        94     */
        95    public List<T> getList() {
                 /*
        P/P       *  Method: List getList()
                  *
                  *  Preconditions:
                  *    init'ed(this.list)
                  *
                  *  Postconditions:
                  *    return_value == this.list
                  *    init'ed(return_value)
                  */
        96      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() {
                 /*
        P/P       *  Method: List getListForPage()
                  *
                  *  Preconditions:
                  *    init'ed(this.endingIndex)
                  *    this.list != null
                  *    init'ed(this.startingIndex)
                  *
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
       105      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() {
                 /*
        P/P       *  Method: int getPageSize()
                  *
                  *  Preconditions:
                  *    init'ed(this.pageSize)
                  *
                  *  Postconditions:
                  *    return_value == this.pageSize
                  *    init'ed(return_value)
                  */
       114      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) {
                 /*
        P/P       *  Method: void setPageSize(int)
                  *
                  *  Preconditions:
                  *    (soft) this.list != null
                  *
                  *  Postconditions:
                  *    possibly_updated(this.maxPages)
                  *    this.pageSize == pageSize
                  *    init'ed(this.pageSize)
                  */
       123      this.pageSize = pageSize;
       124      calculatePages();
       125    }
       126
       127    /**
       128     * Gets the page.
       129     *
       130     * @return  the page as an int
       131     */
       132    public int getPage() {
                 /*
        P/P       *  Method: int getPage()
                  *
                  *  Preconditions:
                  *    init'ed(this.page)
                  *
                  *  Postconditions:
                  *    return_value == this.page
                  *    init'ed(return_value)
                  */
       133      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) {
                 /*
        P/P       *  Method: void setPage(int)
                  *
                  *  Preconditions:
                  *    this.list != null
                  *    init'ed(this.maxPages)
                  *    init'ed(this.pageSize)
                  *
                  *  Postconditions:
                  *    init'ed(this.endingIndex)
                  *    this.page == One-of{this.maxPages, 1, p}
                  *    init'ed(this.page)
                  *    this.startingIndex == One-of{this.pageSize*(this.page - 1), 0}
                  *    this.startingIndex >= 0
                  *    this.pageSize + this.startingIndex in -231..232-1
                  *    this.pageSize*(this.page - 1) in -231..232-1
                  *
                  *  Test Vectors:
                  *    p: {2..232-2}, {-231..1}
                  *    this.maxPages - p: {1..232-3}, {-6_442_450_943..0}
                  */
       142      if (p >= maxPages) {
       143        this.page = maxPages;
       144      } else if (p <= 1) {
       145        this.page = 1;
       146      } else {
       147        this.page = p;
       148      }
       149
       150      // now work out where the sub-list should start and end
       151      startingIndex = pageSize * (page-1);
       152      if (startingIndex < 0) {
       153        startingIndex = 0;
       154      }
       155      endingIndex = startingIndex + pageSize;
       156      if (endingIndex > list.size()) {
       157        endingIndex = list.size();
       158      }
       159    }
       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() {
                 /*
        P/P       *  Method: int getMaxPages()
                  *
                  *  Preconditions:
                  *    init'ed(this.maxPages)
                  *
                  *  Postconditions:
                  *    return_value == this.maxPages
                  *    init'ed(return_value)
                  */
       167      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() {
                 /*
        P/P       *  Method: int getPreviousPage()
                  *
                  *  Preconditions:
                  *    init'ed(this.page)
                  *
                  *  Postconditions:
                  *    return_value == One-of{this.page - 1, 0}
                  *    return_value in 0..232-2
                  *
                  *  Test Vectors:
                  *    this.page: {-231..1}, {2..232-1}
                  */
       176      if (page > 1) {
       177        return page-1;
       178      } else {
       179        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() {
                 /*
        P/P       *  Method: int getNextPage()
                  *
                  *  Preconditions:
                  *    init'ed(this.maxPages)
                  *    init'ed(this.page)
                  *
                  *  Postconditions:
                  *    return_value == One-of{this.page + 1, 0}
                  *    return_value >= -231+1
                  *
                  *  Test Vectors:
                  *    this.maxPages - this.page: {-6_442_450_943..0}, {1..6_442_450_943}
                  */
       189      if (page < maxPages) {
       190        return page+1;
       191      } else {
       192        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() {
                 /*
        P/P       *  Method: int getMinPageRange()
                  *
                  *  Preconditions:
                  *    init'ed(this.page)
                  *
                  *  Postconditions:
                  *    return_value == One-of{this.page - 10, 1}
                  *    return_value in 1..232-11
                  *
                  *  Test Vectors:
                  *    this.page: {-231..10}, {11..232-1}
                  */
       202      if (getPage() > PAGE_WINDOW) {
       203        return getPage() - PAGE_WINDOW;
       204      } else {
       205        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() {
                 /*
        P/P       *  Method: int getMaxPageRange()
                  *
                  *  Preconditions:
                  *    init'ed(this.maxPages)
                  *    init'ed(this.page)
                  *
                  *  Postconditions:
                  *    return_value == One-of{this.page + 10, this.maxPages}
                  *    init'ed(return_value)
                  *
                  *  Test Vectors:
                  *    this.maxPages - this.page: {-6_442_450_943..10}, {11..6_442_450_943}
                  */
       215      if (getPage() < (getMaxPages() - PAGE_WINDOW)) {
       216        return getPage() + PAGE_WINDOW;
       217      } else {
       218        return getMaxPages();
       219      }
       220    }
       221
       222  }

SofCheck Inspector Build Version : 2.22510

pageable.java | 2010-Jun-25 19:40:32
pageable.class | 2010-Jul-19 20:23:38