Implementation of PaginatedList backed by an ArrayList : Paging « Collections Data Structure « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Collections Data Structure » Paging 




Implementation of PaginatedList backed by an ArrayList
 

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

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

/**
 * Implementation of PaginatedList backed by an ArrayList
 
 */
public class PaginatedArrayList implements PaginatedList {

  private static final ArrayList EMPTY_LIST = new ArrayList(0);

  private List list;

  private List page;

  private int pageSize;

  private int index;

  /**
   @param pageSize
   */
  public PaginatedArrayList(int pageSize) {
    this.pageSize = pageSize;
    this.index = 0;
    this.list = new ArrayList();
    repaginate();
  }

  /**
   * Constructor to set the initial size and the page size
   
   @param initialCapacity -
   *          the initial size
   @param pageSize -
   *          the page size
   */
  public PaginatedArrayList(int initialCapacity, int pageSize) {
    this.pageSize = pageSize;
    this.index = 0;
    this.list = new ArrayList(initialCapacity);
    repaginate();
  }

  /**
   * Constructor to create an instance using an existing collection
   
   @param c -
   *          the collection to build the instance with
   @param pageSize -
   *          the page size
   */
  public PaginatedArrayList(Collection c, int pageSize) {
    this.pageSize = pageSize;
    this.index = 0;
    this.list = new ArrayList(c);
    repaginate();
  }

  private void repaginate() {
    if (list.isEmpty()) {
      page = EMPTY_LIST;
    else {
      int start = index * pageSize;
      int end = start + pageSize - 1;
      if (end >= list.size()) {
        end = list.size() 1;
      }
      if (start >= list.size()) {
        index = 0;
        repaginate();
      else if (start < 0) {
        index = list.size() / pageSize;
        if (list.size() % pageSize == 0) {
          index--;
        }
        repaginate();
      else {
        page = list.subList(start, end 1);
      }
    }
  }

  /* List accessors (uses page) */

  public int size() {
    return page.size();
  }

  public boolean isEmpty() {
    return page.isEmpty();
  }

  public boolean contains(Object o) {
    return page.contains(o);
  }

  public Iterator iterator() {
    return page.iterator();
  }

  public Object[] toArray() {
    return page.toArray();
  }

  public Object[] toArray(Object a[]) {
    return page.toArray(a);
  }

  public boolean containsAll(Collection c) {
    return page.containsAll(c);
  }

  public Object get(int index) {
    return page.get(index);
  }

  public int indexOf(Object o) {
    return page.indexOf(o);
  }

  public int lastIndexOf(Object o) {
    return page.lastIndexOf(o);
  }

  public ListIterator listIterator() {
    return page.listIterator();
  }

  public ListIterator listIterator(int index) {
    return page.listIterator(index);
  }

  public List subList(int fromIndex, int toIndex) {
    return page.subList(fromIndex, toIndex);
  }

  /* List mutators (uses master list) */

  public boolean add(Object o) {
    boolean b = list.add(o);
    repaginate();
    return b;
  }

  public boolean remove(Object o) {
    boolean b = list.remove(o);
    repaginate();
    return b;
  }

  public boolean addAll(Collection c) {
    boolean b = list.addAll(c);
    repaginate();
    return b;
  }

  public boolean addAll(int index, Collection c) {
    boolean b = list.addAll(index, c);
    repaginate();
    return b;
  }

  public boolean removeAll(Collection c) {
    boolean b = list.removeAll(c);
    repaginate();
    return b;
  }

  public boolean retainAll(Collection c) {
    boolean b = list.retainAll(c);
    repaginate();
    return b;
  }

  public void clear() {
    list.clear();
    repaginate();
  }

  public Object set(int index, Object element) {
    Object o = list.set(index, element);
    repaginate();
    return o;
  }

  public void add(int index, Object element) {
    list.add(index, element);
    repaginate();
  }

  public Object remove(int index) {
    Object o = list.remove(index);
    repaginate();
    return o;
  }

  /* Paginated List methods */

  public int getPageSize() {
    return pageSize;
  }

  public boolean isFirstPage() {
    return index == 0;
  }

  public boolean isMiddlePage() {
    return !(isFirstPage() || isLastPage());
  }

  public boolean isLastPage() {
    return list.size() ((index + 1* pageSize1;
  }

  public boolean isNextPageAvailable() {
    return !isLastPage();
  }

  public boolean isPreviousPageAvailable() {
    return !isFirstPage();
  }

  public boolean nextPage() {
    if (isNextPageAvailable()) {
      index++;
      repaginate();
      return true;
    else {
      return false;
    }
  }

  public boolean previousPage() {
    if (isPreviousPageAvailable()) {
      index--;
      repaginate();
      return true;
    else {
      return false;
    }
  }

  public void gotoPage(int pageNumber) {
    index = pageNumber;
    repaginate();
  }

  public int getPageIndex() {
    return index;
  }

}

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

/**
 * Interface for lists that support paging
 
 */
interface PaginatedList extends List {

  /**
   * Returns the maximum number of items per page
   
   @return The maximum number of items per page.
   */
  public int getPageSize();

  /**
   * Is the current page the first page?
   
   @return True if the current page is the first page or if only a single page
   *         exists.
   */
  public boolean isFirstPage();

  /**
   * Is the current page a middle page (ie not first or last)?
   
   @return True if the current page is not the first or last page, and more
   *         than one page exists (always returns false if only a single page
   *         exists).
   */
  public boolean isMiddlePage();

  /**
   * Is the current page the last page?
   
   @return True if the current page is the last page or if only a single page
   *         exists.
   */
  public boolean isLastPage();

  /**
   * Is a page available after the current page?
   
   @return True if the next page is available
   */
  public boolean isNextPageAvailable();

  /**
   * Is a page available before the current page?
   
   @return True if the previous page is available
   */
  public boolean isPreviousPageAvailable();

  /**
   * Moves to the next page after the current page. If the current page is the
   * last page, wrap to the first page.
   
   @return True if the page changed
   */
  public boolean nextPage();

  /**
   * Moves to the page before the current page. If the current page is the first
   * page, wrap to the last page.
   
   @return True if the page changed
   */
  public boolean previousPage();

  /**
   * Moves to a specified page. If the specified page is beyond the last page,
   * wrap to the first page. If the specified page is before the first page,
   * wrap to the last page.
   
   @param pageNumber
   *          The page to go to
   */
  public void gotoPage(int pageNumber);

  /**
   * Returns the current page index, which is a zero based integer. All
   * paginated list implementations should know what index they are on, even if
   * they don't know the ultimate boundaries (min/max).
   
   @return The current page
   */
  public int getPageIndex();

}

   
  














Related examples in the same category
1.Paging over a collection
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.