mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2024-11-29 21:48:45 +00:00
several additions and tests for pagination reversing
This commit is contained in:
parent
ff905e1491
commit
4972cc9daa
@ -5,6 +5,8 @@
|
|||||||
* `Pagination`
|
* `Pagination`
|
||||||
* `Common`:
|
* `Common`:
|
||||||
* Extension `Pagination#reverse` has been added
|
* Extension `Pagination#reverse` has been added
|
||||||
|
* Factory `PaginationByIndexes`
|
||||||
|
* Shortcut `calculatePagesNumber` with reversed parameters
|
||||||
|
|
||||||
## 0.2.0
|
## 0.2.0
|
||||||
|
|
||||||
|
@ -43,6 +43,11 @@ val Pagination.lastIndex: Int
|
|||||||
fun calculatePagesNumber(datasetSize: Long, pageSize: Int): Int {
|
fun calculatePagesNumber(datasetSize: Long, pageSize: Int): Int {
|
||||||
return ceil(datasetSize.toDouble() / pageSize).toInt()
|
return ceil(datasetSize.toDouble() / pageSize).toInt()
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* Calculates pages count for given [datasetSize]. As a fact, it is shortcut for [calculatePagesNumber]
|
||||||
|
* @return calculated page number which can be correctly used in [PaginationResult] as [PaginationResult.page] value
|
||||||
|
*/
|
||||||
|
fun calculatePagesNumber(pageSize: Int, datasetSize: Long): Int = calculatePagesNumber(datasetSize, pageSize)
|
||||||
/**
|
/**
|
||||||
* Calculates pages count for given [datasetSize]
|
* Calculates pages count for given [datasetSize]
|
||||||
*/
|
*/
|
||||||
|
@ -27,7 +27,21 @@ data class SimplePagination(
|
|||||||
override val size: Int
|
override val size: Int
|
||||||
) : Pagination
|
) : Pagination
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Factory for [SimplePagination]
|
||||||
|
*/
|
||||||
fun Pagination(
|
fun Pagination(
|
||||||
page: Int,
|
page: Int,
|
||||||
size: Int
|
size: Int
|
||||||
) = SimplePagination(page, size)
|
) = SimplePagination(page, size)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param firstIndex Inclusive first index of pagination
|
||||||
|
* @param lastIndex INCLUSIVE last index of pagination (last index of object covered by result [SimplePagination])
|
||||||
|
*/
|
||||||
|
fun PaginationByIndexes(
|
||||||
|
firstIndex: Int,
|
||||||
|
lastIndex: Int
|
||||||
|
) = maxOf(0, (lastIndex - firstIndex + 1)).let { size ->
|
||||||
|
Pagination(calculatePage(firstIndex, size), size)
|
||||||
|
}
|
||||||
|
@ -11,16 +11,19 @@ import dev.inmo.micro_utils.pagination.*
|
|||||||
* @return Reversed version of this [Pagination]
|
* @return Reversed version of this [Pagination]
|
||||||
*/
|
*/
|
||||||
fun Pagination.reverse(objectsCount: Long): SimplePagination {
|
fun Pagination.reverse(objectsCount: Long): SimplePagination {
|
||||||
val resultSize = minOf(size, objectsCount.toInt())
|
val firstIndex = (objectsCount - (this.lastIndex + 1)).let {
|
||||||
return when {
|
when {
|
||||||
firstIndex > objectsCount -> Pagination(calculatePage(resultSize, resultSize), resultSize)
|
it < 0 -> it
|
||||||
size > objectsCount -> FirstPagePagination(resultSize)
|
it > objectsCount -> objectsCount
|
||||||
else -> {
|
else -> it
|
||||||
val firstIndex = (objectsCount - firstIndex - resultSize).toInt()
|
|
||||||
Pagination(
|
|
||||||
firstIndex,
|
|
||||||
resultSize
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}.toInt()
|
||||||
|
val lastIndex = (objectsCount - (this.firstIndex + 1)).let {
|
||||||
|
when {
|
||||||
|
it < 0 -> it
|
||||||
|
it > objectsCount -> objectsCount
|
||||||
|
else -> it
|
||||||
|
}
|
||||||
|
}.toInt()
|
||||||
|
return PaginationByIndexes(firstIndex, lastIndex)
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,24 @@
|
|||||||
|
package dev.inmo.micro_utils.pagination.utils
|
||||||
|
|
||||||
|
import dev.inmo.micro_utils.pagination.*
|
||||||
|
import kotlin.test.Test
|
||||||
|
import kotlin.test.assertEquals
|
||||||
|
|
||||||
|
class PaginationReversingTests {
|
||||||
|
@Test
|
||||||
|
fun testThatCommonCaseWorksOk() {
|
||||||
|
val pageSize = 2
|
||||||
|
val collectionSize = 8
|
||||||
|
val pages = calculatePage(collectionSize, pageSize)
|
||||||
|
|
||||||
|
doWithPagination(FirstPagePagination(pageSize)) {
|
||||||
|
val reversed = it.reverse(collectionSize.toLong())
|
||||||
|
assertEquals(Pagination(calculatePage(collectionSize - it.firstIndex - it.size, it.size), it.size), reversed)
|
||||||
|
if (it.page < pages) {
|
||||||
|
it.nextPage()
|
||||||
|
} else {
|
||||||
|
null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user