diff options
Diffstat (limited to 'spark-common/src/main/java')
5 files changed, 3 insertions, 739 deletions
diff --git a/spark-common/src/main/java/me/lucko/spark/common/command/modules/ActivityLogModule.java b/spark-common/src/main/java/me/lucko/spark/common/command/modules/ActivityLogModule.java index 9b611fb..ae4613a 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/command/modules/ActivityLogModule.java +++ b/spark-common/src/main/java/me/lucko/spark/common/command/modules/ActivityLogModule.java @@ -24,12 +24,12 @@ import me.lucko.spark.common.activitylog.ActivityLog.Activity; import me.lucko.spark.common.command.Command; import me.lucko.spark.common.command.CommandModule; import me.lucko.spark.common.command.tabcomplete.TabCompleter; -import me.lucko.spark.common.util.pagination.Pagination; -import me.lucko.spark.common.util.pagination.Pagination.Renderer; -import me.lucko.spark.common.util.pagination.Pagination.Renderer.RowRenderer; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.TextComponent; import net.kyori.adventure.text.event.ClickEvent; +import net.kyori.adventure.text.feature.pagination.Pagination; +import net.kyori.adventure.text.feature.pagination.Pagination.Renderer; +import net.kyori.adventure.text.feature.pagination.Pagination.Renderer.RowRenderer; import java.util.ArrayList; import java.util.Collection; diff --git a/spark-common/src/main/java/me/lucko/spark/common/util/pagination/Pagination.java b/spark-common/src/main/java/me/lucko/spark/common/util/pagination/Pagination.java deleted file mode 100644 index a92926f..0000000 --- a/spark-common/src/main/java/me/lucko/spark/common/util/pagination/Pagination.java +++ /dev/null @@ -1,320 +0,0 @@ -/* - * This file is part of text, licensed under the MIT License. - * - * Copyright (c) 2017-2020 KyoriPowered - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -package me.lucko.spark.common.util.pagination; - -import net.kyori.adventure.text.Component; -import net.kyori.adventure.text.event.ClickEvent; -import net.kyori.adventure.text.event.HoverEvent; -import net.kyori.adventure.text.format.NamedTextColor; -import net.kyori.adventure.text.format.Style; - -import java.util.Collection; -import java.util.List; -import java.util.function.Consumer; - -/** - * Pagination. - * - * @param <T> the content type - */ -@FunctionalInterface -public interface Pagination<T> { - /** - * The default interface width. - */ - int WIDTH = 55; - /** - * The default number of results per page. - */ - int RESULTS_PER_PAGE = 6; - /** - * The default line character. - */ - char LINE_CHARACTER = '-'; - /** - * The default line style. - */ - Style LINE_STYLE = Style.style(NamedTextColor.DARK_GRAY); - /** - * The default character for the previous page button. - */ - char PREVIOUS_PAGE_BUTTON_CHARACTER = '\u00AB'; // « - /** - * The default style for the next page button. - */ - Style PREVIOUS_PAGE_BUTTON_STYLE = Style.style() - .color(NamedTextColor.RED) - .hoverEvent(HoverEvent.showText(Component.text("Previous Page", NamedTextColor.RED))) - .build(); - /** - * The default character for the next page button. - */ - char NEXT_PAGE_BUTTON_CHARACTER = '\u00BB'; // » - /** - * The default style for the next page button. - */ - Style NEXT_PAGE_BUTTON_STYLE = Style.style() - .color(NamedTextColor.GREEN) - .hoverEvent(HoverEvent.showText(Component.text("Next Page", NamedTextColor.GREEN))) - .build(); - - /** - * The default interface renderer. - */ - Renderer DEFAULT_RENDERER = new Renderer() { - @Override - public String toString() { - return "Pagination.DEFAULT_RENDERER"; - } - }; - - /** - * Creates a pagination builder. - * - * @return a builder - */ - static Builder builder() { - return new PaginationBuilder(); - } - - /** - * Renders. - * - * @param content the content to render - * @param page the page number - * @return the rendered results - */ - List<Component> render(final Collection<? extends T> content, final int page); - - /** - * A pagination renderer. - */ - interface Renderer { - Component GRAY_LEFT_ROUND_BRACKET = Component.text("(", NamedTextColor.GRAY); - Component GRAY_LEFT_SQUARE_BRACKET = Component.text("[", NamedTextColor.GRAY); - Component GRAY_RIGHT_ROUND_BRACKET = Component.text(")", NamedTextColor.GRAY); - Component GRAY_RIGHT_SQUARE_BRACKET = Component.text("]", NamedTextColor.GRAY); - Component GRAY_FORWARD_SLASH = Component.text("/", NamedTextColor.GRAY); - - /** - * Renders an empty result. - * - * <p>No header or footer are rendered.</p> - * - * @return the rendered component - */ - default Component renderEmpty() { - return Component.text("No results match.", NamedTextColor.GRAY); - } - - /** - * Renders an unknown page. - * - * <p>No header or footer are rendered.</p> - * - * @param page the unknown page - * @param pages the total number of pages - * @return the rendered component - */ - default Component renderUnknownPage(final int page, final int pages) { - return Component.text("Unknown page selected. " + pages + " total pages.", NamedTextColor.GRAY); - } - - /** - * Renders a header. - * - * @param title the title - * @param page the page - * @param pages the total number of pages - * @return the rendered component - */ - default Component renderHeader(final Component title, final int page, final int pages) { - return Component.text() - .append(Component.space()) - .append(title) - .append(Component.space()) - .append(GRAY_LEFT_ROUND_BRACKET) - .append(Component.text(page, NamedTextColor.WHITE)) - .append(GRAY_FORWARD_SLASH) - .append(Component.text(pages, NamedTextColor.WHITE)) - .append(GRAY_RIGHT_ROUND_BRACKET) - .append(Component.space()) - .build(); - } - - /** - * Renders a previous page button. - * - * @param character the button character - * @param style the button style - * @param clickEvent the click event for the button - * @return the rendered component - */ - default Component renderPreviousPageButton(final char character, final Style style, final ClickEvent clickEvent) { - return Component.text() - .append(Component.space()) - .append(GRAY_LEFT_SQUARE_BRACKET) - .append(Component.text(character, style.clickEvent(clickEvent))) - .append(GRAY_RIGHT_SQUARE_BRACKET) - .append(Component.space()) - .build(); - } - - /** - * Renders a next page button. - * - * @param character the button character - * @param style the button style - * @param clickEvent the click event for the button - * @return the rendered component - */ - default Component renderNextPageButton(final char character, final Style style, final ClickEvent clickEvent) { - return Component.text() - .append(Component.space()) - .append(GRAY_LEFT_SQUARE_BRACKET) - .append(Component.text(character, style.clickEvent(clickEvent))) - .append(GRAY_RIGHT_SQUARE_BRACKET) - .append(Component.space()) - .build(); - } - - /** - * A row renderer. - * - * @param <T> the content type - */ - @FunctionalInterface - interface RowRenderer<T> { - /** - * Renders a row. - * - * @param value the value - * @param index the index - * @return the rendered row - */ - Collection<Component> renderRow(final T value, final int index); - } - } - - /** - * A page command function. - */ - @FunctionalInterface - interface PageCommandFunction { - /** - * Gets the command to display the page. - * - * @param page the page - * @return the command - */ - String pageCommand(final int page); - } - - /** - * A pagination builder. - */ - interface Builder { - /** - * Sets the width. - * - * @param width the width - * @return this builder - */ - Builder width(final int width); - - /** - * Sets the number of results per page. - * - * @param resultsPerPage the number of results per page - * @return this builder - */ - Builder resultsPerPage(final int resultsPerPage); - - /** - * Sets the renderer. - * - * @param renderer the renderer - * @return this builder - */ - Builder renderer(final Renderer renderer); - - /** - * Sets the line character and style. - * - * @param line the line consumer - * @return this builder - */ - Builder line(final Consumer<CharacterAndStyle> line); - - /** - * Sets the previous button. - * - * @param previousButton the button consumer - * @return this builder - */ - Builder previousButton(final Consumer<CharacterAndStyle> previousButton); - - /** - * Sets the next button. - * - * @param nextButton the button consumer - * @return this builder - */ - Builder nextButton(final Consumer<CharacterAndStyle> nextButton); - - /** - * Builds. - * - * @param title the title - * @param rowRenderer the row renderer - * @param pageCommand the page command - * @param <T> the content type - * @return pagination - * @throws IllegalStateException if the title has not been set - * @throws IllegalStateException if the row renderer has not been set - */ - <T> Pagination<T> build(final Component title, final Renderer.RowRenderer<T> rowRenderer, final PageCommandFunction pageCommand); - - /** - * A builder for a character and style pair. - */ - interface CharacterAndStyle { - /** - * Sets the character. - * - * @param character the character - * @return this builder - */ - CharacterAndStyle character(final char character); - - /** - * Sets the style. - * - * @param style the style - * @return this builder - */ - CharacterAndStyle style(final Style style); - } - } -}
\ No newline at end of file diff --git a/spark-common/src/main/java/me/lucko/spark/common/util/pagination/PaginationBuilder.java b/spark-common/src/main/java/me/lucko/spark/common/util/pagination/PaginationBuilder.java deleted file mode 100644 index 807b7b9..0000000 --- a/spark-common/src/main/java/me/lucko/spark/common/util/pagination/PaginationBuilder.java +++ /dev/null @@ -1,134 +0,0 @@ -/* - * This file is part of text, licensed under the MIT License. - * - * Copyright (c) 2017-2020 KyoriPowered - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -package me.lucko.spark.common.util.pagination; - -import net.kyori.adventure.text.Component; -import net.kyori.adventure.text.format.Style; - -import java.util.function.Consumer; - -final class PaginationBuilder implements Pagination.Builder { - private int width = Pagination.WIDTH; - private int resultsPerPage = Pagination.RESULTS_PER_PAGE; - - private char lineCharacter = Pagination.LINE_CHARACTER; - private Style lineStyle = Pagination.LINE_STYLE; - - private Pagination.Renderer renderer = Pagination.DEFAULT_RENDERER; - - private char previousPageButtonCharacter = Pagination.PREVIOUS_PAGE_BUTTON_CHARACTER; - private Style previousPageButtonStyle = Pagination.PREVIOUS_PAGE_BUTTON_STYLE; - private char nextPageButtonCharacter = Pagination.NEXT_PAGE_BUTTON_CHARACTER; - private Style nextPageButtonStyle = Pagination.NEXT_PAGE_BUTTON_STYLE; - - @Override - public Pagination.Builder width(final int width) { - this.width = width; - return this; - } - - @Override - public Pagination.Builder resultsPerPage(final int resultsPerPage) { - this.resultsPerPage = resultsPerPage; - return this; - } - - @Override - public Pagination.Builder renderer(final Pagination.Renderer renderer) { - this.renderer = renderer; - return this; - } - - @Override - public Pagination.Builder line(final Consumer<CharacterAndStyle> line) { - line.accept(new CharacterAndStyle() { - @Override - public CharacterAndStyle character(final char character) { - PaginationBuilder.this.lineCharacter = character; - return this; - } - - @Override - public CharacterAndStyle style(final Style style) { - PaginationBuilder.this.lineStyle = style; - return this; - } - }); - return this; - } - - @Override - public Pagination.Builder previousButton(final Consumer<CharacterAndStyle> previousButton) { - previousButton.accept(new CharacterAndStyle() { - @Override - public CharacterAndStyle character(final char character) { - PaginationBuilder.this.previousPageButtonCharacter = character; - return this; - } - - @Override - public CharacterAndStyle style(final Style style) { - PaginationBuilder.this.previousPageButtonStyle = style; - return this; - } - }); - return this; - } - - @Override - public Pagination.Builder nextButton(final Consumer<CharacterAndStyle> nextButton) { - nextButton.accept(new CharacterAndStyle() { - @Override - public CharacterAndStyle character(final char character) { - PaginationBuilder.this.nextPageButtonCharacter = character; - return this; - } - - @Override - public CharacterAndStyle style(final Style style) { - PaginationBuilder.this.nextPageButtonStyle = style; - return this; - } - }); - return this; - } - - @Override - public <T> Pagination<T> build(final Component title, final Pagination.Renderer.RowRenderer<T> rowRenderer, final Pagination.PageCommandFunction pageCommand) { - return new PaginationImpl<>( - this.width, - this.resultsPerPage, - this.renderer, - this.lineCharacter, - this.lineStyle, - this.previousPageButtonCharacter, - this.previousPageButtonStyle, - this.nextPageButtonCharacter, - this.nextPageButtonStyle, - title, - rowRenderer, - pageCommand - ); - } -}
\ No newline at end of file diff --git a/spark-common/src/main/java/me/lucko/spark/common/util/pagination/PaginationImpl.java b/spark-common/src/main/java/me/lucko/spark/common/util/pagination/PaginationImpl.java deleted file mode 100644 index 4a3bde0..0000000 --- a/spark-common/src/main/java/me/lucko/spark/common/util/pagination/PaginationImpl.java +++ /dev/null @@ -1,226 +0,0 @@ -/* - * This file is part of text, licensed under the MIT License. - * - * Copyright (c) 2017-2020 KyoriPowered - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -package me.lucko.spark.common.util.pagination; - -import net.kyori.adventure.text.Component; -import net.kyori.adventure.text.TextComponent; -import net.kyori.adventure.text.event.ClickEvent; -import net.kyori.adventure.text.format.Style; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.List; - -final class PaginationImpl<T> implements Pagination<T> { - private static final int LINE_CHARACTER_LENGTH = 1; - - private final int width; - private final int resultsPerPage; - - private final Renderer renderer; - - private final char lineCharacter; - private final Style lineStyle; - - private final char previousPageButtonCharacter; - private final Style previousPageButtonStyle; - private final char nextPageButtonCharacter; - private final Style nextPageButtonStyle; - - private final Component title; - private final Renderer.RowRenderer<T> rowRenderer; - private final PageCommandFunction pageCommand; - - PaginationImpl(final int width, final int resultsPerPage, final Renderer renderer, final char lineCharacter, final Style lineStyle, final char previousPageButtonCharacter, final Style previousPageButtonStyle, final char nextPageButtonCharacter, final Style nextPageButtonStyle, final Component title, final Renderer.RowRenderer<T> rowRenderer, final PageCommandFunction pageCommand) { - this.width = width; - this.resultsPerPage = resultsPerPage; - this.renderer = renderer; - this.lineCharacter = lineCharacter; - this.lineStyle = lineStyle; - this.previousPageButtonCharacter = previousPageButtonCharacter; - this.previousPageButtonStyle = previousPageButtonStyle; - this.nextPageButtonCharacter = nextPageButtonCharacter; - this.nextPageButtonStyle = nextPageButtonStyle; - this.title = title; - this.rowRenderer = rowRenderer; - this.pageCommand = pageCommand; - } - - @Override - public List<Component> render(final Collection<? extends T> content, final int page) { - if(content.isEmpty()) { - return Collections.singletonList(this.renderer.renderEmpty()); - } - - final int pages = pages(this.resultsPerPage, content.size()); - - if(!pageInRange(page, pages)) { - return Collections.singletonList(this.renderer.renderUnknownPage(page, pages)); - } - - final List<Component> components = new ArrayList<>(); - components.add(this.renderHeader(page, pages)); - Paginator.forEachPageEntry(content, this.resultsPerPage, page, (value, index) -> { - components.addAll(this.rowRenderer.renderRow(value, index)); - }); - components.add(this.renderFooter(page, pages)); - return Collections.unmodifiableList(components); - } - - private Component renderHeader(final int page, final int pages) { - final Component header = this.renderer.renderHeader(this.title, page, pages); - final Component dashes = this.line(header); - - return Component.text() - .append(dashes) - .append(header) - .append(dashes) - .build(); - } - - private Component renderFooter(final int page, final int pages) { - if(page == 1 && page == pages) { - return this.line(this.width); - } - - final Component buttons = this.renderFooterButtons(page, pages); - final Component dashes = this.line(buttons); - - return Component.text() - .append(dashes) - .append(buttons) - .append(dashes) - .build(); - } - - private Component renderFooterButtons(final int page, final int pages) { - final boolean hasPreviousPage = page > 1 && pages > 1; - final boolean hasNextPage = (page < pages && page == 1) || ((hasPreviousPage && page > 1) && page != pages); - - final TextComponent.Builder buttons = Component.text(); - if(hasPreviousPage) { - buttons.append(this.renderer.renderPreviousPageButton(this.previousPageButtonCharacter, this.previousPageButtonStyle, ClickEvent.runCommand(this.pageCommand.pageCommand(page - 1)))); - - if(hasNextPage) { - buttons.append(this.line(8)); - } - } - - if(hasNextPage) { - buttons.append(this.renderer.renderNextPageButton(this.nextPageButtonCharacter, this.nextPageButtonStyle, ClickEvent.runCommand(this.pageCommand.pageCommand(page + 1)))); - } - - return buttons.build(); - } - - private Component line(final Component component) { - return this.line((this.width - length(component)) / (LINE_CHARACTER_LENGTH * 2)); - } - - private Component line(final int characters) { - return Component.text(repeat(String.valueOf(this.lineCharacter), characters), this.lineStyle); - } - - static int length(final Component component) { - int length = 0; - if(component instanceof TextComponent) { - length += ((TextComponent) component).content().length(); - } - for(final Component child : component.children()) { - length += length(child); - } - return length; - } - - static String repeat(final String character, final int count) { - return String.join("", Collections.nCopies(count, character)); - } - - static int pages(final int pageSize, final int count) { - final int pages = count / pageSize + 1; - if(count % pageSize == 0) { - return pages - 1; - } - return pages; - } - - static boolean pageInRange(final int page, final int pages) { - return page > 0 && page <= pages; - } - - @Override - public String toString() { - return "PaginationImpl{" + - "width=" + this.width + - ", resultsPerPage=" + this.resultsPerPage + - ", renderer=" + this.renderer + - ", lineCharacter=" + this.lineCharacter + - ", lineStyle=" + this.lineStyle + - ", previousPageButtonCharacter=" + this.previousPageButtonCharacter + - ", previousPageButtonStyle=" + this.previousPageButtonStyle + - ", nextPageButtonCharacter=" + this.nextPageButtonCharacter + - ", nextPageButtonStyle=" + this.nextPageButtonStyle + - ", title=" + this.title + - ", rowRenderer=" + this.rowRenderer + - ", pageCommand=" + this.pageCommand + - '}'; - } - - @Override - public boolean equals(final Object other) { - if(this == other) return true; - if(other == null || this.getClass() != other.getClass()) return false; - final PaginationImpl<?> that = (PaginationImpl<?>) other; - if(this.width != that.width) return false; - if(this.resultsPerPage != that.resultsPerPage) return false; - if(this.lineCharacter != that.lineCharacter) return false; - if(this.previousPageButtonCharacter != that.previousPageButtonCharacter) return false; - if(this.nextPageButtonCharacter != that.nextPageButtonCharacter) return false; - if(!this.renderer.equals(that.renderer)) return false; - if(!this.lineStyle.equals(that.lineStyle)) return false; - if(!this.previousPageButtonStyle.equals(that.previousPageButtonStyle)) return false; - if(!this.nextPageButtonStyle.equals(that.nextPageButtonStyle)) return false; - if(!this.title.equals(that.title)) return false; - if(!this.rowRenderer.equals(that.rowRenderer)) return false; - return this.pageCommand.equals(that.pageCommand); - } - - @Override - public int hashCode() { - int result = this.width; - result = (31 * result) + this.resultsPerPage; - result = (31 * result) + this.renderer.hashCode(); - result = (31 * result) + (int) this.lineCharacter; - result = (31 * result) + this.lineStyle.hashCode(); - result = (31 * result) + (int) this.previousPageButtonCharacter; - result = (31 * result) + this.previousPageButtonStyle.hashCode(); - result = (31 * result) + (int) this.nextPageButtonCharacter; - result = (31 * result) + this.nextPageButtonStyle.hashCode(); - result = (31 * result) + this.title.hashCode(); - result = (31 * result) + this.rowRenderer.hashCode(); - result = (31 * result) + this.pageCommand.hashCode(); - return result; - } -}
\ No newline at end of file diff --git a/spark-common/src/main/java/me/lucko/spark/common/util/pagination/Paginator.java b/spark-common/src/main/java/me/lucko/spark/common/util/pagination/Paginator.java deleted file mode 100644 index 83b08bf..0000000 --- a/spark-common/src/main/java/me/lucko/spark/common/util/pagination/Paginator.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * This file is part of text, licensed under the MIT License. - * - * Copyright (c) 2017-2020 KyoriPowered - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -package me.lucko.spark.common.util.pagination; - -import java.util.Collection; -import java.util.Iterator; -import java.util.List; -import java.util.RandomAccess; -import java.util.function.ObjIntConsumer; - -interface Paginator { - @SuppressWarnings("unchecked") - static <T> void forEachPageEntry(final Collection<? extends T> content, final int pageSize, final int page, final ObjIntConsumer<? super T> consumer) { - final int size = content.size(); - final int start = pageSize * (page - 1); - final int end = pageSize * page; - if(content instanceof List<?> && content instanceof RandomAccess) { - final List<? extends T> list = (List<? extends T>) content; - for(int i = start; i < end && i < size; i++) { - consumer.accept(list.get(i), i); - } - } else { - final Iterator<? extends T> it = content.iterator(); - - // skip entries on previous pages - for(int i = 0; i < start; i++) { - it.next(); - } - - for(int i = start; i < end && i < size; i++) { - consumer.accept(it.next(), i); - } - } - } -}
\ No newline at end of file |