View Javadoc
1   package com.foxinmy.weixin4j.model.paging;
2   
3   import java.io.Serializable;
4   
5   import com.foxinmy.weixin4j.model.paging.Sort.Direction;
6   
7   /**
8    * 分页数据(页码从1开始
9    * 
10   * @className Pageable
11   * @author jinyu(foxinmy@gmail.com)
12   * @date 2014年12月27日
13   * @since JDK 1.6
14   */
15  public class Pageable implements Serializable {
16  
17  	private static final long serialVersionUID = -8051554878205756307L;
18  
19  	private final int page;
20  	private final int size;
21  	private Sort sort;
22  
23  	/**
24  	 * 
25  	 * @param page
26  	 *            must not be less than one.
27  	 * @param size
28  	 *            must not be less than one.
29  	 */
30  	public Pageable(int page, int size) {
31  		if (page < 1) {
32  			throw new IllegalArgumentException(
33  					"Page index must not be less than one!");
34  		}
35  		if (size < 1) {
36  			throw new IllegalArgumentException(
37  					"Page size must not be less than one!");
38  		}
39  		this.page = page;
40  		this.size = size;
41  	}
42  
43  	public Pageable(int page, int size, Direction direction,
44  			String... properties) {
45  		this(page, size, new Sort(direction, properties));
46  	}
47  
48  	public Pageable(int page, int size, Sort sort) {
49  		this.page = page;
50  		this.size = size;
51  		this.sort = sort;
52  	}
53  
54  	/**
55  	 * page=1,size=20
56  	 * 
57  	 * @return
58  	 */
59  	public static Pageable get() {
60  		return new Pageable(1, 20);
61  	}
62  
63  	public int getPageSize() {
64  		return size;
65  	}
66  
67  	public int getPageNumber() {
68  		return page;
69  	}
70  
71  	public Sort getSort() {
72  		return sort;
73  	}
74  
75  	public void setSort(Sort sort) {
76  		this.sort = sort;
77  	}
78  
79  	public int getOffset() {
80  		return (page - 1) * size;
81  	}
82  
83  	public boolean hasPrevious() {
84  		return page > 1;
85  	}
86  
87  	public Pageable next() {
88  		return new Pageable(getPageNumber() + 1, getPageSize(), getSort());
89  	}
90  
91  	public Pageable previous() {
92  		return getPageNumber() == 1 ? this : new Pageable(getPageNumber() - 1,
93  				getPageSize(), getSort());
94  	}
95  
96  	public Pageable first() {
97  		return new Pageable(1, getPageSize(), getSort());
98  	}
99  
100 	@Override
101 	public String toString() {
102 		return "Pageable [page=" + page + ", size=" + size + ", sort=" + sort
103 				+ "]";
104 	}
105 }