-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Page.java
120 lines (109 loc) · 3.34 KB
/
Page.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package io.quarkus.panache.common;
/**
* <p>
* Utility class to represent paging information. Page instances are immutable.
* </p>
*
* <p>
* Usage:
* </p>
*
* <code><pre>
* Page page = Page.ofSize(25);
* Page secondPage = page.next();
* </pre></code>
*
* @author Stéphane Épardaud
*/
public class Page {
/**
* The current page index (0-based).
*/
public final int index;
/**
* The current page size;
*/
public final int size;
/**
* Builds a page of the given size.
*
* @param size the page size
* @throws IllegalArgumentException if the page size is less than or equal to 0
* @see #ofSize(int)
*/
public Page(int size) {
this(0, size);
}
/**
* Builds a page of the given index and size.
*
* @param index the page index (0-based)
* @param size the page size
* @throws IllegalArgumentException if the page index is less than 0
* @throws IllegalArgumentException if the page size is less than or equal to 0
* @see #of(int, int)
*/
public Page(int index, int size) {
if (index < 0)
throw new IllegalArgumentException("Page index must be >= 0 : " + index);
if (size <= 0)
throw new IllegalArgumentException("Page size must be > 0 : " + size);
this.index = index;
this.size = size;
}
/**
* Builds a page of the given index and size.
*
* @param index the page index (0-based)
* @param size the page size
* @throws IllegalArgumentException if the page index is less than 0
* @throws IllegalArgumentException if the page size is less than or equal to 0
*/
public static Page of(int index, int size) {
return new Page(index, size);
}
/**
* Builds a page of the given size.
*
* @param size the page size
* @throws IllegalArgumentException if the page size is less than or equal to 0
*/
public static Page ofSize(int size) {
return new Page(size);
}
/**
* Returns a new page with the next page index and the same size.
*
* @return a new page with the next page index and the same size.
* @see #previous()
*/
public Page next() {
return new Page(index + 1, size);
}
/**
* Returns a new page with the previous page index and the same size, or this page if it is the first page.
*
* @return a new page with the next page index and the same size, or this page if it is the first page.
* @see #next()
*/
public Page previous() {
return index > 0 ? new Page(index - 1, size) : this;
}
/**
* Returns a new page with the first page index (0) and the same size, or this page if it is the first page.
*
* @return a new page with the first page index (0) and the same size, or this page if it is the first page.
*/
public Page first() {
return index > 0 ? new Page(0, size) : this;
}
/**
* Returns a new page at the given page index and the same size, or this page if the page index is the same.
*
* @param newIndex the new page index
* @return a new page at the given page index and the same size, or this page if the page index is the same.
*/
public Page index(int newIndex) {
return newIndex != index ? new Page(newIndex, size) : this;
}
}