com.noah.ai.platform.pub.entity.Pager.java Source code

Java tutorial

Introduction

Here is the source code for com.noah.ai.platform.pub.entity.Pager.java

Source

/*
 * Pager.java
 *
 * Created Date: 201675
 *            
 * Copyright (c)  Noah Technologies Co., Ltd.
 *
 * This software is the confidential and proprietary information of
 *  Noah Technologies Co., Ltd. ("Confidential Information"). You shall not
 * disclose such Confidential Information and shall use it only in accordance
 * with the terms of the license agreement you entered into with
 * Noah Technologies Co., Ltd.
 */

package com.noah.ai.platform.pub.entity;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;

import com.noah.ai.platform.pub.util.CookieUtils;

/**
 * @author guoshuai
 * @version  <br>
 * <p>???       ##############?mybatis</p>
 */

public class Pager<T> {

    private int pageNo = 1; // ??
    private int pageSize = 50;

    private long count; // ??-1??
    private int first;// 
    private int last;// 
    private int prev;// 
    private int next;// 
    private boolean firstPage;// ?
    private boolean lastPage;// ??
    private int length = 8;// ?
    private int slider = 1;// ???
    private List<T> list = new ArrayList<T>();
    private String funcName = "page"; // ?js??page
    private String funcParam = ""; // ??
    private String message = ""; // ???n???

    public Pager() {
        this.pageSize = -1;
    }

    /**
     * 
     * 
     * @param pageNo
     *            ??
     * @param pageSize
     *            ?
     */
    public Pager(int pageNo, int pageSize) {
        this(pageNo, pageSize, 0);
    }

    /**
     * 
     * @param request  repage ????
     * @param response  Cookie??
     */
    public Pager(HttpServletRequest request, HttpServletResponse response) {
        this(request, response, -2);
    }

    public Pager(HttpServletRequest request, HttpServletResponse response, int defaultPageSize) {
        // ??repage????
        String no = request.getParameter("pageNo");
        if (StringUtils.isNumeric(no)) {
            CookieUtils.setCookie(response, "pageNo", no);
            this.setPageNo(Integer.parseInt(no));
        } else if (request.getParameter("repage") != null) {
            no = CookieUtils.getCookie(request, "pageNo");
            if (StringUtils.isNumeric(no)) {
                this.setPageNo(Integer.parseInt(no));
            }
        }
        // ???repage?????
        String size = request.getParameter("pageSize");
        if (StringUtils.isNumeric(size)) {
            CookieUtils.setCookie(response, "pageSize", size);
            this.setPageSize(Integer.parseInt(size));
        } else if (request.getParameter("repage") != null) {
            no = CookieUtils.getCookie(request, "pageSize");
            if (StringUtils.isNumeric(size)) {
                this.setPageSize(Integer.parseInt(size));
            }
        } else if (defaultPageSize != -2) {
            this.pageSize = defaultPageSize;
        }
    }

    public Pager(int pageNo, int pageSize, long count) {
        this(pageNo, pageSize, count, new ArrayList<T>());
    }

    public Pager(int pageNo, int pageSize, long count, List<T> list) {
        this.setCount(count);
        this.setPageNo(pageNo);
        this.pageSize = pageSize;
        this.list = list;
    }

    public void initialize() {
        // 1
        this.first = 1;

        this.last = (int) (count / (this.pageSize < 1 ? 20 : this.pageSize) + first - 1);

        if (this.count % this.pageSize != 0 || this.last == 0) {
            this.last++;
        }

        if (this.last < this.first) {
            this.last = this.first;
        }

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

        if (this.pageNo >= this.last) {
            this.pageNo = this.last;
            this.lastPage = true;
        }

        if (this.pageNo < this.last - 1) {
            this.next = this.pageNo + 1;
        } else {
            this.next = this.last;
        }

        if (this.pageNo > 1) {
            this.prev = this.pageNo - 1;
        } else {
            this.prev = this.first;
        }

        // 2
        if (this.pageNo < this.first) {// ??
            this.pageNo = this.first;
        }

        if (this.pageNo > this.last) {// ?
            this.pageNo = this.last;
        }

    }

    public List<T> getPagedList() {
        if (pageNo < 1) {
            pageNo = 1;
        }
        int fromIndex = (pageNo - 1) * pageSize;
        if (fromIndex >= list.size()) {
            return Collections.emptyList();
        }

        int toIndex = pageNo * pageSize;
        if (toIndex >= list.size()) {
            toIndex = list.size();
        }
        return list.subList(fromIndex, toIndex);
    }

