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

net.nemerosa.ontrack.model.pagination.PageRequest.kt Maven / Gradle / Ivy

There is a newer version: 4.4.5
Show newest version
package net.nemerosa.ontrack.model.pagination

import java.lang.Integer.max
import java.lang.Integer.min

/**
 * Asking for a page in a collection.
 */
class PageRequest(
        val offset: Int = 0,
        val size: Int = DEFAULT_PAGE_SIZE
) {
    /**
     * Computes the previous page request.
     *
     * @param total Total number of elements in the list
     * @param pageSize Size for the previous page
     * @return `null` if there is no previous page
     */
    fun previous(total: Int, pageSize: Int): PageRequest? {
        val newOffset = max(offset - pageSize, 0)
        val newEnd = minOf(offset, total - 1)
        val newSize = newEnd - newOffset
        return if (newSize > 0)
            PageRequest(newOffset, newSize)
        else
            null
    }

    /**
     * Computes the next page request.
     *
     * @param total Total number of elements in the list
     * @param pageSize Size for the next page
     * @return `null` if there is no next page
     */
    fun next(total: Int, pageSize: Int): PageRequest? {
        val newOffset = min(offset + size, total)
        val newEnd = minOf(offset + size + pageSize, total)
        val newSize = newEnd - newOffset
        return if (newSize > 0)
            PageRequest(newOffset, newSize)
        else
            null
    }

    companion object {
        const val DEFAULT_PAGE_SIZE = 20
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy