com.weibo.common.utils.ResultPage.java Source code

Java tutorial

Introduction

Here is the source code for com.weibo.common.utils.ResultPage.java

Source

package com.weibo.common.utils;

/**
 * Copyright (c) 2005-2010 springside.org.cn
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * 
 * $Id: Page.java 1183 2010-08-28 08:05:49Z calvinxiu $
 */
import java.io.Serializable;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.dw.party.mbmsupport.dto.Result;
import com.google.common.collect.Lists;

/**
 * ????result,???,?
 * @param <T> Page
 * @author zel
 */
public class ResultPage<T> extends Result implements Serializable {
    //--??--//
    /**
     * --??--
     */
    public static final String ASC = "asc";
    public static final String DESC = "desc";

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

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

    //-- --//
    /**
     * 
     */
    public ResultPage() {
        this.pageSize = 50;
    }

    /**
     * ????
     * @param pageSize
     */
    public ResultPage(int pageSize) {
        this.pageSize = pageSize;
    }

    /**
     * ?
     */
    public int getPageNo() {
        return pageNo;
    }

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

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

    /**
     * ??page
     */
    public ResultPage<T> pageNo(final int thePageNo) {
        setPageNo(thePageNo);
        return this;
    }

    /**
     * ?result?
     */
    public int getPageSize() {
        return pageSize;
    }

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

    /**
     * ???page
     */
    public ResultPage<T> pageSize(final int thePageSize) {
        setPageSize(thePageSize);
        return this;
    }

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

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

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

    /**
     * ?page 
     */
    public ResultPage<T> orderBy(final String theOrderBy) {
        setOrderBy(theOrderBy);
        return this;
    }

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

    /**
     *
     * ??
     * @param order
     */
    public void setOrder(final String order) {
        String lowcaseOrder = StringUtils.lowerCase(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 ResultPage<T> order(final String theOrder) {
        setOrder(theOrder);
        return this;
    }

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

    /**
     * ?
     */
    public boolean isAutoCount() {
        return autoCount;
    }

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

    /**
     * ??
     */
    public ResultPage<T> autoCount(final boolean theAutoCount) {
        setAutoCount(theAutoCount);
        return this;
    }

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

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

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

    /**
     * ???
     */
    public long getTotalCount() {
        return totalCount;
    }

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

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

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

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

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

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

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