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 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 * @return New [PaginationResult] with [data] without checking of data sizes equality
*/ */
fun <I, O> PaginationResult<I>.changeResultsUnchecked( fun <I, O> PaginationResult<I>.changeResultsUnchecked(
data: List<O> 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 * @return New [PaginationResult] with [data] <b>with</b> checking of data sizes equality
*/ */
fun <I, O> PaginationResult<I>.changeResults( fun <I, O> PaginationResult<I>.changeResults(
data: List<O> data: List<O>
): PaginationResult<O> { ): PaginationResult<O> = changeResults { data }
require(data.size == results.size)
return changeResultsUnchecked(data)
}
fun <T> List<T>.createPaginationResult( fun <T> List<T>.createPaginationResult(
pagination: Pagination, 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> { fun <T> List<T>.paginate(with: Pagination): PaginationResult<T> {
val indices = indices if (with.firstIndex !in indices || with.lastIndex !in indices) {
val firstIndex = with.firstIndex.coerceIn(indices) return emptyPaginationResult(with)
val lastIndex = with.lastIndex.coerceIn(indices) + 1 // up to size
if (firstIndex > lastIndex) {
return emptyPaginationResult()
} }
return subList(firstIndex, lastIndex).createPaginationResult( return asSequence().drop(with.firstIndex).take(with.size).toList().createPaginationResult(
with, with,
size.toLong() size.toLong()
) )
} }
fun <T> List<T>.paginate(with: Pagination, reversed: Boolean): PaginationResult<T> { fun <T> List<T>.paginate(with: Pagination, reversed: Boolean): PaginationResult<T> {
val actualPagination = with.optionallyReverse( return if (reversed) {
size, val actualPagination = with.optionallyReverse(
reversed size,
) reversed
val indices = indices )
val firstIndex = actualPagination.firstIndex.coerceIn(indices) paginate(actualPagination).changeResultsUnchecked { results.reversed() }
val lastIndex = actualPagination.lastIndex.coerceIn(indices) + 1 // up to size } else {
if (firstIndex > lastIndex) { paginate(with)
return emptyPaginationResult()
} }
return subList(firstIndex, lastIndex).optionallyReverse(reversed).createPaginationResult(
with,
size.toLong()
)
} }
fun <T> Set<T>.paginate(with: Pagination): PaginationResult<T> { fun <T> Set<T>.paginate(with: Pagination): PaginationResult<T> {

View File

@ -1,11 +1,9 @@
package dev.inmo.micro_utils.pagination.utils package dev.inmo.micro_utils.pagination.utils
import dev.inmo.micro_utils.pagination.FirstPagePagination import dev.inmo.micro_utils.pagination.*
import dev.inmo.micro_utils.pagination.firstIndex
import dev.inmo.micro_utils.pagination.isLastPage
import dev.inmo.micro_utils.pagination.lastIndexExclusive
import kotlin.test.Test import kotlin.test.Test
import kotlin.test.assertEquals import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertTrue import kotlin.test.assertTrue
class PaginationPaging { class PaginationPaging {
@ -31,4 +29,24 @@ class PaginationPaging {
assertTrue(lastPageHappened) 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)
}
} }