mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2024-11-26 03:58:45 +00:00
commit
9091fa5bd8
@ -1,5 +1,13 @@
|
||||
# Changelog
|
||||
|
||||
## 0.2.1
|
||||
|
||||
* `Pagination`
|
||||
* `Common`:
|
||||
* Extension `Pagination#reverse` has been added
|
||||
* Factory `PaginationByIndexes`
|
||||
* Shortcut `calculatePagesNumber` with reversed parameters
|
||||
|
||||
## 0.2.0
|
||||
|
||||
* `Repos`
|
||||
|
@ -19,4 +19,4 @@ github_release_plugin_version=2.2.12
|
||||
uuidVersion=0.2.2
|
||||
|
||||
group=dev.inmo
|
||||
version=0.2.0
|
||||
version=0.2.1
|
||||
|
@ -43,6 +43,11 @@ val Pagination.lastIndex: Int
|
||||
fun calculatePagesNumber(datasetSize: Long, pageSize: Int): Int {
|
||||
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]
|
||||
*/
|
||||
|
@ -27,7 +27,21 @@ data class SimplePagination(
|
||||
override val size: Int
|
||||
) : Pagination
|
||||
|
||||
/**
|
||||
* Factory for [SimplePagination]
|
||||
*/
|
||||
fun Pagination(
|
||||
page: Int,
|
||||
size: Int
|
||||
) = 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)
|
||||
}
|
||||
|
@ -0,0 +1,34 @@
|
||||
package dev.inmo.micro_utils.pagination.utils
|
||||
|
||||
import dev.inmo.micro_utils.pagination.*
|
||||
|
||||
/**
|
||||
* Example:
|
||||
*
|
||||
* * `|__f__l_______________________|` will be transformed to `|_______________________f__l__|`
|
||||
* * `|__f__l_|` will be transformed to `|__f__l_|`
|
||||
*
|
||||
* @return Reversed version of this [Pagination]
|
||||
*/
|
||||
fun Pagination.reverse(objectsCount: Long): SimplePagination {
|
||||
val firstIndex = (objectsCount - (this.lastIndex + 1)).let {
|
||||
when {
|
||||
it < 0 -> it
|
||||
it > objectsCount -> objectsCount
|
||||
else -> it
|
||||
}
|
||||
}.toInt()
|
||||
val lastIndex = (objectsCount - (this.firstIndex + 1)).let {
|
||||
when {
|
||||
it < 0 -> it
|
||||
it > objectsCount -> objectsCount
|
||||
else -> it
|
||||
}
|
||||
}.toInt()
|
||||
return PaginationByIndexes(firstIndex, lastIndex)
|
||||
}
|
||||
|
||||
/**
|
||||
* Shortcut for [reverse]
|
||||
*/
|
||||
fun Pagination.reverse(objectsCount: Int) = reverse(objectsCount.toLong())
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -2,6 +2,8 @@ package dev.inmo.micro_utils.repos
|
||||
|
||||
import dev.inmo.micro_utils.coroutines.BroadcastFlow
|
||||
import dev.inmo.micro_utils.pagination.*
|
||||
import dev.inmo.micro_utils.pagination.utils.paginate
|
||||
import dev.inmo.micro_utils.pagination.utils.reverse
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
class ReadMapKeyValueRepo<Key, Value>(
|
||||
@ -13,31 +15,27 @@ class ReadMapKeyValueRepo<Key, Value>(
|
||||
pagination: Pagination,
|
||||
reversed: Boolean
|
||||
): PaginationResult<Value> {
|
||||
val firstIndex: Int = if (reversed) {
|
||||
val size = map.size
|
||||
(size - pagination.lastIndex).let { if (it < 0) 0 else it }
|
||||
val values = map.values
|
||||
val actualPagination = if (reversed) pagination.reverse(values.size) else pagination
|
||||
return values.paginate(actualPagination).let {
|
||||
if (reversed) {
|
||||
it.copy(results = it.results.reversed())
|
||||
} else {
|
||||
pagination.firstIndex
|
||||
it
|
||||
}
|
||||
}
|
||||
|
||||
return map.keys.drop(firstIndex).take(pagination.size).mapNotNull { map[it] }.createPaginationResult(
|
||||
firstIndex,
|
||||
count()
|
||||
)
|
||||
}
|
||||
|
||||
override suspend fun keys(pagination: Pagination, reversed: Boolean): PaginationResult<Key> {
|
||||
val firstIndex: Int = if (reversed) {
|
||||
val size = map.size
|
||||
(size - pagination.lastIndex).let { if (it < 0) 0 else it }
|
||||
val keys = map.keys
|
||||
val actualPagination = if (reversed) pagination.reverse(keys.size) else pagination
|
||||
return keys.paginate(actualPagination).let {
|
||||
if (reversed) {
|
||||
it.copy(results = it.results.reversed())
|
||||
} else {
|
||||
pagination.firstIndex
|
||||
it
|
||||
}
|
||||
}
|
||||
|
||||
return map.keys.drop(firstIndex).take(pagination.size).createPaginationResult(
|
||||
firstIndex,
|
||||
count()
|
||||
)
|
||||
}
|
||||
|
||||
override suspend fun contains(key: Key): Boolean = map.containsKey(key)
|
||||
|
@ -3,6 +3,7 @@ package dev.inmo.micro_utils.repos
|
||||
import dev.inmo.micro_utils.coroutines.BroadcastFlow
|
||||
import dev.inmo.micro_utils.pagination.*
|
||||
import dev.inmo.micro_utils.pagination.utils.paginate
|
||||
import dev.inmo.micro_utils.pagination.utils.reverse
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
class MapReadOneToManyKeyValueRepo<Key, Value>(
|
||||
@ -13,8 +14,7 @@ class MapReadOneToManyKeyValueRepo<Key, Value>(
|
||||
|
||||
return list.paginate(
|
||||
if (reversed) {
|
||||
val firstIndex = (map.size - pagination.lastIndex).let { if (it < 0) 0 else it }
|
||||
SimplePagination(firstIndex, pagination.size)
|
||||
pagination.reverse(list.size)
|
||||
} else {
|
||||
pagination
|
||||
}
|
||||
@ -22,17 +22,15 @@ class MapReadOneToManyKeyValueRepo<Key, Value>(
|
||||
}
|
||||
|
||||
override suspend fun keys(pagination: Pagination, reversed: Boolean): PaginationResult<Key> {
|
||||
val firstIndex: Int = if (reversed) {
|
||||
val size = map.size
|
||||
(size - pagination.lastIndex).let { if (it < 0) 0 else it }
|
||||
val keys = map.keys
|
||||
val actualPagination = if (reversed) pagination.reverse(keys.size) else pagination
|
||||
return keys.paginate(actualPagination).let {
|
||||
if (reversed) {
|
||||
it.copy(results = it.results.reversed())
|
||||
} else {
|
||||
pagination.firstIndex
|
||||
it
|
||||
}
|
||||
}
|
||||
|
||||
return map.keys.drop(firstIndex).take(pagination.size).createPaginationResult(
|
||||
firstIndex,
|
||||
count()
|
||||
)
|
||||
}
|
||||
|
||||
override suspend fun contains(k: Key): Boolean = map.containsKey(k)
|
||||
|
Loading…
Reference in New Issue
Block a user