Functions to work with null and fixes

This commit is contained in:
Andrew Golovashevich 2024-11-10 21:57:34 +03:00
parent 4db4a3f667
commit 36ecb46b13
4 changed files with 175 additions and 2 deletions

View File

@ -1,4 +1,4 @@
@file:JvmName("IntConversions_Sign")
@file:JvmName("_IntConversions_Sign")
@file:Suppress("NOTHING_TO_INLINE")
package ru.landrafhomyak.kotlin.utilities

View File

@ -1,4 +1,4 @@
@file:JvmName("IntConversions_Size")
@file:JvmName("_IntConversions_Size")
@file:Suppress("NOTHING_TO_INLINE")
package ru.landrafhomyak.kotlin.utilities

View File

@ -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
}
}

View File

@ -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> T?.onNull(action: () -> Unit): T? {
contract {
callsInPlace(action, InvocationKind.AT_MOST_ONCE)
}
if (this == null)
action()
return this
}
@JvmName("mapNull")
public inline fun <T : R, R> T?.mapNull(action: () -> R): R {
contract {
callsInPlace(action, InvocationKind.AT_MOST_ONCE)
}
return this ?: action()
}
@JvmName("onNotNull")
public inline fun <T> T?.onNotNull(action: (T) -> Unit): T? {
contract {
callsInPlace(action, InvocationKind.AT_MOST_ONCE)
}
if (this != null)
action(this)
return this
}