several additions and tests for pagination reversing

This commit is contained in:
InsanusMokrassar 2020-10-22 15:41:21 +06:00
parent ff905e1491
commit 4972cc9daa
5 changed files with 59 additions and 11 deletions

View File

@ -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

View File

@ -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]
*/ */

View File

@ -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)
}

View File

@ -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)
} }

View File

@ -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
}
}
}
}