-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
77 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/main/java/com/muhammaddaffa/mdlib/gui/pagination/Pagination.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.muhammaddaffa.mdlib.gui.pagination; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class Pagination { | ||
|
||
private final Map<Integer, List<?>> pageItems = new HashMap<>(); | ||
|
||
public int totalPage = 1; | ||
public int currentPage = 1; | ||
|
||
public Pagination(List<?> items, int maxItemsPerPage) { | ||
if (items == null || maxItemsPerPage <= 0) { | ||
throw new IllegalArgumentException("Invalid input list or maxItemsPerPage"); | ||
} | ||
|
||
// If the items is empty | ||
if (items.isEmpty()) { | ||
pageItems.put(1, new ArrayList<>()); | ||
return; | ||
} | ||
|
||
int totalItems = items.size(); | ||
totalPage = (int) Math.ceil((double) totalItems / maxItemsPerPage); | ||
|
||
for (int i = 0; i < totalPage; i++) { | ||
int start = i * maxItemsPerPage; | ||
int end = Math.min(start + maxItemsPerPage, totalItems); | ||
List<?> pageContent = items.subList(start, end); | ||
pageItems.put(i + 1, new ArrayList<>(pageContent)); | ||
} | ||
} | ||
|
||
public boolean nextPage() { | ||
return this.pageItems.get(this.currentPage + 1) != null; | ||
} | ||
|
||
public boolean previousPage() { | ||
return this.pageItems.get(this.currentPage - 1) != null; | ||
} | ||
|
||
public List<?> getItems() { | ||
return this.pageItems.get(this.currentPage); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters