jvmMain.com.caesarealabs.rpc4k.processor.utils.Utils.kt Maven / Gradle / Ivy
The newest version!
package com.caesarealabs.rpc4k.processor.utils
internal inline fun String.appendIf(condition: Boolean, toAppend: () -> String) = if (condition) this + toAppend() else this
internal inline fun Iterable.findDuplicate(byValue: (T) -> V): V? {
val keys = hashSetOf()
for (item in this) {
val key = byValue(item)
if (key in keys) {
return key
}
keys.add(key)
}
return null
}
internal inline fun List.appendIf(condition: Boolean, element: () -> T) = if (condition) this + element() else this
/**
* If any item is null, returns null. Otherwise, returns a non-null list.
*/
internal fun List.allOrNothing(): List? {
@Suppress("UNCHECKED_CAST")
if (any { it == null }) return null
else return this as List
}