com.cndatacom.core.orm.Page.java Source code

Java tutorial

Introduction

Here is the source code for com.cndatacom.core.orm.Page.java

Source

/**
 * Copyright (c) 2005-2009 springside.org.cn
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * 
 * $Id: Page.java,v 1.2 2012/07/23 01:46:22 zhangyx Exp $
 */
package com.cndatacom.core.orm;

import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.google.common.collect.Lists;

/**
 * ORM???.
 * ???1.
 * 
 * @param <T> Page.
 * 
 * @author calvin
 */
public class Page<T> {
    //-- ?? --//
    public static final String ASC = "asc";
    public static final String DESC = "desc";

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

    //--  --//
    protected List<T> result = Lists.newArrayList();
    protected long totalCount = -1;

    private boolean success = true;

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

    public Page(int pageSize) {
        this.pageSize = pageSize;
    }

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

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

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

    /**
     * ??,??1,11.
     */
    public void setPageStart(final int pageStart) {
        this.pageNo = pageStart / pageSize + 1;

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

    public Page<T> pageNo(final int thePageNo) {
        setPageNo(thePageNo);
        return this;
    }

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

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

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

    public Page<T> pageSize(final int thePageSize) {
        setPageSize(thePageSize);
        return this;
    }

    /**
     * ?pageNopageSize???,??1.
     */
    public int getFirst() {
        //((pageNo - 1) * pageSize) + 1;
        return start;
    }

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

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

    public Page<T> orderBy(final String theOrderBy) {
        setOrderBy(theOrderBy);
        return this;
    }

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

    /**
     * ???.
     * 
     * @param order ?descasc,?','.
     */
    public void setOrder(final String order) {
        //order?
        String[] orders = StringUtils.split(StringUtils.lowerCase(order), ',');
        for (String orderStr : orders) {
            if (!StringUtils.equals(DESC, orderStr) && !StringUtils.equals(ASC, orderStr)) {
                throw new IllegalArgumentException("??" + orderStr + "??");
            }
        }

        this.order = StringUtils.lowerCase(order);
    }

    public Page<T> order(final String theOrder) {
        setOrder(theOrder);
        return this;
    }

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

    /**
     * ??count?, false.
     */
    public boolean isAutoCount() {
        return autoCount;
    }

    /**
     * ??count?.
     */
    public void setAutoCount(final boolean autoCount) {
        this.autoCount = autoCount;
    }

    public Page<T> autoCount(final boolean theAutoCount) {
        setAutoCount(theAutoCount);
        return this;
    }

    //--  --//

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

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

    /**
     * ?, -1.
     */
    public long getTotalCount() {
        return totalCount;
    }

    /**
     * .
     */
    public void setTotalCount(final long totalCount) {
        this.totalCount = totalCount;
    }

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

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

        long count = totalCount / this.getPageSize();
        if (totalCount % this.getPageSize() > 0) {
            count++;
        }
        return count;
    }

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

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

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

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

    public int getStart() {
        return start;
    }

    public void setStart(int start) {
        this.start = start;
    }

    public int getLimit() {
        return limit;
    }

    public void setLimit(int limit) {
        this.limit = limit;
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }
}