make improvements in pagination

This commit is contained in:
InsanusMokrassar 2024-05-26 23:35:24 +06:00
parent 72f2fe3cc3
commit 77f56c5dda
3 changed files with 52 additions and 29 deletions

View File

@ -60,21 +60,35 @@ fun <T> emptyPaginationResult(
0L
)
/**
* @return New [PaginationResult] with [data] without checking of data sizes equality
*/
inline fun <I, O> PaginationResult<I>.changeResultsUnchecked(
block: PaginationResult<I>.() -> List<O>
): PaginationResult<O> = PaginationResult(page, size, block(), objectsNumber)
/**
* @return New [PaginationResult] with [data] without checking of data sizes equality
*/
fun <I, O> PaginationResult<I>.changeResultsUnchecked(
data: List<O>
): PaginationResult<O> = PaginationResult(page, size, data, objectsNumber)
): PaginationResult<O> = changeResultsUnchecked { data }
/**
* @return New [PaginationResult] with [data] <b>with</b> checking of data sizes equality
*/
inline fun <I, O> PaginationResult<I>.changeResults(
block: PaginationResult<I>.() -> List<O>
): PaginationResult<O> {
val data = block()
require(data.size == results.size)
return changeResultsUnchecked(data)
}
/**
* @return New [PaginationResult] with [data] <b>with</b> checking of data sizes equality
*/
fun <I, O> PaginationResult<I>.changeResults(
data: List<O>
): PaginationResult<O> {
require(data.size == results.size)
return changeResultsUnchecked(data)
}
): PaginationResult<O> = changeResults { data }
fun <T> List<T>.createPaginationResult(
pagination: Pagination,

View File

@ -21,34 +21,25 @@ fun <T> Iterable<T>.paginate(with: Pagination): PaginationResult<T> {
}
fun <T> List<T>.paginate(with: Pagination): PaginationResult<T> {
val indices = indices
val firstIndex = with.firstIndex.coerceIn(indices)
val lastIndex = with.lastIndex.coerceIn(indices) + 1 // up to size
if (firstIndex > lastIndex) {
return emptyPaginationResult()
if (with.firstIndex !in indices || with.lastIndex !in indices) {
return emptyPaginationResult(with)
}
return subList(firstIndex, lastIndex).createPaginationResult(
return asSequence().drop(with.firstIndex).take(with.size).toList().createPaginationResult(
with,
size.toLong()
)
}
fun <T> List<T>.paginate(with: Pagination, reversed: Boolean): PaginationResult<T> {
return if (reversed) {
val actualPagination = with.optionallyReverse(
size,
reversed
)
val indices = indices
val firstIndex = actualPagination.firstIndex.coerceIn(indices)
val lastIndex = actualPagination.lastIndex.coerceIn(indices) + 1 // up to size
if (firstIndex > lastIndex) {
return emptyPaginationResult()
paginate(actualPagination).changeResultsUnchecked { results.reversed() }
} else {
paginate(with)
}
return subList(firstIndex, lastIndex).optionallyReverse(reversed).createPaginationResult(
with,
size.toLong()
)
}
fun <T> Set<T>.paginate(with: Pagination): PaginationResult<T> {

View File

@ -1,11 +1,9 @@
package dev.inmo.micro_utils.pagination.utils
import dev.inmo.micro_utils.pagination.FirstPagePagination
import dev.inmo.micro_utils.pagination.firstIndex
import dev.inmo.micro_utils.pagination.isLastPage
import dev.inmo.micro_utils.pagination.lastIndexExclusive
import dev.inmo.micro_utils.pagination.*
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertTrue
class PaginationPaging {
@ -31,4 +29,24 @@ class PaginationPaging {
assertTrue(lastPageHappened)
}
@Test
fun testEmptyPaginateOnList() {
val list = listOf<Int>()
val startPagination = FirstPagePagination(2)
var paginationHappend = false
doForAllWithNextPaging(startPagination) {
val resultPagination = list.paginate(it)
assertEquals(resultPagination, emptyPaginationResult(it))
assertFalse(paginationHappend)
paginationHappend = true
resultPagination
}
assertTrue(paginationHappend)
}
}