mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2025-10-09 09:20:25 +00:00
Compare commits
10 Commits
Author | SHA1 | Date | |
---|---|---|---|
dbd2e963a1 | |||
6928ca5329 | |||
9ece160aa8 | |||
6115c1bcac | |||
e026e94cbf | |||
56cdd8d6af | |||
899e6760e1 | |||
864d0ffcc6 | |||
c6f417f8c8 | |||
9091fa5bd8 |
@@ -1,5 +1,13 @@
|
|||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
|
## 0.2.2
|
||||||
|
|
||||||
|
* `Repos`
|
||||||
|
* `Common`
|
||||||
|
* Several new methods `ReadOneToManyKeyValueRepo#getAll`
|
||||||
|
* Several new method `WriteOneToManyKeyValueRepo#add` and several extensions
|
||||||
|
* Several new method `WriteOneToManyKeyValueRepo#remove` and several extensions
|
||||||
|
|
||||||
## 0.2.1
|
## 0.2.1
|
||||||
|
|
||||||
* `Pagination`
|
* `Pagination`
|
||||||
@@ -7,6 +15,7 @@
|
|||||||
* Extension `Pagination#reverse` has been added
|
* Extension `Pagination#reverse` has been added
|
||||||
* Factory `PaginationByIndexes`
|
* Factory `PaginationByIndexes`
|
||||||
* Shortcut `calculatePagesNumber` with reversed parameters
|
* Shortcut `calculatePagesNumber` with reversed parameters
|
||||||
|
* Value `emptyPagination` for empty `SimplePagination` cases
|
||||||
|
|
||||||
## 0.2.0
|
## 0.2.0
|
||||||
|
|
||||||
|
@@ -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.1
|
version=0.2.2
|
||||||
|
@@ -14,6 +14,8 @@ inline fun FirstPagePagination(size: Int = defaultMediumPageSize) =
|
|||||||
size = size
|
size = size
|
||||||
)
|
)
|
||||||
|
|
||||||
|
val emptyPagination = Pagination(0, 0)
|
||||||
|
|
||||||
@Suppress("NOTHING_TO_INLINE")
|
@Suppress("NOTHING_TO_INLINE")
|
||||||
inline fun Pagination.nextPage() =
|
inline fun Pagination.nextPage() =
|
||||||
SimplePagination(
|
SimplePagination(
|
||||||
|
@@ -10,22 +10,16 @@ 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(datasetSize: Long): SimplePagination {
|
||||||
val firstIndex = (objectsCount - (this.lastIndex + 1)).let {
|
val pagesNumber = calculatePagesNumber(size, datasetSize)
|
||||||
when {
|
val newPage = pagesNumber - page - 1
|
||||||
it < 0 -> it
|
return when {
|
||||||
it > objectsCount -> objectsCount
|
page < 0 || page >= pagesNumber -> emptyPagination
|
||||||
else -> it
|
else -> Pagination(
|
||||||
}
|
newPage,
|
||||||
}.toInt()
|
size
|
||||||
val lastIndex = (objectsCount - (this.firstIndex + 1)).let {
|
)
|
||||||
when {
|
}
|
||||||
it < 0 -> it
|
|
||||||
it > objectsCount -> objectsCount
|
|
||||||
else -> it
|
|
||||||
}
|
|
||||||
}.toInt()
|
|
||||||
return PaginationByIndexes(firstIndex, lastIndex)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -7,18 +7,18 @@ import kotlin.test.assertEquals
|
|||||||
class PaginationReversingTests {
|
class PaginationReversingTests {
|
||||||
@Test
|
@Test
|
||||||
fun testThatCommonCaseWorksOk() {
|
fun testThatCommonCaseWorksOk() {
|
||||||
val pageSize = 2
|
val pageSize = 3
|
||||||
val collectionSize = 8
|
val collectionSize = 9
|
||||||
val pages = calculatePage(collectionSize, pageSize)
|
|
||||||
|
|
||||||
doWithPagination(FirstPagePagination(pageSize)) {
|
assertEquals(Pagination(-1, pageSize).reverse(collectionSize), Pagination(0, 0))
|
||||||
val reversed = it.reverse(collectionSize.toLong())
|
|
||||||
assertEquals(Pagination(calculatePage(collectionSize - it.firstIndex - it.size, it.size), it.size), reversed)
|
val middleFirstIndex = collectionSize / 2 - pageSize / 2
|
||||||
if (it.page < pages) {
|
val middleLastIndex = middleFirstIndex + pageSize - 1
|
||||||
it.nextPage()
|
assertEquals(
|
||||||
} else {
|
PaginationByIndexes(middleFirstIndex, middleLastIndex).reverse(collectionSize),
|
||||||
null
|
PaginationByIndexes(middleFirstIndex, middleLastIndex)
|
||||||
}
|
)
|
||||||
}
|
|
||||||
|
assertEquals(Pagination(calculatePagesNumber(collectionSize, pageSize), pageSize).reverse(collectionSize), Pagination(0, 0))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,7 +1,6 @@
|
|||||||
package dev.inmo.micro_utils.repos
|
package dev.inmo.micro_utils.repos
|
||||||
|
|
||||||
import dev.inmo.micro_utils.pagination.Pagination
|
import dev.inmo.micro_utils.pagination.*
|
||||||
import dev.inmo.micro_utils.pagination.PaginationResult
|
|
||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
|
||||||
interface ReadOneToManyKeyValueRepo<Key, Value> : Repo {
|
interface ReadOneToManyKeyValueRepo<Key, Value> : Repo {
|
||||||
@@ -11,6 +10,30 @@ interface ReadOneToManyKeyValueRepo<Key, Value> : Repo {
|
|||||||
suspend fun contains(k: Key, v: Value): Boolean
|
suspend fun contains(k: Key, v: Value): Boolean
|
||||||
suspend fun count(k: Key): Long
|
suspend fun count(k: Key): Long
|
||||||
suspend fun count(): Long
|
suspend fun count(): Long
|
||||||
|
|
||||||
|
suspend fun getAll(k: Key, reversed: Boolean = false): List<Value> = mutableListOf<Value>().also { list ->
|
||||||
|
doWithPagination {
|
||||||
|
get(k, it).also {
|
||||||
|
list.addAll(it.results)
|
||||||
|
}.nextPageIfNotEmpty()
|
||||||
|
}
|
||||||
|
if (reversed) {
|
||||||
|
list.reverse()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* WARNING!!! THIS METHOD PROBABLY IS NOT EFFICIENT, USE WITH CAUTION
|
||||||
|
*/
|
||||||
|
suspend fun getAll(reverseLists: Boolean = false): Map<Key, List<Value>> = mutableMapOf<Key, List<Value>>().also { map ->
|
||||||
|
doWithPagination {
|
||||||
|
keys(it).also { paginationResult ->
|
||||||
|
paginationResult.results.forEach { k ->
|
||||||
|
map[k] = getAll(k, reverseLists)
|
||||||
|
}
|
||||||
|
}.nextPageIfNotEmpty()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@Deprecated("Renamed", ReplaceWith("ReadOneToManyKeyValueRepo", "dev.inmo.micro_utils.repos.ReadOneToManyKeyValueRepo"))
|
@Deprecated("Renamed", ReplaceWith("ReadOneToManyKeyValueRepo", "dev.inmo.micro_utils.repos.ReadOneToManyKeyValueRepo"))
|
||||||
typealias OneToManyReadKeyValueRepo<Key, Value> = ReadOneToManyKeyValueRepo<Key, Value>
|
typealias OneToManyReadKeyValueRepo<Key, Value> = ReadOneToManyKeyValueRepo<Key, Value>
|
||||||
@@ -20,11 +43,51 @@ interface WriteOneToManyKeyValueRepo<Key, Value> : Repo {
|
|||||||
val onValueRemoved: Flow<Pair<Key, Value>>
|
val onValueRemoved: Flow<Pair<Key, Value>>
|
||||||
val onDataCleared: Flow<Key>
|
val onDataCleared: Flow<Key>
|
||||||
|
|
||||||
|
suspend fun add(toAdd: Map<Key, List<Value>>) = toAdd.forEach { (k, values) ->
|
||||||
|
values.forEach { v ->
|
||||||
|
add(k, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@Deprecated("Will be extracted as extension for other add method")
|
||||||
suspend fun add(k: Key, v: Value)
|
suspend fun add(k: Key, v: Value)
|
||||||
|
|
||||||
|
suspend fun remove(toRemove: Map<Key, List<Value>>) = toRemove.forEach { (k, values) ->
|
||||||
|
values.forEach { v ->
|
||||||
|
remove(k, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@Deprecated("Will be extracted as extension for other remove method")
|
||||||
suspend fun remove(k: Key, v: Value)
|
suspend fun remove(k: Key, v: Value)
|
||||||
|
|
||||||
suspend fun clear(k: Key)
|
suspend fun clear(k: Key)
|
||||||
}
|
}
|
||||||
@Deprecated("Renamed", ReplaceWith("WriteOneToManyKeyValueRepo", "dev.inmo.micro_utils.repos.WriteOneToManyKeyValueRepo"))
|
@Deprecated("Renamed", ReplaceWith("WriteOneToManyKeyValueRepo", "dev.inmo.micro_utils.repos.WriteOneToManyKeyValueRepo"))
|
||||||
typealias OneToManyWriteKeyValueRepo<Key, Value> = WriteOneToManyKeyValueRepo<Key, Value>
|
typealias OneToManyWriteKeyValueRepo<Key, Value> = WriteOneToManyKeyValueRepo<Key, Value>
|
||||||
|
|
||||||
interface OneToManyKeyValueRepo<Key, Value> : ReadOneToManyKeyValueRepo<Key, Value>, WriteOneToManyKeyValueRepo<Key, Value>
|
interface OneToManyKeyValueRepo<Key, Value> : ReadOneToManyKeyValueRepo<Key, Value>, WriteOneToManyKeyValueRepo<Key, Value>
|
||||||
|
|
||||||
|
suspend inline fun <Key, Value, REPO : WriteOneToManyKeyValueRepo<Key, Value>> REPO.add(
|
||||||
|
k: Key,
|
||||||
|
vararg v: Value
|
||||||
|
) = add(mapOf(k to v.toList()))
|
||||||
|
|
||||||
|
suspend inline fun <Key, Value, REPO : WriteOneToManyKeyValueRepo<Key, Value>> REPO.add(
|
||||||
|
keysAndValues: List<Pair<Key, List<Value>>>
|
||||||
|
) = add(keysAndValues.toMap())
|
||||||
|
|
||||||
|
suspend inline fun <Key, Value, REPO : WriteOneToManyKeyValueRepo<Key, Value>> REPO.add(
|
||||||
|
vararg keysAndValues: Pair<Key, List<Value>>
|
||||||
|
) = add(keysAndValues.toMap())
|
||||||
|
|
||||||
|
suspend inline fun <Key, Value, REPO : WriteOneToManyKeyValueRepo<Key, Value>> REPO.remove(
|
||||||
|
k: Key,
|
||||||
|
vararg v: Value
|
||||||
|
) = remove(mapOf(k to v.toList()))
|
||||||
|
|
||||||
|
suspend inline fun <Key, Value, REPO : WriteOneToManyKeyValueRepo<Key, Value>> REPO.remove(
|
||||||
|
keysAndValues: List<Pair<Key, List<Value>>>
|
||||||
|
) = remove(keysAndValues.toMap())
|
||||||
|
|
||||||
|
suspend inline fun <Key, Value, REPO : WriteOneToManyKeyValueRepo<Key, Value>> REPO.remove(
|
||||||
|
vararg keysAndValues: Pair<Key, List<Value>>
|
||||||
|
) = remove(keysAndValues.toMap())
|
||||||
|
@@ -1,7 +1,7 @@
|
|||||||
package dev.inmo.micro_utils.repos.pagination
|
package dev.inmo.micro_utils.repos.pagination
|
||||||
|
|
||||||
import dev.inmo.micro_utils.pagination.*
|
import dev.inmo.micro_utils.pagination.*
|
||||||
import dev.inmo.micro_utils.repos.*
|
import dev.inmo.micro_utils.repos.ReadStandardKeyValueRepo
|
||||||
|
|
||||||
suspend inline fun <Key, Value, REPO : ReadStandardKeyValueRepo<Key, Value>> REPO.doForAll(
|
suspend inline fun <Key, Value, REPO : ReadStandardKeyValueRepo<Key, Value>> REPO.doForAll(
|
||||||
@Suppress("REDUNDANT_INLINE_SUSPEND_FUNCTION_TYPE")
|
@Suppress("REDUNDANT_INLINE_SUSPEND_FUNCTION_TYPE")
|
||||||
|
@@ -1,7 +1,7 @@
|
|||||||
package dev.inmo.micro_utils.repos.pagination
|
package dev.inmo.micro_utils.repos.pagination
|
||||||
|
|
||||||
import dev.inmo.micro_utils.pagination.*
|
import dev.inmo.micro_utils.pagination.*
|
||||||
import dev.inmo.micro_utils.repos.*
|
import dev.inmo.micro_utils.repos.ReadOneToManyKeyValueRepo
|
||||||
|
|
||||||
suspend inline fun <Key, Value, REPO : ReadOneToManyKeyValueRepo<Key, Value>> REPO.doForAll(
|
suspend inline fun <Key, Value, REPO : ReadOneToManyKeyValueRepo<Key, Value>> REPO.doForAll(
|
||||||
@Suppress("REDUNDANT_INLINE_SUSPEND_FUNCTION_TYPE")
|
@Suppress("REDUNDANT_INLINE_SUSPEND_FUNCTION_TYPE")
|
||||||
|
Reference in New Issue
Block a user