cn.yiyizuche.common.base.Page.java Source code

Java tutorial

Introduction

Here is the source code for cn.yiyizuche.common.base.Page.java

Source

/**
 * Copyright (c) 2005-2011 springside.org.cn
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * 
 * $Id: Page.java 1453 2011-01-02 14:21:45Z calvinxiu $
 */
package cn.yiyizuche.common.base;

import org.apache.commons.collections.IteratorUtils;
import org.apache.commons.lang.StringUtils;

import java.util.*;

/**
 * ORM???. ???, ?Paginator.
 * 
 * @param <T>
 *            Page.
 * 
 */
public class Page<T> implements Iterable<T> {

    public Map<String, Object> getGridMap() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("current", (getPageNo() < 0) ? 0 : getPageNo());
        map.put("rowCount", (getPageSize() < 0) ? 0 : getPageSize());
        map.put("rows", getResult());
        map.put("total", (getTotalItems() < 0) ? 0 : getTotalItems());
        return map;
    }

    // -- ?? --//
    public static final String ASC = "asc";
    public static final String DESC = "desc";

    // -- ? --//
    protected int pageNo = 1;
    protected int pageSize = -1;
    protected boolean autoCount = true;
    protected String orderBy = null;
    protected String order = null;

    // --  --//
    protected List<T> result = new ArrayList<T>();
    protected long totalItems = -1;

    // --  --//
    public Page() {
    }

    public Page(int pageSize) {
        setPageSize(pageSize);
    }

    public Page(int pageNo, int pageSize) {
        setPageNo(pageNo);
        setPageSize(pageSize);
    }

    // -- ? --//
    /**
     * ??,??1,1.
     */
    public int getPageNo() {
        return pageNo;
    }

    /**
     * ??,??1,11.
     */
    public void setPageNo(final int pageNo) {
        this.pageNo = pageNo;

        if (pageNo < 1) {
            this.pageNo = 1;
        }
    }

    /**
     * ??, -1.
     */
    public int getPageSize() {
        return pageSize;
    }

    /**
     * ??.
     */
    public void setPageSize(final int pageSize) {
        this.pageSize = pageSize;
    }

    /**
     * ?,. ?','.
     */
    public String getOrderBy() {
        return orderBy;
    }

    /**
     * ?,?','.
     */
    public void setOrderBy(final String orderBy) {
        this.orderBy = orderBy;
    }

    /**
     * ??, .
     */
    public String getOrder() {
        return order;
    }

    /**
     * ???.
     * 
     * @param order
     *            ?descasc,?','.
     */
    public void setOrder(final String order) {
        String lowcaseOrder = StringUtils.lowerCase(order);

        // order?
        String[] orders = StringUtils.split(lowcaseOrder, ',');
        for (String orderStr : orders) {
            if (!StringUtils.equals(DESC, orderStr) && !StringUtils.equals(ASC, orderStr)) {
                throw new IllegalArgumentException("??" + orderStr + "??");
            }
        }

        this.order = lowcaseOrder;
    }

    /**
     * ??,.
     */
    public boolean isOrderBySetted() {
        return (StringUtils.isNotBlank(orderBy) && StringUtils.isNotBlank(order));
    }

    /**
     * ?pageNopageSize???,??0. Mysql,Hibernate.
     */
    public int getOffset() {
        return ((pageNo - 1) * pageSize);
    }

    /**
     * ?pageNopageSize???,??1. Oracle.
     */
    public int getStartRow() {
        return getOffset() + 1;
    }

    /**
     * ?pageNopageSize????, ??1. Oracle.
     */
    public int getEndRow() {
        return pageSize * pageNo;
    }

    // --  --//

    /**
     * .
     */
    public List<T> getResult() {
        return result;
    }

    /**
     * .
     */
    public void setResult(final List<T> result) {
        this.result = result;
    }

    /**
     * Iterable?,?for(Object item : page)??
     */
    @SuppressWarnings("unchecked")
    public Iterator<T> iterator() {
        return result == null ? IteratorUtils.EMPTY_ITERATOR : result.iterator();
    }

    /**
     * , -1.
     */
    public long getTotalItems() {
        return totalItems;
    }

    /**
     * .
     */
    public void setTotalItems(final long totalItems) {
        this.totalItems = totalItems;
    }

    /**
     * ??.
     */
    public boolean isLastPage() {
        return pageNo == getTotalPages();
    }

    /**
     * ?.
     */
    public boolean isHasNextPage() {
        return (pageNo + 1 <= getTotalPages());
    }

    /**
     * ??, ??1. ????.
     */
    public int getNextPage() {
        if (isHasNextPage()) {
            return pageNo + 1;
        } else {
            return pageNo;
        }
    }

    /**
     * ?.
     */
    public boolean isFirstPage() {
        return pageNo == 1;
    }

    /**
     * ?.
     */
    public boolean isHasPrePage() {
        return (pageNo - 1 >= 1);
    }

    /**
     * ??, ??1. ???.
     */
    public int getPrePage() {
        if (isHasPrePage()) {
            return pageNo - 1;
        } else {
            return pageNo;
        }
    }

    /**
     * ?pageSizetotalItems, -1.
     */
    public long getTotalPages() {
        if (totalItems < 0) {
            return -1;
        }

        long count = totalItems / pageSize;
        if (totalItems % pageSize > 0) {
            count++;
        }
        return count;
    }

    /**
     * ??,",23,24,25,26,27,"
     * 
     * @param count
     *            ??
     * @return pageNo
     */
    public List<Long> getSlider(int count) {
        int halfSize = count / 2;
        long totalPage = getTotalPages();

        long startPageNumber = Math.max(pageNo - halfSize, 1);
        long endPageNumber = Math.min(startPageNumber + count - 1, totalPage);

        if (endPageNumber - startPageNumber < count) {
            startPageNumber = Math.max(endPageNumber - count, 1);
        }

        List<Long> result = new ArrayList<Long>();
        for (long i = startPageNumber; i <= endPageNumber; i++) {
            result.add(new Long(i));
        }
        return result;
    }
}