Skip to content

Instantly share code, notes, and snippets.

@bitsycore
Created October 8, 2024 09:19
Show Gist options
  • Save bitsycore/e514b06e0a8373ba0eedbeb8a17a3d19 to your computer and use it in GitHub Desktop.
Save bitsycore/e514b06e0a8373ba0eedbeb8a17a3d19 to your computer and use it in GitHub Desktop.
@file:Suppress("NOTHING_TO_INLINE")
// --------------------------
// MARK: IF NOT NULL
// --------------------------
class EndNotNullResult<T>(val result: NotNullResult<T>)
open class NotNullResultValue<T>(override var resultPredicate: Boolean, open val value: T) : NotNullResult<T>(resultPredicate)
open class NotNullResult<T>(open var resultPredicate: Boolean)
// IF NOT NULL FINAL
inline fun <T> T?.isNotNull(returnNonNullObject: (T) -> Unit): EndNotNullResult<T> {
val result = isNotNull()
if (result is NotNullResultValue) returnNonNullObject(result.value)
return EndNotNullResult(result)
}
// IF NOT NULL CHAIN
inline fun <T> T?.isNotNull(): NotNullResult<T> =
if (this != null) NotNullResultValue(true, this)
else NotNullResult(false)
// AND FINAL
inline fun <T, V> NotNullResult<T>.and(
predicate: V,
returnNonNullObject: (T) -> Unit
): EndNotNullResult<T> = and({ predicate }, returnNonNullObject)
// AND FINAL W/ LAMBDA PREDICATE
inline fun <T, V> NotNullResult<T>.and(returnPredicate: T.() -> V, returnNonNullObject: (T) -> Unit): EndNotNullResult<T> {
val result = and(returnPredicate)
if (result is NotNullResultValue && result.resultPredicate) returnNonNullObject(result.value)
return EndNotNullResult(result)
}
// AND CHAIN
inline fun < T, V> NotNullResult<T>.and(predicate: V): NotNullResult<T> = and { predicate }
// AND CHAIN W/ LAMBDA PREDICATE
inline fun <T, V> NotNullResult<T>.and(returnPredicate: T.() -> V): NotNullResult<T> {
if (this.resultPredicate && this is NotNullResultValue && this.value.returnPredicate().predicateAsBoolean()) {
return this
} else {
this.resultPredicate = false
return this
}
}
// ELSE
inline fun <T> EndNotNullResult<T>.otherwise(function: () -> Unit) {
if (!this.result.resultPredicate) function()
}
// Removed the or for now because they kind of break the condition chain
// Or Final
//inline fun<T> IfNotNullResult<T>.or(predicate: Boolean, function: (T) -> Unit) : IfNotNullResultEnd<T> {
// if ((this.resultPredicate || predicate) && this is IfNotNullResultValue) {
// function(this.value)
// this.resultPredicate = true
// } else {
// this.resultPredicate = false
// }
//
// return IfNotNullResultEnd(this)
//}
// Or Chainable
//inline fun<T> IfNotNullResult<T>.or(predicate: Boolean) : IfNotNullResult<T> {
// return if (this.resultPredicate || predicate) {
// this.resultPredicate = true
// this
// } else {
// this.resultPredicate = false
// this
// }
//}
// Else Do
//inline fun<T> IfNotNullResultEnd<T>.butNull(function: () -> Unit): IfNotNullResultEnd<T> {
// if (this.result.resultPredicate) {
// function()
// this.result.resultPredicate = true
// }
// return this
//}
// --------------------------
// MARK: IF NULL
// --------------------------
class EndNullResult<T>(val result: NullResult<T>)
class NullResultValue<T>(override var resultPredicate: Boolean, val value: T) : NullResult<T>(resultPredicate)
open class NullResult<T>(open var resultPredicate: Boolean)
// IF NULL FINAL
inline fun <T> T?.isNull(function: () -> Unit): EndNullResult<T> {
val result = isNull()
if (result.resultPredicate) function()
return EndNullResult(result)
}
// IF NULL CHAIN
inline fun <T> T?.isNull(): NullResult<T> {
return if (this == null) {
NullResult(true)
} else {
NullResultValue(false, this)
}
}
// AND FINAL
inline fun <T, V> NullResult<T>.and(predicate: V, onChainTrue: () -> Unit): EndNullResult<T> {
val result = and(predicate)
if (result.resultPredicate) onChainTrue()
return EndNullResult(result)
}
// AND CHAIN
inline fun <T, V> NullResult<T>.and(predicate: V): NullResult<T> {
if (this.resultPredicate && predicate.predicateAsBoolean()) {
this.resultPredicate = true
return this
} else {
this.resultPredicate = false
return this
}
}
// ELSE
inline fun <T> EndNullResult<T>.otherwise(onChainFalse: () -> Unit) {
if (!this.result.resultPredicate) onChainFalse()
}
// Removed the or for now because they kind of break the condition chain
// Or Final
//inline fun<T> IfNullResult<T>.or(predicate: Boolean, function: () -> Unit) : IfNullResultEnd<T> {
// if(this.resultPredicate || predicate) {
// function()
// this.resultPredicate = true
// return IfNullResultEnd(this)
// }
// else {
// this.resultPredicate = false
// return IfNullResultEnd(this)
// }
//}
// Or Chainable
//inline fun<T> IfNullResult<T>.or(predicate: Boolean) : IfNullResult<T> {
// if(this.resultPredicate || predicate) {
// this.resultPredicate = true
// return this
// }
// else {
// this.resultPredicate = false
// return this
// }
//}
// Else where the condition is met but reasult is Not Null
//inline fun<T> IfNullResultEnd<T>.butNotNull(function: (T) -> Unit): IfNullResultEnd<T> {
// if (!this.result.resultPredicate && this.result is IfNullResultValue) {
// function(this.result.value)
// this.result.resultPredicate = true
// }
// return this
//}
// Utils
fun Any?.predicateAsBoolean(): Boolean {
return when (this) {
null -> false
is Boolean -> this
is NotNullResult<*> -> this.resultPredicate
is NullResult<*> -> this.resultPredicate
else -> false
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment