org.cp.elements.util.paging.Pageable Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cp-elements Show documentation
Show all versions of cp-elements Show documentation
Java Simplified. Extensions and Useful Constructs for the Java Platform.
Codeprimate Elements (a.k.a. cp-elements) is a Java library and micro-framework used to simplify
the development of software applications written in Java. Elements packages several APIs into one
library in order to address various application concerns and aspects of software design and development
collectively and conveniently. Elements is a highly simple, yet robust and proven library built on
solid OO principles, software design patterns and best practices to effectively solve common
and reoccurring problems in software development.
/*
* Copyright (c) 2011-Present. Codeprimate, LLC and authors. All Rights Reserved.
*
* This software is licensed under the Codeprimate End User License Agreement (EULA).
* This software is proprietary and confidential in addition to an intellectual asset
* of the aforementioned authors.
*
* By using the software, the end-user implicitly consents to and agrees to be in compliance
* with all terms and conditions of the EULA. Failure to comply with the EULA will result in
* the maximum penalties permissible by law.
*
* In short, this software may not be reverse engineered, reproduced, copied, modified
* or distributed without prior authorization of the aforementioned authors, permissible
* and expressed only in writing. The authors grant the end-user non-exclusive, non-negotiable
* and non-transferable use of the software "as is" without expressed or implied WARRANTIES,
* EXTENSIONS or CONDITIONS of any kind.
*
* For further information on the software license, the end user is encouraged to read
* the EULA @ ...
*/
package org.cp.elements.util.paging;
import static java.util.stream.StreamSupport.stream;
import static org.cp.elements.lang.ElementsExceptionsFactory.newPageNotFoundException;
import java.util.Comparator;
import org.cp.elements.lang.Assert;
/**
* The {@link Pageable} interface defines a contract for implementing objects that support paging
* over the contents of the object, such as a {@link Iterable}.
*
* @author John J. Blum
* @param {@link Class type} of the elements or items contained in the {@link Page pages}
* of this {@link Pageable} object.
* @see java.lang.Iterable
* @see org.cp.elements.util.paging.Page
* @since 1.0.0
*/
@SuppressWarnings("unused")
public interface Pageable extends Iterable> {
/**
* Determines whether this {@link Pageable} object contains any {@link Page Pages}.
*
* @return a boolean value indicating whether this {@link Pageable} object contains any {@link Page Pages}.
* @see #count()
*/
default boolean isEmpty() {
return count() < 1;
}
/**
* Get the total number of {@link Page pages} in this {@link Pageable} object.
*
* @return an int value indicating the total number of {@link Page pages} in this {@link Pageable} object.
*/
default int count() {
return (int) stream(this.spliterator(), false).count();
}
/**
* Jump to the first {@link Page} in this {@link Pageable} collection of {@link Page Pages}.
*
* @return the first {@link Page}.
* @throws PageNotFoundException if no {@link Page Pages} are available.
* @see #getPage(int)
*/
default Page firstPage() {
try {
return getPage(1);
}
catch (Exception cause) {
throw newPageNotFoundException(cause, "No first page");
}
}
/**
* Gets the {@link Page} for the given page {@link Integer#TYPE number}, starting with page one.
*
* @param number integer value indicating the page number of the {@link Page} to return.
* @return the {@link Page} with {@link Integer#TYPE number}.
* @throws IllegalArgumentException if the {@link Integer#TYPE page number} is less than equal to 0.
* @throws PageNotFoundException if a {@link Page} with {@link Integer#TYPE number} is not found.
* @see org.cp.elements.util.paging.Page
*/
default Page getPage(int number) {
Assert.isTrue(number > 0, "Page number [%d] must be greater than 0", number);
int count = 0;
for (Page page : this) {
if (++count == number) {
return page;
}
}
throw newPageNotFoundException("Page with number [%d] not found", number);
}
/**
* Jump to the last {@link Page} in this {@link Pageable} collection of {@link Page Pages}.
*
* @return the last {@link Page}.
* @throws PageNotFoundException if no {@link Page Pages} are available.
* @see #getPage(int)
*/
default Page lastPage() {
try {
return getPage(count());
}
catch (Exception cause) {
throw newPageNotFoundException(cause, "No last page");
}
}
/**
* Sorts (orders) all the elements across all the {@link Page Pages} contained by this {@link Pageable}.
*
* @param orderBy {@link Comparator} used to sort (order) all the elements across all the {@link Page Pages}
* contained by this {@link Pageable}.
* @see java.util.Comparator
*/
void sort(Comparator orderBy);
}