/* * Copyright 2011-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package pl.com.bottega.cqrs.query; import java.io.Serializable; import java.util.Collections; import java.util.List; @SuppressWarnings("serial") public class PaginatedResult<T> implements Serializable { private final List<T> items; private final int pageSize; private final int pageNumber; private final int pagesCount; private final int totalItemsCount; public PaginatedResult(int pageNumber, int pageSize) { this.pageNumber = pageNumber; this.pageSize = pageSize; items = Collections.emptyList(); pagesCount = 0; totalItemsCount = 0; } public PaginatedResult(List<T> items, int pageNumber, int pageSize, int totalItemsCount) { this.items = items; this.pageNumber = pageNumber; this.pageSize = pageSize; this.pagesCount = countPages(pageSize, totalItemsCount); this.totalItemsCount = totalItemsCount; } private int countPages(int size, int itemsCount) { return (int) Math.ceil((double) itemsCount / size); } public List<T> getItems() { return items; } public int getPageSize() { return pageSize; } public int getPageNumber() { return pageNumber; } public int getPagesCount() { return pagesCount; } public int getTotalItemsCount() { return totalItemsCount; } }