001/* 002 * PlotSquared, a land and world management plugin for Minecraft. 003 * Copyright (C) IntellectualSites <https://intellectualsites.com> 004 * Copyright (C) IntellectualSites team and contributors 005 * 006 * This program is free software: you can redistribute it and/or modify 007 * it under the terms of the GNU General Public License as published by 008 * the Free Software Foundation, either version 3 of the License, or 009 * (at your option) any later version. 010 * 011 * This program is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 014 * GNU General Public License for more details. 015 * 016 * You should have received a copy of the GNU General Public License 017 * along with this program. If not, see <https://www.gnu.org/licenses/>. 018 */ 019package com.plotsquared.core.util.query; 020 021import com.google.common.base.Preconditions; 022import com.plotsquared.core.plot.Plot; 023import org.checkerframework.checker.nullness.qual.NonNull; 024 025import java.util.Collections; 026import java.util.List; 027 028/** 029 * Paginated collection of plots as a result of a {@link PlotQuery query} 030 */ 031public final class PaginatedPlotResult { 032 033 private final List<Plot> plots; 034 private final int pageSize; 035 036 PaginatedPlotResult(final @NonNull List<Plot> plots, final int pageSize) { 037 this.plots = plots; 038 this.pageSize = pageSize; 039 } 040 041 /** 042 * Get the plots belonging to a certain page. 043 * 044 * @param page Positive page number. Indexed from 1 045 * @return Plots that belong to the specified page 046 */ 047 public List<Plot> getPage(final int page) { 048 Preconditions.checkState(page >= 0, "Page must be positive"); 049 final int from = (page - 1) * this.pageSize; 050 if (this.plots.size() < from) { 051 return Collections.emptyList(); 052 } 053 final int to = Math.max(from + pageSize, this.plots.size()); 054 return this.plots.subList(from, to); 055 } 056 057 /** 058 * Get the number of available pages 059 * 060 * @return Available pages 061 */ 062 public int getPages() { 063 return (int) Math.ceil((double) plots.size() / (double) pageSize); 064 } 065 066}