-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPaginationHelper.java
More file actions
38 lines (31 loc) · 1.04 KB
/
PaginationHelper.java
File metadata and controls
38 lines (31 loc) · 1.04 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
import java.util.List;
public class PaginationHelper<I> {
List<I> collection;
int itemsPerPage;
public PaginationHelper(List<I> collection, int itemsPerPage) {
this.collection = collection;
this.itemsPerPage = itemsPerPage;
}
public int itemCount() {
return collection.size();
}
public int pageCount() {
int division = (int)(collection.size() / itemsPerPage);
int resto = collection.size() % itemsPerPage;
return resto == 0 ? division : division + 1;
}
public int pageItemCount(int pageIndex) {
int comienzoPagIndex = itemsPerPage * pageIndex;
int finalPageIndex = comienzoPagIndex + itemsPerPage;
if (comienzoPagIndex >= collection.size() || comienzoPagIndex < 0) {
return -1;
}else if (finalPageIndex >= collection.size()) {
return collection.size() - comienzoPagIndex;
} else {
return itemsPerPage;
}
}
public int pageIndex(int itemIndex) {
return itemIndex >= collection.size() || itemIndex < 0 ? -1 : (int)(itemIndex / itemsPerPage);
}
}