Merge pull request #4 from InsanusMokrassar/0.2.1

0.2.1
This commit is contained in:
InsanusMokrassar 2020-10-22 15:50:30 +06:00 committed by GitHub
commit 9091fa5bd8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 114 additions and 33 deletions

View File

@ -1,5 +1,13 @@
# Changelog # Changelog
## 0.2.1
* `Pagination`
* `Common`:
* Extension `Pagination#reverse` has been added
* Factory `PaginationByIndexes`
* Shortcut `calculatePagesNumber` with reversed parameters
## 0.2.0 ## 0.2.0
* `Repos` * `Repos`

View File

@ -19,4 +19,4 @@ github_release_plugin_version=2.2.12
uuidVersion=0.2.2 uuidVersion=0.2.2
group=dev.inmo group=dev.inmo
version=0.2.0 version=0.2.1

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

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

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

View File

@ -2,6 +2,8 @@ package dev.inmo.micro_utils.repos
import dev.inmo.micro_utils.coroutines.BroadcastFlow import dev.inmo.micro_utils.coroutines.BroadcastFlow
import dev.inmo.micro_utils.pagination.* 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 import kotlinx.coroutines.flow.Flow
class ReadMapKeyValueRepo<Key, Value>( class ReadMapKeyValueRepo<Key, Value>(
@ -13,31 +15,27 @@ class ReadMapKeyValueRepo<Key, Value>(
pagination: Pagination, pagination: Pagination,
reversed: Boolean reversed: Boolean
): PaginationResult<Value> { ): PaginationResult<Value> {
val firstIndex: Int = if (reversed) { val values = map.values
val size = map.size val actualPagination = if (reversed) pagination.reverse(values.size) else pagination
(size - pagination.lastIndex).let { if (it < 0) 0 else it } return values.paginate(actualPagination).let {
} else { if (reversed) {
pagination.firstIndex it.copy(results = it.results.reversed())
} else {
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> { override suspend fun keys(pagination: Pagination, reversed: Boolean): PaginationResult<Key> {
val firstIndex: Int = if (reversed) { val keys = map.keys
val size = map.size val actualPagination = if (reversed) pagination.reverse(keys.size) else pagination
(size - pagination.lastIndex).let { if (it < 0) 0 else it } return keys.paginate(actualPagination).let {
} else { if (reversed) {
pagination.firstIndex it.copy(results = it.results.reversed())
} else {
it
}
} }
return map.keys.drop(firstIndex).take(pagination.size).createPaginationResult(
firstIndex,
count()
)
} }
override suspend fun contains(key: Key): Boolean = map.containsKey(key) override suspend fun contains(key: Key): Boolean = map.containsKey(key)

View File

@ -3,6 +3,7 @@ package dev.inmo.micro_utils.repos
import dev.inmo.micro_utils.coroutines.BroadcastFlow import dev.inmo.micro_utils.coroutines.BroadcastFlow
import dev.inmo.micro_utils.pagination.* import dev.inmo.micro_utils.pagination.*
import dev.inmo.micro_utils.pagination.utils.paginate import dev.inmo.micro_utils.pagination.utils.paginate
import dev.inmo.micro_utils.pagination.utils.reverse
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
class MapReadOneToManyKeyValueRepo<Key, Value>( class MapReadOneToManyKeyValueRepo<Key, Value>(
@ -13,8 +14,7 @@ class MapReadOneToManyKeyValueRepo<Key, Value>(
return list.paginate( return list.paginate(
if (reversed) { if (reversed) {
val firstIndex = (map.size - pagination.lastIndex).let { if (it < 0) 0 else it } pagination.reverse(list.size)
SimplePagination(firstIndex, pagination.size)
} else { } else {
pagination pagination
} }
@ -22,17 +22,15 @@ class MapReadOneToManyKeyValueRepo<Key, Value>(
} }
override suspend fun keys(pagination: Pagination, reversed: Boolean): PaginationResult<Key> { override suspend fun keys(pagination: Pagination, reversed: Boolean): PaginationResult<Key> {
val firstIndex: Int = if (reversed) { val keys = map.keys
val size = map.size val actualPagination = if (reversed) pagination.reverse(keys.size) else pagination
(size - pagination.lastIndex).let { if (it < 0) 0 else it } return keys.paginate(actualPagination).let {
} else { if (reversed) {
pagination.firstIndex it.copy(results = it.results.reversed())
} else {
it
}
} }
return map.keys.drop(firstIndex).take(pagination.size).createPaginationResult(
firstIndex,
count()
)
} }
override suspend fun contains(k: Key): Boolean = map.containsKey(k) override suspend fun contains(k: Key): Boolean = map.containsKey(k)