mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2024-11-17 22:03:50 +00:00
commit
7fc5ee70e1
11
CHANGELOG.md
11
CHANGELOG.md
@ -1,5 +1,16 @@
|
||||
# Changelog
|
||||
|
||||
## 0.5.25
|
||||
|
||||
* `Versions`:
|
||||
* `UUID`: `0.3.0` -> `0.3.1`
|
||||
* `Common`:
|
||||
* New property `MPPFile#withoutSlashAtTheEnd`
|
||||
* Extension `clamp` has been deprecated
|
||||
* New extension `Iterable#diff`
|
||||
* `Serialization`:
|
||||
* New operators `TypedSerializer#plusAssign` and `TypedSerializer#minusAssign`
|
||||
|
||||
## 0.5.24
|
||||
|
||||
* `Versions`:
|
||||
|
@ -1,10 +1,5 @@
|
||||
package dev.inmo.micro_utils.common
|
||||
|
||||
@Deprecated("Redundant", ReplaceWith("coerceIn(min, max)"))
|
||||
@Suppress("NOTHING_TO_INLINE")
|
||||
inline fun <T : Comparable<T>> T.clamp(min: T, max: T): T {
|
||||
return when {
|
||||
this < min -> min
|
||||
this > max -> max
|
||||
else -> this
|
||||
}
|
||||
}
|
||||
inline fun <T : Comparable<T>> T.clamp(min: T, max: T): T = coerceIn(min, max)
|
||||
|
@ -2,6 +2,8 @@
|
||||
|
||||
package dev.inmo.micro_utils.common
|
||||
|
||||
import kotlin.jvm.JvmInline
|
||||
|
||||
private inline fun <T> getObject(
|
||||
additional: MutableList<T>,
|
||||
iterator: Iterator<T>
|
||||
@ -27,8 +29,8 @@ data class Diff<T> internal constructor(
|
||||
|
||||
private inline fun <T> performChanges(
|
||||
potentialChanges: MutableList<Pair<IndexedValue<T>?, IndexedValue<T>?>>,
|
||||
additionalsInOld: MutableList<T>,
|
||||
additionalsInNew: MutableList<T>,
|
||||
additionsInOld: MutableList<T>,
|
||||
additionsInNew: MutableList<T>,
|
||||
changedList: MutableList<Pair<IndexedValue<T>, IndexedValue<T>>>,
|
||||
removedList: MutableList<IndexedValue<T>>,
|
||||
addedList: MutableList<IndexedValue<T>>,
|
||||
@ -52,20 +54,20 @@ private inline fun <T> performChanges(
|
||||
newPotentials.first().second ?.let { addedList.add(it) }
|
||||
newPotentials.drop(1).take(newPotentials.size - 2).forEach { (oldOne, newOne) ->
|
||||
addedList.add(newOne!!)
|
||||
oldOne ?.let { additionalsInOld.add(oldOne.value) }
|
||||
oldOne ?.let { additionsInOld.add(oldOne.value) }
|
||||
}
|
||||
if (newPotentials.size > 1) {
|
||||
newPotentials.last().first ?.value ?.let { additionalsInOld.add(it) }
|
||||
newPotentials.last().first ?.value ?.let { additionsInOld.add(it) }
|
||||
}
|
||||
}
|
||||
newOneEqualToOldObject -> {
|
||||
newPotentials.first().first ?.let { removedList.add(it) }
|
||||
newPotentials.drop(1).take(newPotentials.size - 2).forEach { (oldOne, newOne) ->
|
||||
removedList.add(oldOne!!)
|
||||
newOne ?.let { additionalsInNew.add(newOne.value) }
|
||||
newOne ?.let { additionsInNew.add(newOne.value) }
|
||||
}
|
||||
if (newPotentials.size > 1) {
|
||||
newPotentials.last().second ?.value ?.let { additionalsInNew.add(it) }
|
||||
newPotentials.last().second ?.value ?.let { additionsInNew.add(it) }
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -139,6 +141,10 @@ fun <T> Iterable<T>.calculateDiff(
|
||||
|
||||
return Diff(removedObjects.toList(), changedObjects.toList(), addedObjects.toList())
|
||||
}
|
||||
inline fun <T> Iterable<T>.diff(
|
||||
other: Iterable<T>,
|
||||
strictComparison: Boolean = false
|
||||
): Diff<T> = calculateDiff(other, strictComparison)
|
||||
|
||||
inline fun <T> Diff(old: Iterable<T>, new: Iterable<T>) = old.calculateDiff(new)
|
||||
inline fun <T> StrictDiff(old: Iterable<T>, new: Iterable<T>) = old.calculateDiff(new, true)
|
||||
|
@ -7,7 +7,7 @@ import kotlin.jvm.JvmInline
|
||||
@JvmInline
|
||||
value class FileName(val string: String) {
|
||||
val name: String
|
||||
get() = string.takeLastWhile { it != '/' }
|
||||
get() = withoutSlashAtTheEnd.takeLastWhile { it != '/' }
|
||||
val extension: String
|
||||
get() = name.takeLastWhile { it != '.' }
|
||||
val nameWithoutExtension: String
|
||||
@ -17,6 +17,8 @@ value class FileName(val string: String) {
|
||||
filename.substring(0, it)
|
||||
} ?: filename
|
||||
}
|
||||
val withoutSlashAtTheEnd: String
|
||||
get() = string.dropLastWhile { it == '/' }
|
||||
override fun toString(): String = string
|
||||
}
|
||||
|
||||
|
@ -18,7 +18,7 @@ klockVersion=2.4.1
|
||||
|
||||
github_release_plugin_version=2.2.12
|
||||
|
||||
uuidVersion=0.3.0
|
||||
uuidVersion=0.3.1
|
||||
|
||||
# ANDROID
|
||||
|
||||
@ -45,5 +45,5 @@ dokka_version=1.5.0
|
||||
# Project data
|
||||
|
||||
group=dev.inmo
|
||||
version=0.5.24
|
||||
android_code_version=65
|
||||
version=0.5.25
|
||||
android_code_version=66
|
||||
|
@ -11,6 +11,7 @@ open class TypedSerializer<T : Any>(
|
||||
presetSerializers: Map<String, KSerializer<out T>> = emptyMap(),
|
||||
) : KSerializer<T> {
|
||||
protected val serializers = presetSerializers.toMutableMap()
|
||||
@ExperimentalSerializationApi
|
||||
@InternalSerializationApi
|
||||
override val descriptor: SerialDescriptor = buildSerialDescriptor(
|
||||
"TypedSerializer",
|
||||
@ -32,8 +33,9 @@ open class TypedSerializer<T : Any>(
|
||||
element("value", ContextualSerializer(kClass).descriptor)
|
||||
}
|
||||
|
||||
@ExperimentalSerializationApi
|
||||
@InternalSerializationApi
|
||||
open override fun deserialize(decoder: Decoder): T {
|
||||
override fun deserialize(decoder: Decoder): T {
|
||||
return decoder.decodeStructure(descriptor) {
|
||||
var type: String? = null
|
||||
lateinit var result: T
|
||||
@ -56,13 +58,15 @@ open class TypedSerializer<T : Any>(
|
||||
}
|
||||
}
|
||||
|
||||
@ExperimentalSerializationApi
|
||||
@InternalSerializationApi
|
||||
protected open fun <O: T> CompositeEncoder.encode(value: O) {
|
||||
encodeSerializableElement(descriptor, 1, value::class.serializer() as KSerializer<O>, value)
|
||||
}
|
||||
|
||||
@ExperimentalSerializationApi
|
||||
@InternalSerializationApi
|
||||
open override fun serialize(encoder: Encoder, value: T) {
|
||||
override fun serialize(encoder: Encoder, value: T) {
|
||||
encoder.encodeStructure(descriptor) {
|
||||
val valueSerializer = value::class.serializer()
|
||||
val type = serializers.keys.first { serializers[it] == valueSerializer }
|
||||
@ -81,6 +85,16 @@ open class TypedSerializer<T : Any>(
|
||||
}
|
||||
}
|
||||
|
||||
@InternalSerializationApi
|
||||
operator fun <T : Any> TypedSerializer<T>.plusAssign(kClass: KClass<T>) {
|
||||
include(kClass.simpleName!!, kClass.serializer())
|
||||
}
|
||||
|
||||
@InternalSerializationApi
|
||||
operator fun <T : Any> TypedSerializer<T>.minusAssign(kClass: KClass<T>) {
|
||||
exclude(kClass.simpleName!!)
|
||||
}
|
||||
|
||||
inline fun <reified T : Any> TypedSerializer(
|
||||
presetSerializers: Map<String, KSerializer<out T>> = emptyMap()
|
||||
) = TypedSerializer(T::class, presetSerializers)
|
||||
|
Loading…
Reference in New Issue
Block a user