    @Override
    public String toString() {

        StringBuilder sb = new StringBuilder();

        if (pageNo == first) {// 
            sb.append("<li class=\"disabled\"><a href=\"javascript:\">&#171; </a></li>\n");
        } else {
            sb.append("<li><a href=\"javascript:\" onclick=\"" + funcName + "(" + prev + "," + pageSize + ",'"
                    + funcParam + "');\">&#171; </a></li>\n");
        }

        int begin = pageNo - (length / 2);

        if (begin < first) {
            begin = first;
        }

        int end = begin + length - 1;

        if (end >= last) {
            end = last;
            begin = end - length + 1;
            if (begin < first) {
                begin = first;
            }
        }

        if (begin > first) {
            int i = 0;
            for (i = first; i < first + slider && i < begin; i++) {
                sb.append("<li><a href=\"javascript:\" onclick=\"" + funcName + "(" + i + "," + pageSize + ",'"
                        + funcParam + "');\">" + (i + 1 - first) + "</a></li>\n");
            }
            if (i < begin) {
                sb.append("<li class=\"disabled\"><a href=\"javascript:\">...</a></li>\n");
            }
        }

        for (int i = begin; i <= end; i++) {
            if (i == pageNo) {
                sb.append("<li class=\"active\"><a href=\"javascript:\">" + (i + 1 - first) + "</a></li>\n");
            } else {
                sb.append("<li><a href=\"javascript:\" onclick=\"" + funcName + "(" + i + "," + pageSize + ",'"
                        + funcParam + "');\">" + (i + 1 - first) + "</a></li>\n");
            }
        }

        if (last - end > slider) {
            sb.append("<li class=\"disabled\"><a href=\"javascript:\">...</a></li>\n");
            end = last - slider;
        }

        for (int i = end + 1; i <= last; i++) {
            sb.append("<li><a href=\"javascript:\" onclick=\"" + funcName + "(" + i + "," + pageSize + ",'"
                    + funcParam + "');\">" + (i + 1 - first) + "</a></li>\n");
        }

        if (pageNo == last) {
            sb.append("<li class=\"disabled\"><a href=\"javascript:\"> &#187;</a></li>\n");
        } else {
            sb.append("<li><a href=\"javascript:\" onclick=\"" + funcName + "(" + next + "," + pageSize + ",'"
                    + funcParam + "');\">" + " &#187;</a></li>\n");
        }

        sb.append("<li class=\"disabled controls\"><a href=\"javascript:\">? ");
        sb.append("<input type=\"text\" value=\"" + pageNo
                + "\" onkeypress=\"var e=window.event||this;var c=e.keyCode||e.which;if(c==13)");
        sb.append(
                funcName + "(this.value," + pageSize + ",'" + funcParam + "');\" onclick=\"this.select();\"/> / ");
        sb.append("<input type=\"text\" value=\"" + pageSize
                + "\" onkeypress=\"var e=window.event||this;var c=e.keyCode||e.which;if(c==13)");
        sb.append(funcName + "(" + pageNo + ",this.value,'" + funcParam
                + "');\" onclick=\"this.select();\"/> ?");
        sb.append(" " + count + " ?" + (message != null ? message : "") + "</a></li>\n");

        sb.insert(0, "<ul>\n").append("</ul>\n");

        sb.append("<div style=\"clear:both;\"></div>");

        // sb.insert(0,"<div class=\"page\">\n").append("</div>\n");

        return sb.toString();
    }

    /**
     * ?HTML?
     * 
     * @return
     */
    public String getHtml() {
        return toString();
    }

    /**
     * ?
     * 
     * @return
     */
    public long getCount() {
        return count;
    }

    /**
     * ?
     * 
     * @param count
     */
    public void setCount(long count) {
        this.count = count;
        if (pageSize >= count) {
            pageNo = 1;
        }
    }

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

    /**
     * ??
     * 
     * @param pageNo
     */
    public void setPageNo(int pageNo) {
        this.pageNo = pageNo;
    }

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

    /**
     * ??500
     * 
     * @param pageSize
     */
    public void setPageSize(int pageSize) {
        this.pageSize = pageSize <= 0 ? 10 : pageSize;// > 500 ? 500 : pageSize;
    }

    /**
     * 
     * 
     * @return
     */
    public int getFirst() {
        return first;
    }

    /**
     * 
     * 
     * @return
     */
    public int getLast() {
        return last;
    }

    /**
     * ??
     * 
     * @return getLast();
     */
    public int getTotalPage() {
        return getLast();
    }

    /**
     * ?
     * 
     * @return
     */
    public boolean isFirstPage() {
        return firstPage;
    }

    /**
     * ??
     * 
     * @return
     */
    public boolean isLastPage() {
        return lastPage;
    }

    /**
     * 
     * 
     * @return
     */
    public int getPrev() {
        if (isFirstPage()) {
            return pageNo;
        } else {
            return pageNo - 1;
        }
    }

    /**
     * 
     * 
     * @return
     */
    public int getNext() {
        if (isLastPage()) {
            return pageNo;
        } else {
            return pageNo + 1;
        }
    }

    /**
     * ??
     * 
     * @return List<T>
     */
    public List<T> getList() {
        return list;
    }

    /**
     * ?
     * 
     * @param list
     */
    public Pager<T> setList(List<T> list) {
        this.list = list;
        initialize();
        return this;
    }

    public String getFuncName() {
        return funcName;
    }

    public void setFuncName(String funcName) {
        this.funcName = funcName;
    }

    public String getFuncParam() {
        return funcParam;
    }

    public void setFuncParam(String funcParam) {
        this.funcParam = funcParam;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public boolean isDisabled() {
        return this.pageSize == -1;
    }

    /**
     * ?
     * 
     * @return this.count==-1
     */
    public boolean isNotCount() {
        return this.count == -1;
    }

    public static void main(String[] args) {

        /*   Integer[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
             List<Integer> list = Arrays.asList(array);  
            
           Pager<Integer> page = new Pager<Integer>(1, 10, list.size());
           page.setList(list);
               
           System.out.println(page.toString());
            System.out.println(""+page.getFirst());
            System.out.println(""+page.getLast());
            System.out.println(""+page.getPrev());
            System.out.println(""+page.getNext());
               
            List<Integer> page1 = page.getPagedList(1);  
             System.out.println(page1);  
            
             List<Integer> page2 = page.getPagedList(2);  
             System.out.println(page2);  
            
             List<Integer> page3 = page.getPagedList(0);  
             System.out.println(page3);  
           */

    }
}