All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.tkit.quarkus.jpa.daos.PageResult Maven / Gradle / Ivy

package org.tkit.quarkus.jpa.daos;

import java.util.stream.Stream;

/**
 * The page query result
 *
 * @param  the page results type
 */
public class PageResult {

    /**
     * Creates empty page result.
     *
     * @return empty page result.
     * @param  the page results type.
     */
    public static  PageResult empty() {
        return new PageResult<>(0, Stream.empty(), Page.of(0, 1));
    }

    /**
     * The total elements in the database.
     */
    private long totalElements;

    /**
     * The page number.
     */
    private long number;

    /**
     * The page size.
     */
    private long size;

    /**
     * The number of pages.
     */
    private long totalPages;

    /**
     * The data stream.
     */
    private Stream stream;

    /**
     * The default constructor.
     *
     * @param totalElements the count of all items.
     * @param stream the data stream.
     * @param page the page.
     */
    public PageResult(long totalElements, Stream stream, Page page) {
        this(totalElements, stream, page.number(), page.size());
    }

    /**
     * The default constructor.
     *
     * @param totalElements the count of all items.
     * @param stream the data stream.
     * @param number number of the page.
     * @param size size of the page.
     */
    public PageResult(long totalElements, Stream stream, long number, long size) {
        this.totalElements = totalElements;
        this.stream = stream;
        this.number = number;
        this.size = size;
        this.totalPages = (totalElements + size - 1) / size;
    }

    /**
     * Gets the number of the pages.
     *
     * @return the number of the pages.
     */
    public long getTotalPages() {
        return totalPages;
    }

    /**
     * Gets the size of the page.
     *
     * @return the size of the page.
     */
    public long getSize() {
        return size;
    }

    /**
     * Returns {@code true} if the PageResult is empty.
     *
     * @return {@code true} for empty pare result, {@code totalElements == 0}.
     */
    public boolean isEmpty() {
        return totalElements <= 0;
    }

    /**
     * Gets the index of the page.
     *
     * @return the index of the page.
     */
    public long getNumber() {
        return number;
    }

    /**
     * Gets the count of all items in the database.
     *
     * @return the count of all items in the database.
     */
    public long getTotalElements() {
        return totalElements;
    }

    /**
     * The stream of the data.
     *
     * @return the stream of the data.
     */
    public Stream getStream() {
        return stream;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String toString() {
        return "PageResult{" +
                "c=" + totalElements +
                ",n=" + number +
                ",s=" + size +
                '}';
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy