-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoffsetBasedPageRequest.java
More file actions
69 lines (58 loc) · 1.66 KB
/
Copy pathoffsetBasedPageRequest.java
File metadata and controls
69 lines (58 loc) · 1.66 KB
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
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
public class OffsetBasedPageRequest implements Pageable
{
private final int limit;
private final int offset;
/**
* Creates a new {@link OffsetBasedPageRequest} with sort parameters applied.
*
* @param offset zero-based offset.
* @param limit the size of the elements to be returned.
*/
public OffsetBasedPageRequest(int offset, int limit) {
if (offset < 0) {
throw new IllegalArgumentException("Offset index must not be less than zero");
}
if (limit < 1) {
throw new IllegalArgumentException("Limit must not be less than one");
}
this.offset = offset;
this.limit = limit;
}
@Override
public int getPageNumber() {
return offset / limit;
}
@Override
public int getPageSize() {
return limit;
}
@Override
public int getOffset() {
return offset;
}
@Override
public Sort getSort() {
return null;
}
@Override
public Pageable next() {
return new OffsetBasedPageRequest(getPageSize(), (int)(getOffset() + getPageSize()));
}
public Pageable previous() {
return hasPrevious() ? new OffsetBasedPageRequest(getPageSize(), (int)(getOffset() - getPageSize())): this;
}
@Override
public Pageable previousOrFirst() {
return hasPrevious() ? previous() : first();
}
@Override
public Pageable first() {
return new OffsetBasedPageRequest(getPageSize(), 0);
}
@Override
public boolean hasPrevious() {
return offset > limit;
}
}