mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2024-11-22 16:23:50 +00:00
make improvements in pagination
This commit is contained in:
parent
72f2fe3cc3
commit
77f56c5dda
@ -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,
|
||||||
|
@ -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> {
|
||||||
|
@ -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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user