diff --git a/CHANGELOG.md b/CHANGELOG.md index 4c8f38e77cd..82782f96337 100644 --- a/CHANGELOG.md +++ b/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`: diff --git a/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/Clamp.kt b/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/Clamp.kt index 98eab69372c..7d6159ca5f7 100644 --- a/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/Clamp.kt +++ b/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/Clamp.kt @@ -1,10 +1,5 @@ package dev.inmo.micro_utils.common +@Deprecated("Redundant", ReplaceWith("coerceIn(min, max)")) @Suppress("NOTHING_TO_INLINE") -inline fun > T.clamp(min: T, max: T): T { - return when { - this < min -> min - this > max -> max - else -> this - } -} +inline fun > T.clamp(min: T, max: T): T = coerceIn(min, max) diff --git a/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/DiffUtils.kt b/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/DiffUtils.kt index 0c63e30c786..221237b4fee 100644 --- a/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/DiffUtils.kt +++ b/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/DiffUtils.kt @@ -2,6 +2,8 @@ package dev.inmo.micro_utils.common +import kotlin.jvm.JvmInline + private inline fun getObject( additional: MutableList, iterator: Iterator @@ -27,8 +29,8 @@ data class Diff internal constructor( private inline fun performChanges( potentialChanges: MutableList?, IndexedValue?>>, - additionalsInOld: MutableList, - additionalsInNew: MutableList, + additionsInOld: MutableList, + additionsInNew: MutableList, changedList: MutableList, IndexedValue>>, removedList: MutableList>, addedList: MutableList>, @@ -52,20 +54,20 @@ private inline fun 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 Iterable.calculateDiff( return Diff(removedObjects.toList(), changedObjects.toList(), addedObjects.toList()) } +inline fun Iterable.diff( + other: Iterable, + strictComparison: Boolean = false +): Diff = calculateDiff(other, strictComparison) inline fun Diff(old: Iterable, new: Iterable) = old.calculateDiff(new) inline fun StrictDiff(old: Iterable, new: Iterable) = old.calculateDiff(new, true) diff --git a/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/MPPFile.kt b/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/MPPFile.kt index c60eb0d6b80..8aada0ca9c7 100644 --- a/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/MPPFile.kt +++ b/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/MPPFile.kt @@ -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 } diff --git a/gradle.properties b/gradle.properties index 0bb6ea64dc4..2f8f59b988c 100644 --- a/gradle.properties +++ b/gradle.properties @@ -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 diff --git a/serialization/typed_serializer/src/commonMain/kotlin/dev/inmo/micro_utils/serialization/typed_serializer/TypedSerializer.kt b/serialization/typed_serializer/src/commonMain/kotlin/dev/inmo/micro_utils/serialization/typed_serializer/TypedSerializer.kt index 158d22994e5..3e6d9045f34 100644 --- a/serialization/typed_serializer/src/commonMain/kotlin/dev/inmo/micro_utils/serialization/typed_serializer/TypedSerializer.kt +++ b/serialization/typed_serializer/src/commonMain/kotlin/dev/inmo/micro_utils/serialization/typed_serializer/TypedSerializer.kt @@ -11,6 +11,7 @@ open class TypedSerializer( presetSerializers: Map> = emptyMap(), ) : KSerializer { protected val serializers = presetSerializers.toMutableMap() + @ExperimentalSerializationApi @InternalSerializationApi override val descriptor: SerialDescriptor = buildSerialDescriptor( "TypedSerializer", @@ -32,8 +33,9 @@ open class TypedSerializer( 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( } } + @ExperimentalSerializationApi @InternalSerializationApi protected open fun CompositeEncoder.encode(value: O) { encodeSerializableElement(descriptor, 1, value::class.serializer() as KSerializer, 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( } } +@InternalSerializationApi +operator fun TypedSerializer.plusAssign(kClass: KClass) { + include(kClass.simpleName!!, kClass.serializer()) +} + +@InternalSerializationApi +operator fun TypedSerializer.minusAssign(kClass: KClass) { + exclude(kClass.simpleName!!) +} + inline fun TypedSerializer( presetSerializers: Map> = emptyMap() ) = TypedSerializer(T::class, presetSerializers)