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
|
# 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
|
## 0.5.24
|
||||||
|
|
||||||
* `Versions`:
|
* `Versions`:
|
||||||
|
@ -1,10 +1,5 @@
|
|||||||
package dev.inmo.micro_utils.common
|
package dev.inmo.micro_utils.common
|
||||||
|
|
||||||
|
@Deprecated("Redundant", ReplaceWith("coerceIn(min, max)"))
|
||||||
@Suppress("NOTHING_TO_INLINE")
|
@Suppress("NOTHING_TO_INLINE")
|
||||||
inline fun <T : Comparable<T>> T.clamp(min: T, max: T): T {
|
inline fun <T : Comparable<T>> T.clamp(min: T, max: T): T = coerceIn(min, max)
|
||||||
return when {
|
|
||||||
this < min -> min
|
|
||||||
this > max -> max
|
|
||||||
else -> this
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
package dev.inmo.micro_utils.common
|
package dev.inmo.micro_utils.common
|
||||||
|
|
||||||
|
import kotlin.jvm.JvmInline
|
||||||
|
|
||||||
private inline fun <T> getObject(
|
private inline fun <T> getObject(
|
||||||
additional: MutableList<T>,
|
additional: MutableList<T>,
|
||||||
iterator: Iterator<T>
|
iterator: Iterator<T>
|
||||||
@ -27,8 +29,8 @@ data class Diff<T> internal constructor(
|
|||||||
|
|
||||||
private inline fun <T> performChanges(
|
private inline fun <T> performChanges(
|
||||||
potentialChanges: MutableList<Pair<IndexedValue<T>?, IndexedValue<T>?>>,
|
potentialChanges: MutableList<Pair<IndexedValue<T>?, IndexedValue<T>?>>,
|
||||||
additionalsInOld: MutableList<T>,
|
additionsInOld: MutableList<T>,
|
||||||
additionalsInNew: MutableList<T>,
|
additionsInNew: MutableList<T>,
|
||||||
changedList: MutableList<Pair<IndexedValue<T>, IndexedValue<T>>>,
|
changedList: MutableList<Pair<IndexedValue<T>, IndexedValue<T>>>,
|
||||||
removedList: MutableList<IndexedValue<T>>,
|
removedList: MutableList<IndexedValue<T>>,
|
||||||
addedList: MutableList<IndexedValue<T>>,
|
addedList: MutableList<IndexedValue<T>>,
|
||||||
@ -52,20 +54,20 @@ private inline fun <T> performChanges(
|
|||||||
newPotentials.first().second ?.let { addedList.add(it) }
|
newPotentials.first().second ?.let { addedList.add(it) }
|
||||||
newPotentials.drop(1).take(newPotentials.size - 2).forEach { (oldOne, newOne) ->
|
newPotentials.drop(1).take(newPotentials.size - 2).forEach { (oldOne, newOne) ->
|
||||||
addedList.add(newOne!!)
|
addedList.add(newOne!!)
|
||||||
oldOne ?.let { additionalsInOld.add(oldOne.value) }
|
oldOne ?.let { additionsInOld.add(oldOne.value) }
|
||||||
}
|
}
|
||||||
if (newPotentials.size > 1) {
|
if (newPotentials.size > 1) {
|
||||||
newPotentials.last().first ?.value ?.let { additionalsInOld.add(it) }
|
newPotentials.last().first ?.value ?.let { additionsInOld.add(it) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
newOneEqualToOldObject -> {
|
newOneEqualToOldObject -> {
|
||||||
newPotentials.first().first ?.let { removedList.add(it) }
|
newPotentials.first().first ?.let { removedList.add(it) }
|
||||||
newPotentials.drop(1).take(newPotentials.size - 2).forEach { (oldOne, newOne) ->
|
newPotentials.drop(1).take(newPotentials.size - 2).forEach { (oldOne, newOne) ->
|
||||||
removedList.add(oldOne!!)
|
removedList.add(oldOne!!)
|
||||||
newOne ?.let { additionalsInNew.add(newOne.value) }
|
newOne ?.let { additionsInNew.add(newOne.value) }
|
||||||
}
|
}
|
||||||
if (newPotentials.size > 1) {
|
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())
|
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> Diff(old: Iterable<T>, new: Iterable<T>) = old.calculateDiff(new)
|
||||||
inline fun <T> StrictDiff(old: Iterable<T>, new: Iterable<T>) = old.calculateDiff(new, true)
|
inline fun <T> StrictDiff(old: Iterable<T>, new: Iterable<T>) = old.calculateDiff(new, true)
|
||||||
|
@ -7,7 +7,7 @@ import kotlin.jvm.JvmInline
|
|||||||
@JvmInline
|
@JvmInline
|
||||||
value class FileName(val string: String) {
|
value class FileName(val string: String) {
|
||||||
val name: String
|
val name: String
|
||||||
get() = string.takeLastWhile { it != '/' }
|
get() = withoutSlashAtTheEnd.takeLastWhile { it != '/' }
|
||||||
val extension: String
|
val extension: String
|
||||||
get() = name.takeLastWhile { it != '.' }
|
get() = name.takeLastWhile { it != '.' }
|
||||||
val nameWithoutExtension: String
|
val nameWithoutExtension: String
|
||||||
@ -17,6 +17,8 @@ value class FileName(val string: String) {
|
|||||||
filename.substring(0, it)
|
filename.substring(0, it)
|
||||||
} ?: filename
|
} ?: filename
|
||||||
}
|
}
|
||||||
|
val withoutSlashAtTheEnd: String
|
||||||
|
get() = string.dropLastWhile { it == '/' }
|
||||||
override fun toString(): String = string
|
override fun toString(): String = string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,7 +18,7 @@ klockVersion=2.4.1
|
|||||||
|
|
||||||
github_release_plugin_version=2.2.12
|
github_release_plugin_version=2.2.12
|
||||||
|
|
||||||
uuidVersion=0.3.0
|
uuidVersion=0.3.1
|
||||||
|
|
||||||
# ANDROID
|
# ANDROID
|
||||||
|
|
||||||
@ -45,5 +45,5 @@ dokka_version=1.5.0
|
|||||||
# Project data
|
# Project data
|
||||||
|
|
||||||
group=dev.inmo
|
group=dev.inmo
|
||||||
version=0.5.24
|
version=0.5.25
|
||||||
android_code_version=65
|
android_code_version=66
|
||||||
|
@ -11,6 +11,7 @@ open class TypedSerializer<T : Any>(
|
|||||||
presetSerializers: Map<String, KSerializer<out T>> = emptyMap(),
|
presetSerializers: Map<String, KSerializer<out T>> = emptyMap(),
|
||||||
) : KSerializer<T> {
|
) : KSerializer<T> {
|
||||||
protected val serializers = presetSerializers.toMutableMap()
|
protected val serializers = presetSerializers.toMutableMap()
|
||||||
|
@ExperimentalSerializationApi
|
||||||
@InternalSerializationApi
|
@InternalSerializationApi
|
||||||
override val descriptor: SerialDescriptor = buildSerialDescriptor(
|
override val descriptor: SerialDescriptor = buildSerialDescriptor(
|
||||||
"TypedSerializer",
|
"TypedSerializer",
|
||||||
@ -32,8 +33,9 @@ open class TypedSerializer<T : Any>(
|
|||||||
element("value", ContextualSerializer(kClass).descriptor)
|
element("value", ContextualSerializer(kClass).descriptor)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ExperimentalSerializationApi
|
||||||
@InternalSerializationApi
|
@InternalSerializationApi
|
||||||
open override fun deserialize(decoder: Decoder): T {
|
override fun deserialize(decoder: Decoder): T {
|
||||||
return decoder.decodeStructure(descriptor) {
|
return decoder.decodeStructure(descriptor) {
|
||||||
var type: String? = null
|
var type: String? = null
|
||||||
lateinit var result: T
|
lateinit var result: T
|
||||||
@ -56,13 +58,15 @@ open class TypedSerializer<T : Any>(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ExperimentalSerializationApi
|
||||||
@InternalSerializationApi
|
@InternalSerializationApi
|
||||||
protected open fun <O: T> CompositeEncoder.encode(value: O) {
|
protected open fun <O: T> CompositeEncoder.encode(value: O) {
|
||||||
encodeSerializableElement(descriptor, 1, value::class.serializer() as KSerializer<O>, value)
|
encodeSerializableElement(descriptor, 1, value::class.serializer() as KSerializer<O>, value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ExperimentalSerializationApi
|
||||||
@InternalSerializationApi
|
@InternalSerializationApi
|
||||||
open override fun serialize(encoder: Encoder, value: T) {
|
override fun serialize(encoder: Encoder, value: T) {
|
||||||
encoder.encodeStructure(descriptor) {
|
encoder.encodeStructure(descriptor) {
|
||||||
val valueSerializer = value::class.serializer()
|
val valueSerializer = value::class.serializer()
|
||||||
val type = serializers.keys.first { serializers[it] == valueSerializer }
|
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(
|
inline fun <reified T : Any> TypedSerializer(
|
||||||
presetSerializers: Map<String, KSerializer<out T>> = emptyMap()
|
presetSerializers: Map<String, KSerializer<out T>> = emptyMap()
|
||||||
) = TypedSerializer(T::class, presetSerializers)
|
) = TypedSerializer(T::class, presetSerializers)
|
||||||
|
Loading…
Reference in New Issue
Block a user