mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2024-11-10 18:33:49 +00:00
more fixes to god of fixes in paginations
This commit is contained in:
parent
77f56c5dda
commit
758a92410b
@ -50,15 +50,19 @@ val PaginationResult<*>.lastPage
|
|||||||
val PaginationResult<*>.isLastPage
|
val PaginationResult<*>.isLastPage
|
||||||
get() = page.toLong() == lastPageLong
|
get() = page.toLong() == lastPageLong
|
||||||
|
|
||||||
fun <T> emptyPaginationResult() = PaginationResult<T>(0, 0, emptyList(), 0L)
|
|
||||||
fun <T> emptyPaginationResult(
|
fun <T> emptyPaginationResult(
|
||||||
basePagination: Pagination
|
basePagination: Pagination,
|
||||||
|
objectsNumber: Number
|
||||||
) = PaginationResult<T>(
|
) = PaginationResult<T>(
|
||||||
basePagination.page,
|
basePagination.page,
|
||||||
basePagination.size,
|
basePagination.size,
|
||||||
emptyList(),
|
emptyList(),
|
||||||
0L
|
objectsNumber.toLong()
|
||||||
)
|
)
|
||||||
|
fun <T> emptyPaginationResult(
|
||||||
|
basePagination: Pagination,
|
||||||
|
) = emptyPaginationResult<T>(basePagination, 0)
|
||||||
|
fun <T> emptyPaginationResult() = emptyPaginationResult<T>(FirstPagePagination(0))
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return New [PaginationResult] with [data] without checking of data sizes equality
|
* @return New [PaginationResult] with [data] without checking of data sizes equality
|
||||||
|
@ -21,8 +21,8 @@ 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> {
|
||||||
if (with.firstIndex !in indices || with.lastIndex !in indices) {
|
if (with.firstIndex >= size || with.lastIndex < 0) {
|
||||||
return emptyPaginationResult(with)
|
return emptyPaginationResult(with, size.toLong())
|
||||||
}
|
}
|
||||||
return asSequence().drop(with.firstIndex).take(with.size).toList().createPaginationResult(
|
return asSequence().drop(with.firstIndex).take(with.size).toList().createPaginationResult(
|
||||||
with,
|
with,
|
||||||
|
@ -38,7 +38,27 @@ class PaginationPaging {
|
|||||||
doForAllWithNextPaging(startPagination) {
|
doForAllWithNextPaging(startPagination) {
|
||||||
val resultPagination = list.paginate(it)
|
val resultPagination = list.paginate(it)
|
||||||
|
|
||||||
assertEquals(resultPagination, emptyPaginationResult(it))
|
assertEquals(resultPagination, emptyPaginationResult(it, list.size))
|
||||||
|
|
||||||
|
assertFalse(paginationHappend)
|
||||||
|
|
||||||
|
paginationHappend = true
|
||||||
|
|
||||||
|
resultPagination
|
||||||
|
}
|
||||||
|
|
||||||
|
assertTrue(paginationHappend)
|
||||||
|
}
|
||||||
|
@Test
|
||||||
|
fun testRightOutPaginateOnList() {
|
||||||
|
val list = (0 until 7).toList()
|
||||||
|
val startPagination = SimplePagination(page = 4, size = 2)
|
||||||
|
|
||||||
|
var paginationHappend = false
|
||||||
|
doForAllWithNextPaging(startPagination) {
|
||||||
|
val resultPagination = list.paginate(it)
|
||||||
|
|
||||||
|
assertEquals(resultPagination, emptyPaginationResult(it, list.size))
|
||||||
|
|
||||||
assertFalse(paginationHappend)
|
assertFalse(paginationHappend)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user