From 36ecb46b13e771c04e43f4f46cb309b412ab6235 Mon Sep 17 00:00:00 2001 From: Andrew Golovashevich Date: Sun, 10 Nov 2024 21:57:34 +0300 Subject: [PATCH] Functions to work with null and fixes --- .../kotlin/utilities/int_conversions_sign.kt | 2 +- .../kotlin/utilities/int_conversions_size.kt | 2 +- .../kotlin/utilities/int_variables.kt | 131 ++++++++++++++++++ .../landrafhomyak/kotlin/utilities/nulls.kt | 42 ++++++ 4 files changed, 175 insertions(+), 2 deletions(-) create mode 100644 src/commonMain/kotlin/ru/landrafhomyak/kotlin/utilities/nulls.kt diff --git a/src/commonMain/kotlin/ru/landrafhomyak/kotlin/utilities/int_conversions_sign.kt b/src/commonMain/kotlin/ru/landrafhomyak/kotlin/utilities/int_conversions_sign.kt index 0e64258..f8d858a 100644 --- a/src/commonMain/kotlin/ru/landrafhomyak/kotlin/utilities/int_conversions_sign.kt +++ b/src/commonMain/kotlin/ru/landrafhomyak/kotlin/utilities/int_conversions_sign.kt @@ -1,4 +1,4 @@ -@file:JvmName("IntConversions_Sign") +@file:JvmName("_IntConversions_Sign") @file:Suppress("NOTHING_TO_INLINE") package ru.landrafhomyak.kotlin.utilities diff --git a/src/commonMain/kotlin/ru/landrafhomyak/kotlin/utilities/int_conversions_size.kt b/src/commonMain/kotlin/ru/landrafhomyak/kotlin/utilities/int_conversions_size.kt index ad8a5d4..63a8afb 100644 --- a/src/commonMain/kotlin/ru/landrafhomyak/kotlin/utilities/int_conversions_size.kt +++ b/src/commonMain/kotlin/ru/landrafhomyak/kotlin/utilities/int_conversions_size.kt @@ -1,4 +1,4 @@ -@file:JvmName("IntConversions_Size") +@file:JvmName("_IntConversions_Size") @file:Suppress("NOTHING_TO_INLINE") package ru.landrafhomyak.kotlin.utilities diff --git a/src/commonMain/kotlin/ru/landrafhomyak/kotlin/utilities/int_variables.kt b/src/commonMain/kotlin/ru/landrafhomyak/kotlin/utilities/int_variables.kt index 3ff0841..f5bc4c2 100644 --- a/src/commonMain/kotlin/ru/landrafhomyak/kotlin/utilities/int_variables.kt +++ b/src/commonMain/kotlin/ru/landrafhomyak/kotlin/utilities/int_variables.kt @@ -1,6 +1,9 @@ +@file:JvmName("_IntVariables") + package ru.landrafhomyak.kotlin.utilities import kotlin.jvm.JvmField +import kotlin.jvm.JvmName public class ByteVariable( @Suppress("MemberVisibilityCanBePrivate") @@ -8,14 +11,30 @@ public class ByteVariable( public var value: Byte ) { @Suppress("MemberVisibilityCanBePrivate") + @JvmName("getAndSet") public fun getAndSet(newValue: Byte): Byte { val oldValue = this.value this.value = newValue return oldValue } + @JvmName("getAndAdd") public fun getAndAdd(d: Byte): Byte = this.getAndSet((this.value + d).trimToByte()) + + @JvmName("getAndSub") public fun getAndSub(d: Byte): Byte = this.getAndSet((this.value - d).trimToByte()) + + @JvmName("addAndGet") + public fun addAndGet(d: Byte): Byte { + this.value = (this.value + d).trimToByte() + return this.value + } + + @JvmName("subAndGet") + public fun subAndGet(d: Byte): Byte { + this.value = (this.value - d).trimToByte() + return this.value + } } public class UByteVariable( @@ -23,14 +42,30 @@ public class UByteVariable( public var value: UByte ) { @Suppress("MemberVisibilityCanBePrivate") + @JvmName("getAndSet") public fun getAndSet(newValue: UByte): UByte { val oldValue = this.value this.value = newValue return oldValue } + @JvmName("getAndAdd") public fun getAndAdd(d: UByte): UByte = this.getAndSet((this.value + d).trimToUByte()) + + @JvmName("getAndSub") public fun getAndSub(d: UByte): UByte = this.getAndSet((this.value - d).trimToUByte()) + + @JvmName("addAndGet") + public fun addAndGet(d: UByte): UByte { + this.value = (this.value + d).trimToUByte() + return this.value + } + + @JvmName("subAndGet") + public fun subAndGet(d: UByte): UByte { + this.value = (this.value - d).trimToUByte() + return this.value + } } public class ShortVariable( @@ -39,14 +74,30 @@ public class ShortVariable( public var value: Short ) { @Suppress("MemberVisibilityCanBePrivate") + @JvmName("getAndSet") public fun getAndSet(newValue: Short): Short { val oldValue = this.value this.value = newValue return oldValue } + @JvmName("getAndAdd") public fun getAndAdd(d: Short): Short = this.getAndSet((this.value + d).trimToShort()) + + @JvmName("getAndSub") public fun getAndSub(d: Short): Short = this.getAndSet((this.value - d).trimToShort()) + + @JvmName("addAndGet") + public fun addAndGet(d: Short): Short { + this.value = (this.value + d).trimToShort() + return this.value + } + + @JvmName("subAndGet") + public fun subAndGet(d: Short): Short { + this.value = (this.value - d).trimToShort() + return this.value + } } public class UShortVariable( @@ -54,14 +105,30 @@ public class UShortVariable( public var value: UShort ) { @Suppress("MemberVisibilityCanBePrivate") + @JvmName("getAndSet") public fun getAndSet(newValue: UShort): UShort { val oldValue = this.value this.value = newValue return oldValue } + @JvmName("getAndAdd") public fun getAndAdd(d: UShort): UShort = this.getAndSet((this.value + d).trimToUShort()) + + @JvmName("getAndSub") public fun getAndSub(d: UShort): UShort = this.getAndSet((this.value - d).trimToUShort()) + + @JvmName("addAndGet") + public fun addAndGet(d: UShort): UShort { + this.value = (this.value + d).trimToUShort() + return this.value + } + + @JvmName("subAndGet") + public fun subAndGet(d: UShort): UShort { + this.value = (this.value - d).trimToUShort() + return this.value + } } public class IntVariable( @@ -69,14 +136,30 @@ public class IntVariable( @JvmField public var value: Int ) { @Suppress("MemberVisibilityCanBePrivate") + @JvmName("getAndSet") public fun getAndSet(newValue: Int): Int { val oldValue = this.value this.value = newValue return oldValue } + @JvmName("getAndAdd") public fun getAndAdd(d: Int): Int = this.getAndSet(this.value + d) + + @JvmName("getAndSub") public fun getAndSub(d: Int): Int = this.getAndSet(this.value - d) + + @JvmName("addAndGet") + public fun addAndGet(d: Int): Int { + this.value += d + return this.value + } + + @JvmName("subAndGet") + public fun subAndGet(d: Int): Int { + this.value -= d + return this.value + } } public class UIntVariable( @@ -84,14 +167,30 @@ public class UIntVariable( public var value: UInt ) { @Suppress("MemberVisibilityCanBePrivate") + @JvmName("getAndSet") public fun getAndSet(newValue: UInt): UInt { val oldValue = this.value this.value = newValue return oldValue } + @JvmName("getAndAdd") public fun getAndAdd(d: UInt): UInt = this.getAndSet(this.value + d) + + @JvmName("getAndSub") public fun getAndSub(d: UInt): UInt = this.getAndSet(this.value - d) + + @JvmName("addAndGet") + public fun addAndGet(d: UInt): UInt { + this.value += d + return this.value + } + + @JvmName("subAndGet") + public fun subAndGet(d: UInt): UInt { + this.value -= d + return this.value + } } public class LongVariable( @@ -100,14 +199,30 @@ public class LongVariable( public var value: Long ) { @Suppress("MemberVisibilityCanBePrivate") + @JvmName("getAndSet") public fun getAndSet(newValue: Long): Long { val oldValue = this.value this.value = newValue return oldValue } + @JvmName("getAndAdd") public fun getAndAdd(d: Long): Long = this.getAndSet(this.value + d) + + @JvmName("getAndSub") public fun getAndSub(d: Long): Long = this.getAndSet(this.value - d) + + @JvmName("addAndGet") + public fun addAndGet(d: Long): Long { + this.value += d + return this.value + } + + @JvmName("subAndGet") + public fun subAndGet(d: Long): Long { + this.value -= d + return this.value + } } public class ULongVariable( @@ -115,12 +230,28 @@ public class ULongVariable( public var value: ULong ) { @Suppress("MemberVisibilityCanBePrivate") + @JvmName("getAndSet") public fun getAndSet(newValue: ULong): ULong { val oldValue = this.value this.value = newValue return oldValue } + @JvmName("getAndAdd") public fun getAndAdd(d: ULong): ULong = this.getAndSet(this.value + d) + + @JvmName("getAndSub") public fun getAndSub(d: ULong): ULong = this.getAndSet(this.value - d) + + @JvmName("addAndGet") + public fun addAndGet(d: ULong): ULong { + this.value += d + return this.value + } + + @JvmName("subAndGet") + public fun subAndGet(d: ULong): ULong { + this.value -= d + return this.value + } } \ No newline at end of file diff --git a/src/commonMain/kotlin/ru/landrafhomyak/kotlin/utilities/nulls.kt b/src/commonMain/kotlin/ru/landrafhomyak/kotlin/utilities/nulls.kt new file mode 100644 index 0000000..090860f --- /dev/null +++ b/src/commonMain/kotlin/ru/landrafhomyak/kotlin/utilities/nulls.kt @@ -0,0 +1,42 @@ +@file:JvmName("_Nulls") + +package ru.landrafhomyak.kotlin.utilities + +import kotlin.contracts.InvocationKind +import kotlin.contracts.contract +import kotlin.jvm.JvmName + +@JvmName("onNull") +public inline fun T?.onNull(action: () -> Unit): T? { + contract { + callsInPlace(action, InvocationKind.AT_MOST_ONCE) + } + + if (this == null) + action() + + return this +} + + +@JvmName("mapNull") +public inline fun T?.mapNull(action: () -> R): R { + contract { + callsInPlace(action, InvocationKind.AT_MOST_ONCE) + } + + return this ?: action() +} + + +@JvmName("onNotNull") +public inline fun T?.onNotNull(action: (T) -> Unit): T? { + contract { + callsInPlace(action, InvocationKind.AT_MOST_ONCE) + } + + if (this != null) + action(this) + + return this +} \ No newline at end of file