mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2024-11-23 02:28:47 +00:00
commit
2e429e9704
@ -1,5 +1,14 @@
|
|||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
|
## 0.16.10
|
||||||
|
|
||||||
|
* `Repos`:
|
||||||
|
* `Cache`:
|
||||||
|
* New transformer type: `ReadCRUDFromKeyValueRepo`
|
||||||
|
* New transformer type: `ReadKeyValueFromCRUDRepo`
|
||||||
|
* `Pagination`:
|
||||||
|
* New `paginate` extensions with `reversed` support for `List`/`Set`
|
||||||
|
|
||||||
## 0.16.9
|
## 0.16.9
|
||||||
|
|
||||||
* `Versions`:
|
* `Versions`:
|
||||||
|
@ -14,5 +14,5 @@ crypto_js_version=4.1.1
|
|||||||
# Project data
|
# Project data
|
||||||
|
|
||||||
group=dev.inmo
|
group=dev.inmo
|
||||||
version=0.16.9
|
version=0.16.10
|
||||||
android_code_version=177
|
android_code_version=178
|
||||||
|
2
gradle/wrapper/gradle-wrapper.properties
vendored
2
gradle/wrapper/gradle-wrapper.properties
vendored
@ -1,5 +1,5 @@
|
|||||||
distributionBase=GRADLE_USER_HOME
|
distributionBase=GRADLE_USER_HOME
|
||||||
distributionPath=wrapper/dists
|
distributionPath=wrapper/dists
|
||||||
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5.1-bin.zip
|
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6-bin.zip
|
||||||
zipStoreBase=GRADLE_USER_HOME
|
zipStoreBase=GRADLE_USER_HOME
|
||||||
zipStorePath=wrapper/dists
|
zipStorePath=wrapper/dists
|
||||||
|
@ -0,0 +1,29 @@
|
|||||||
|
package dev.inmo.micro_utils.pagination.utils
|
||||||
|
|
||||||
|
fun <T> Iterable<T>.optionallyReverse(reverse: Boolean): Iterable<T> = when (this) {
|
||||||
|
is List<T> -> optionallyReverse(reverse)
|
||||||
|
is Set<T> -> optionallyReverse(reverse)
|
||||||
|
else -> if (reverse) {
|
||||||
|
reversed()
|
||||||
|
} else {
|
||||||
|
this
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fun <T> List<T>.optionallyReverse(reverse: Boolean): List<T> = if (reverse) {
|
||||||
|
reversed()
|
||||||
|
} else {
|
||||||
|
this
|
||||||
|
}
|
||||||
|
fun <T> Set<T>.optionallyReverse(reverse: Boolean): Set<T> = if (reverse) {
|
||||||
|
reversed().toSet()
|
||||||
|
} else {
|
||||||
|
this
|
||||||
|
}
|
||||||
|
|
||||||
|
inline fun <reified T> Array<T>.optionallyReverse(reverse: Boolean) = if (reverse) {
|
||||||
|
Array(size) {
|
||||||
|
get(lastIndex - it)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
this
|
||||||
|
}
|
@ -32,6 +32,24 @@ fun <T> List<T>.paginate(with: Pagination): PaginationResult<T> {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun <T> List<T>.paginate(with: Pagination, reversed: Boolean): PaginationResult<T> {
|
||||||
|
val actualPagination = with.optionallyReverse(
|
||||||
|
size,
|
||||||
|
reversed
|
||||||
|
)
|
||||||
|
|
||||||
|
val firstIndex = maxOf(actualPagination.firstIndex, 0)
|
||||||
|
val lastIndex = minOf(actualPagination.lastIndexExclusive, size)
|
||||||
|
if (firstIndex > lastIndex) {
|
||||||
|
return emptyPaginationResult()
|
||||||
|
}
|
||||||
|
|
||||||
|
return subList(firstIndex, lastIndex).optionallyReverse(reversed).createPaginationResult(
|
||||||
|
with,
|
||||||
|
size.toLong()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
fun <T> Set<T>.paginate(with: Pagination): PaginationResult<T> {
|
fun <T> Set<T>.paginate(with: Pagination): PaginationResult<T> {
|
||||||
return this.drop(with.firstIndex).take(with.size).createPaginationResult(
|
return this.drop(with.firstIndex).take(with.size).createPaginationResult(
|
||||||
with,
|
with,
|
||||||
@ -39,30 +57,20 @@ fun <T> Set<T>.paginate(with: Pagination): PaginationResult<T> {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun <T> Iterable<T>.optionallyReverse(reverse: Boolean): Iterable<T> = when (this) {
|
fun <T> Set<T>.paginate(with: Pagination, reversed: Boolean): PaginationResult<T> {
|
||||||
is List<T> -> optionallyReverse(reverse)
|
val actualPagination = with.optionallyReverse(
|
||||||
is Set<T> -> optionallyReverse(reverse)
|
size,
|
||||||
else -> if (reverse) {
|
reversed
|
||||||
reversed()
|
)
|
||||||
} else {
|
|
||||||
this
|
val firstIndex = maxOf(actualPagination.firstIndex, 0)
|
||||||
}
|
val lastIndex = minOf(actualPagination.lastIndexExclusive, size)
|
||||||
}
|
if (firstIndex > lastIndex) {
|
||||||
fun <T> List<T>.optionallyReverse(reverse: Boolean): List<T> = if (reverse) {
|
return emptyPaginationResult()
|
||||||
reversed()
|
|
||||||
} else {
|
|
||||||
this
|
|
||||||
}
|
|
||||||
fun <T> Set<T>.optionallyReverse(reverse: Boolean): Set<T> = if (reverse) {
|
|
||||||
reversed().toSet()
|
|
||||||
} else {
|
|
||||||
this
|
|
||||||
}
|
}
|
||||||
|
|
||||||
inline fun <reified T> Array<T>.optionallyReverse(reverse: Boolean) = if (reverse) {
|
return this.drop(firstIndex).take(lastIndex - firstIndex).optionallyReverse(reversed).createPaginationResult(
|
||||||
Array(size) {
|
with,
|
||||||
get(lastIndex - it)
|
size.toLong()
|
||||||
}
|
)
|
||||||
} else {
|
|
||||||
this
|
|
||||||
}
|
}
|
@ -2,6 +2,8 @@ package dev.inmo.micro_utils.repos
|
|||||||
|
|
||||||
import dev.inmo.micro_utils.pagination.*
|
import dev.inmo.micro_utils.pagination.*
|
||||||
import dev.inmo.micro_utils.pagination.utils.doAllWithCurrentPaging
|
import dev.inmo.micro_utils.pagination.utils.doAllWithCurrentPaging
|
||||||
|
import dev.inmo.micro_utils.pagination.utils.getAllWithNextPaging
|
||||||
|
import dev.inmo.micro_utils.pagination.utils.paginate
|
||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -17,24 +19,32 @@ interface ReadKeyValueRepo<Key, Value> : Repo {
|
|||||||
suspend fun get(k: Key): Value?
|
suspend fun get(k: Key): Value?
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method should use sorted by [Key]s search and take the [PaginationResult]. By default, it should use
|
* This method should use sorted by [Key]s search and return the [PaginationResult]. By default, it should use
|
||||||
* ascending sort for [Key]s
|
* ascending sort for [Key]s
|
||||||
*/
|
*/
|
||||||
suspend fun values(pagination: Pagination, reversed: Boolean = false): PaginationResult<Value>
|
suspend fun values(pagination: Pagination, reversed: Boolean = false): PaginationResult<Value>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method should use sorted by [Key]s search and take the [PaginationResult]. By default, it should use
|
* This method should use sorted by [Key]s search and return the [PaginationResult]. By default, it should use
|
||||||
* ascending sort for [Key]s
|
* ascending sort for [Key]s
|
||||||
*/
|
*/
|
||||||
suspend fun keys(pagination: Pagination, reversed: Boolean = false): PaginationResult<Key>
|
suspend fun keys(pagination: Pagination, reversed: Boolean = false): PaginationResult<Key>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method should use sorted by [Key]s search and take the [PaginationResult]. By default, it should use
|
* This method should use sorted by [Key]s search and return the [PaginationResult]. By default, it should use
|
||||||
* ascending sort for [Key]s
|
* ascending sort for [Key]s.
|
||||||
|
*
|
||||||
|
* **DEFAULT REALIZATION IS NOT OPTIMAL AND HAS BEEN ADDED TO COVER CASES OF DIFFERENT COMMON MAPPINGS AND TRANSFORMATIONS**
|
||||||
*
|
*
|
||||||
* @param v This value should be used to exclude from search the items with different [Value]s
|
* @param v This value should be used to exclude from search the items with different [Value]s
|
||||||
*/
|
*/
|
||||||
suspend fun keys(v: Value, pagination: Pagination, reversed: Boolean = false): PaginationResult<Key>
|
suspend fun keys(v: Value, pagination: Pagination, reversed: Boolean = false): PaginationResult<Key> {
|
||||||
|
return getAllWithNextPaging {
|
||||||
|
keys(it)
|
||||||
|
}.filter {
|
||||||
|
get(it) == v
|
||||||
|
}.paginate(pagination, reversed)
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return true if [key] is presented in current collection or false otherwise
|
* @return true if [key] is presented in current collection or false otherwise
|
||||||
|
@ -0,0 +1,10 @@
|
|||||||
|
package dev.inmo.micro_utils.repos.transforms.crud
|
||||||
|
|
||||||
|
import dev.inmo.micro_utils.repos.KeyValueRepo
|
||||||
|
import dev.inmo.micro_utils.repos.KeyValuesRepo
|
||||||
|
import dev.inmo.micro_utils.repos.ReadKeyValueRepo
|
||||||
|
import dev.inmo.micro_utils.repos.ReadKeyValuesRepo
|
||||||
|
import kotlin.js.JsName
|
||||||
|
import kotlin.jvm.JvmName
|
||||||
|
|
||||||
|
fun <K, V> ReadKeyValueRepo<K, V>.asReadCRUDRepo() = ReadCRUDFromKeyValueRepo(this)
|
@ -0,0 +1,20 @@
|
|||||||
|
package dev.inmo.micro_utils.repos.transforms.crud
|
||||||
|
|
||||||
|
import dev.inmo.micro_utils.pagination.Pagination
|
||||||
|
import dev.inmo.micro_utils.pagination.PaginationResult
|
||||||
|
import dev.inmo.micro_utils.repos.ReadCRUDRepo
|
||||||
|
import dev.inmo.micro_utils.repos.ReadKeyValueRepo
|
||||||
|
|
||||||
|
open class ReadCRUDFromKeyValueRepo<RegisteredType, IdType>(
|
||||||
|
protected open val original: ReadKeyValueRepo<IdType, RegisteredType>
|
||||||
|
) : ReadCRUDRepo<RegisteredType, IdType> {
|
||||||
|
override suspend fun contains(id: IdType): Boolean = original.contains(id)
|
||||||
|
|
||||||
|
override suspend fun count(): Long = original.count()
|
||||||
|
|
||||||
|
override suspend fun getByPagination(pagination: Pagination): PaginationResult<RegisteredType> = original.values(pagination)
|
||||||
|
|
||||||
|
override suspend fun getIdsByPagination(pagination: Pagination): PaginationResult<IdType> = original.keys(pagination)
|
||||||
|
|
||||||
|
override suspend fun getById(id: IdType): RegisteredType? = original.get(id)
|
||||||
|
}
|
@ -2,6 +2,7 @@ package dev.inmo.micro_utils.repos.transforms.kv
|
|||||||
|
|
||||||
import dev.inmo.micro_utils.repos.KeyValueRepo
|
import dev.inmo.micro_utils.repos.KeyValueRepo
|
||||||
import dev.inmo.micro_utils.repos.KeyValuesRepo
|
import dev.inmo.micro_utils.repos.KeyValuesRepo
|
||||||
|
import dev.inmo.micro_utils.repos.ReadCRUDRepo
|
||||||
import dev.inmo.micro_utils.repos.ReadKeyValueRepo
|
import dev.inmo.micro_utils.repos.ReadKeyValueRepo
|
||||||
import dev.inmo.micro_utils.repos.ReadKeyValuesRepo
|
import dev.inmo.micro_utils.repos.ReadKeyValuesRepo
|
||||||
import kotlin.js.JsName
|
import kotlin.js.JsName
|
||||||
@ -10,3 +11,5 @@ import kotlin.jvm.JvmName
|
|||||||
fun <K, V> ReadKeyValuesRepo<K, V>.asReadKeyValueRepo() = ReadKeyValueFromKeyValuesRepo(this)
|
fun <K, V> ReadKeyValuesRepo<K, V>.asReadKeyValueRepo() = ReadKeyValueFromKeyValuesRepo(this)
|
||||||
|
|
||||||
fun <K, V> KeyValuesRepo<K, V>.asKeyValueRepo() = KeyValueFromKeyValuesRepo(this)
|
fun <K, V> KeyValuesRepo<K, V>.asKeyValueRepo() = KeyValueFromKeyValuesRepo(this)
|
||||||
|
|
||||||
|
fun <K, V> ReadCRUDRepo<K, V>.asReadKeyValueRepo() = ReadKeyValueFromCRUDRepo(this)
|
||||||
|
@ -0,0 +1,46 @@
|
|||||||
|
package dev.inmo.micro_utils.repos.transforms.kv
|
||||||
|
|
||||||
|
import dev.inmo.micro_utils.pagination.FirstPagePagination
|
||||||
|
import dev.inmo.micro_utils.pagination.Pagination
|
||||||
|
import dev.inmo.micro_utils.pagination.PaginationResult
|
||||||
|
import dev.inmo.micro_utils.pagination.changeResults
|
||||||
|
import dev.inmo.micro_utils.pagination.changeResultsUnchecked
|
||||||
|
import dev.inmo.micro_utils.pagination.utils.doForAllWithNextPaging
|
||||||
|
import dev.inmo.micro_utils.pagination.utils.optionallyReverse
|
||||||
|
import dev.inmo.micro_utils.pagination.utils.paginate
|
||||||
|
import dev.inmo.micro_utils.repos.ReadCRUDRepo
|
||||||
|
import dev.inmo.micro_utils.repos.ReadKeyValueRepo
|
||||||
|
import dev.inmo.micro_utils.repos.ReadKeyValuesRepo
|
||||||
|
import dev.inmo.micro_utils.repos.transforms.kvs.ReadKeyValuesFromKeyValueRepo
|
||||||
|
import kotlin.jvm.JvmInline
|
||||||
|
|
||||||
|
@JvmInline
|
||||||
|
value class ReadKeyValueFromCRUDRepo<Key, Value>(
|
||||||
|
private val original: ReadCRUDRepo<Value, Key>
|
||||||
|
) : ReadKeyValueRepo<Key, Value> {
|
||||||
|
override suspend fun get(k: Key): Value? = original.getById(k)
|
||||||
|
|
||||||
|
override suspend fun values(pagination: Pagination, reversed: Boolean): PaginationResult<Value> = original.getByPagination(
|
||||||
|
pagination.optionallyReverse(count(), reversed)
|
||||||
|
).let {
|
||||||
|
if (reversed) {
|
||||||
|
it.changeResultsUnchecked(it.results.reversed())
|
||||||
|
} else {
|
||||||
|
it
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun keys(pagination: Pagination, reversed: Boolean): PaginationResult<Key> = original.getIdsByPagination(
|
||||||
|
pagination.optionallyReverse(count(), reversed)
|
||||||
|
).let {
|
||||||
|
if (reversed) {
|
||||||
|
it.changeResultsUnchecked(it.results.reversed())
|
||||||
|
} else {
|
||||||
|
it
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun count(): Long = original.count()
|
||||||
|
|
||||||
|
override suspend fun contains(key: Key): Boolean = original.contains(key)
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user