mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2024-11-26 03:58:45 +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
|
||||
get() = page.toLong() == lastPageLong
|
||||
|
||||
fun <T> emptyPaginationResult() = PaginationResult<T>(0, 0, emptyList(), 0L)
|
||||
fun <T> emptyPaginationResult(
|
||||
basePagination: Pagination
|
||||
basePagination: Pagination,
|
||||
objectsNumber: Number
|
||||
) = PaginationResult<T>(
|
||||
basePagination.page,
|
||||
basePagination.size,
|
||||
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
|
||||
|
@ -21,8 +21,8 @@ fun <T> Iterable<T>.paginate(with: Pagination): PaginationResult<T> {
|
||||
}
|
||||
|
||||
fun <T> List<T>.paginate(with: Pagination): PaginationResult<T> {
|
||||
if (with.firstIndex !in indices || with.lastIndex !in indices) {
|
||||
return emptyPaginationResult(with)
|
||||
if (with.firstIndex >= size || with.lastIndex < 0) {
|
||||
return emptyPaginationResult(with, size.toLong())
|
||||
}
|
||||
return asSequence().drop(with.firstIndex).take(with.size).toList().createPaginationResult(
|
||||
with,
|
||||
|
@ -38,7 +38,27 @@ class PaginationPaging {
|
||||
doForAllWithNextPaging(startPagination) {
|
||||
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)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user