mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2024-12-25 10:07:17 +00:00
make improvements in pagination
This commit is contained in:
parent
72f2fe3cc3
commit
77f56c5dda
@ -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,
|
||||
|
@ -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> {
|
||||
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()
|
||||
return if (reversed) {
|
||||
val actualPagination = with.optionallyReverse(
|
||||
size,
|
||||
reversed
|
||||
)
|
||||
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> {
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user