Skip to content
Snippets Groups Projects

Resolve "Use PageUtil class in librador"

4 files
+ 7
49
Compare changes
  • Side-by-side
  • Inline

Files

/*
* TAM
* Copyright (C) 2021 - Delft University of Technology
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package nl.tudelft.tam.controller.utility;
import java.util.List;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
public class PageUtil {
public static <T> Page<T> pageFromList(List<T> list, Pageable pageable) {
int total = list.size();
int startIndex = pageable.getPageNumber() * pageable.getPageSize();
startIndex = startIndex > total ? 0 : startIndex;
int endIndex = Math.min(startIndex + pageable.getPageSize(), total);
List<T> listOnPage = list.subList(startIndex, endIndex);
return new PageImpl<>(
listOnPage,
PageRequest.of(pageable.getPageNumber(), pageable.getPageSize()),
total);
}
}
Loading