Skip to content
This repository was archived by the owner on Aug 12, 2025. It is now read-only.

Commit 526aab8

Browse files
committed
Add chat pagination management classes for improved user experience
1 parent 8f58b81 commit 526aab8

7 files changed

Lines changed: 321 additions & 37 deletions

File tree

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package gg.nextforge.pagination;
2+
3+
import gg.nextforge.pagination.model.Page;
4+
import lombok.Getter;
5+
6+
import java.util.ArrayList;
7+
import java.util.Collections;
8+
import java.util.List;
9+
10+
/**
11+
* A utility class for managing pagination of a list of items.
12+
* This class provides methods to divide a list of items into pages
13+
* and retrieve specific pages based on the index.
14+
*
15+
* @param <T> the type of items to be paginated
16+
*/
17+
public class Pagination<T> {
18+
19+
// The list of items to be paginated
20+
private final List<T> items = new ArrayList<>();
21+
// The number of items per page
22+
@Getter
23+
private final int itemsPerPage;
24+
25+
/**
26+
* Constructs a Pagination object with the specified number of items per page.
27+
*
28+
* @param itemsPerPage the number of items per page; must be greater than 0
29+
* @throws IllegalArgumentException if itemsPerPage is less than or equal to 0
30+
*/
31+
public Pagination(int itemsPerPage) {
32+
if (itemsPerPage <= 0) throw new IllegalArgumentException("itemsPerPage must be > 0");
33+
this.itemsPerPage = itemsPerPage;
34+
}
35+
36+
/**
37+
* Adds a single item to the pagination.
38+
*
39+
* @param item the item to add
40+
*/
41+
public void addItem(T item) {
42+
items.add(item);
43+
}
44+
45+
/**
46+
* Adds a list of items to the pagination.
47+
*
48+
* @param elements the list of items to add
49+
*/
50+
public void addAll(List<T> elements) {
51+
items.addAll(elements);
52+
}
53+
54+
/**
55+
* Retrieves a specific page of items based on the given index.
56+
*
57+
* @param index the index of the page to retrieve (0-based)
58+
* @return a {@link Page} object containing the items for the specified page
59+
* or an empty page if the index is out of bounds
60+
*/
61+
public Page<T> getPage(int index) {
62+
int totalPages = getTotalPages();
63+
if (index < 0 || index >= totalPages) return new Page<>(index, totalPages, Collections.emptyList());
64+
65+
int start = index * itemsPerPage;
66+
int end = Math.min(start + itemsPerPage, items.size());
67+
68+
List<T> sublist = items.subList(start, end);
69+
return new Page<>(index, totalPages, new ArrayList<>(sublist));
70+
}
71+
72+
/**
73+
* Calculates the total number of pages based on the number of items and items per page.
74+
*
75+
* @return the total number of pages
76+
*/
77+
public int getTotalPages() {
78+
return (int) Math.ceil((double) items.size() / itemsPerPage);
79+
}
80+
81+
/**
82+
* Returns the total number of items in the pagination.
83+
*
84+
* @return the total number of items
85+
*/
86+
public int getTotalItems() {
87+
return items.size();
88+
}
89+
90+
/**
91+
* Checks if the pagination contains no items.
92+
*
93+
* @return true if there are no items, false otherwise
94+
*/
95+
public boolean isEmpty() {
96+
return items.isEmpty();
97+
}
98+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package gg.nextforge.pagination.chat;
2+
3+
import gg.nextforge.pagination.session.PaginationSession;
4+
5+
import java.util.Map;
6+
import java.util.UUID;
7+
import java.util.concurrent.ConcurrentHashMap;
8+
9+
/**
10+
* Manages chat pagination sessions for players.
11+
* This class provides methods to set, get, remove, and check the existence of pagination sessions.
12+
*/
13+
public class ChatPaginatorManager {
14+
15+
// A thread-safe map to store pagination sessions keyed by player UUID.
16+
private static final Map<UUID, PaginationSession<String>> sessions = new ConcurrentHashMap<>();
17+
18+
/**
19+
* Sets a pagination session for a player.
20+
*
21+
* @param playerId The UUID of the player.
22+
* @param session The pagination session to set for the player.
23+
*/
24+
public static void setSession(UUID playerId, PaginationSession<String> session) {
25+
sessions.put(playerId, session);
26+
}
27+
28+
/**
29+
* Retrieves the pagination session for a player.
30+
*
31+
* @param playerId The UUID of the player.
32+
* @return The pagination session for the player, or null if no session exists.
33+
*/
34+
public static PaginationSession<String> getSession(UUID playerId) {
35+
return sessions.get(playerId);
36+
}
37+
38+
/**
39+
* Removes the pagination session for a player.
40+
*
41+
* @param playerId The UUID of the player.
42+
*/
43+
public static void removeSession(UUID playerId) {
44+
sessions.remove(playerId);
45+
}
46+
47+
/**
48+
* Checks if a pagination session exists for a player.
49+
*
50+
* @param playerId The UUID of the player.
51+
* @return true if a session exists for the player, false otherwise.
52+
*/
53+
public static boolean hasSession(UUID playerId) {
54+
return sessions.containsKey(playerId);
55+
}
56+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package gg.nextforge.pagination.chat;
2+
3+
import gg.nextforge.pagination.model.Page;
4+
import gg.nextforge.pagination.renderer.PageRenderer;
5+
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
9+
/**
10+
* Default implementation of {@link PageRenderer} for rendering chat pages.
11+
* @param <T> the type of items in the page
12+
*/
13+
public class DefaultChatPageRenderer<T> implements PageRenderer<T> {
14+
15+
// Header and footer templates for the chat page
16+
private final String header;
17+
private final String footer;
18+
19+
/**
20+
* Creates a new DefaultChatPageRenderer with the specified header and footer.
21+
* @param header the header template, which should contain placeholders for page index and total pages
22+
* @param footer the footer template
23+
*/
24+
public DefaultChatPageRenderer(String header, String footer) {
25+
this.header = header;
26+
this.footer = footer;
27+
}
28+
29+
/**
30+
* Renders the given page into a list of strings suitable for chat display.
31+
* @param page the page to render
32+
* @return a list of strings representing the rendered page
33+
*/
34+
@Override
35+
public List<String> render(Page<T> page) {
36+
List<String> lines = new ArrayList<>();
37+
lines.add(String.format(header, page.index() + 1, page.totalPages()));
38+
39+
for (T item : page.content()) {
40+
lines.add(item.toString());
41+
}
42+
43+
lines.add(footer);
44+
return lines;
45+
}
46+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package gg.nextforge.pagination.model;
2+
3+
import java.util.List;
4+
5+
/**
6+
* Represents a paginated result set.
7+
* @param index the current page index (0-based)
8+
* @param totalPages the total number of pages available
9+
* @param content the list of items on the current page
10+
* @param <T> the type of items in the page
11+
*/
12+
public record Page<T>(int index, int totalPages, List<T> content) {
13+
14+
/**
15+
* Checks if the page is empty.
16+
* @return true if the page has no content, false otherwise
17+
*/
18+
public boolean isEmpty() {
19+
return content.isEmpty();
20+
}
21+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package gg.nextforge.pagination.renderer;
2+
3+
import gg.nextforge.pagination.model.Page;
4+
5+
import java.util.List;
6+
7+
/**
8+
* Functional interface for rendering a page of items.
9+
* @param <T> the type of items in the page
10+
*/
11+
@FunctionalInterface
12+
public interface PageRenderer<T> {
13+
/**
14+
* Render a page of items.
15+
* @param page the page to render
16+
* @return a list of strings representing the rendered items
17+
*/
18+
List<String> render(Page<T> page);
19+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package gg.nextforge.pagination.session;
2+
3+
import gg.nextforge.pagination.Pagination;
4+
import gg.nextforge.pagination.model.Page;
5+
6+
/**
7+
* A session for managing pagination state.
8+
* This class allows navigating through pages of a {@link Pagination} object.
9+
*
10+
* @param <T> the type of items in the pagination
11+
*/
12+
public class PaginationSession<T> {
13+
14+
// The Pagination object that holds the items and pagination logic
15+
private final Pagination<T> pagination;
16+
// The current page index, starting from 0
17+
private int currentPage = 0;
18+
19+
/**
20+
* Constructs a PaginationSession with the given Pagination object.
21+
*
22+
* @param pagination the Pagination object to manage
23+
*/
24+
public PaginationSession(Pagination<T> pagination) {
25+
this.pagination = pagination;
26+
}
27+
28+
/**
29+
* Moves to the next page if available and returns it.
30+
*
31+
* @return the next {@link Page} of items
32+
*/
33+
public Page<T> next() {
34+
if (currentPage + 1 < pagination.getTotalPages()) currentPage++;
35+
return pagination.getPage(currentPage);
36+
}
37+
38+
/**
39+
* Moves to the previous page if available and returns it.
40+
*
41+
* @return the previous {@link Page} of items
42+
*/
43+
public Page<T> previous() {
44+
if (currentPage > 0) currentPage--;
45+
return pagination.getPage(currentPage);
46+
}
47+
48+
/**
49+
* Returns the current page without changing the state.
50+
*
51+
* @return the current {@link Page} of items
52+
*/
53+
public Page<T> current() {
54+
return pagination.getPage(currentPage);
55+
}
56+
57+
/**
58+
* Checks if there is a next page available.
59+
*
60+
* @return true if a next page exists, false otherwise
61+
*/
62+
public boolean hasNext() {
63+
return currentPage + 1 < pagination.getTotalPages();
64+
}
65+
66+
/**
67+
* Checks if there is a previous page available.
68+
*
69+
* @return true if a previous page exists, false otherwise
70+
*/
71+
public boolean hasPrevious() {
72+
return currentPage > 0;
73+
}
74+
75+
/**
76+
* Resets the session to the first page.
77+
*/
78+
public void reset() {
79+
currentPage = 0;
80+
}
81+
}

src/main/java/gg/nextforge/utility/ChatPagination.java

Lines changed: 0 additions & 37 deletions
This file was deleted.

0 commit comments

Comments
 (0)