From 19db5ece8677c0ce0c52819efe22a5abcc146c4c Mon Sep 17 00:00:00 2001 From: Elifarley Cruz Date: Thu, 2 Jan 2020 19:11:56 +0100 Subject: [PATCH] Some more functions --- base/src/main/kotlin/klib/base/FunKit.kt | 32 ++++++++++++++++++++ base/src/main/kotlin/klib/base/LoggingKit.kt | 10 ++++++ base/src/main/kotlin/klib/base/MiscKit.kt | 24 --------------- base/src/main/kotlin/klib/base/StringKit.kt | 18 +++++++++++ 4 files changed, 60 insertions(+), 24 deletions(-) create mode 100644 base/src/main/kotlin/klib/base/FunKit.kt diff --git a/base/src/main/kotlin/klib/base/FunKit.kt b/base/src/main/kotlin/klib/base/FunKit.kt new file mode 100644 index 0000000..30e7887 --- /dev/null +++ b/base/src/main/kotlin/klib/base/FunKit.kt @@ -0,0 +1,32 @@ +package klib.base + +/** + * Created by elifarley on 22/11/16. + */ + +inline fun Any?.nullIf(nullValue: Any) = this.takeUnless { this == nullValue } + +/** + * Calls the specified function [block] with no argument and always returns `Unit`. + */ +inline fun unit(block: () -> Any?) { + block() +} + +/** + * Calls the specified function [block] with `this` value as its argument and always returns `Unit`. + */ +inline fun T.unit(block: (T) -> Any?) { + block(this) +} + +/** + * Can be used to transform a `when` receiver into an expression, making it **exhaustive**. + * @return `Unit` + */ +val Any?.unit inline get() = Unit + +inline operator fun Boolean?.rem(block: () -> T): T? = this?.let { if (this) block() else null } + +inline infix operator fun ((A) -> B).rem(crossinline other: (B) -> C): (A) -> C = + { other(this(it)) } diff --git a/base/src/main/kotlin/klib/base/LoggingKit.kt b/base/src/main/kotlin/klib/base/LoggingKit.kt index daa4713..3a2feae 100644 --- a/base/src/main/kotlin/klib/base/LoggingKit.kt +++ b/base/src/main/kotlin/klib/base/LoggingKit.kt @@ -40,6 +40,16 @@ abstract class WithLogging { val log: Logger by lazyLogger() } +/** + * Executes the given [block], logs elapsed time and returns whatever [block] returned. + */ +inline fun Logger.debugTime(logMessage: String? = null, block: () -> T): T = System.nanoTime().let { start -> + block().also { + if (isDebugEnabled) + debug("{}; ELAPSED {} ms", logMessage, 1e3 * (System.nanoTime() - start)) + } +} + class MDCCloseable : Closeable { private val keys = mutableSetOf() diff --git a/base/src/main/kotlin/klib/base/MiscKit.kt b/base/src/main/kotlin/klib/base/MiscKit.kt index 17a0dca..82ad2f6 100644 --- a/base/src/main/kotlin/klib/base/MiscKit.kt +++ b/base/src/main/kotlin/klib/base/MiscKit.kt @@ -10,32 +10,8 @@ import java.util.concurrent.ThreadLocalRandom * Created by elifarley on 22/11/16. */ -inline fun Any?.nullIf(nullValue: Any) = if (this == nullValue) null else this - -/** - * Calls the specified function [block] with no argument and always returns `Unit`. - */ -inline fun unit(block: () -> Any?) { - block() -} - -/** - * Calls the specified function [block] with `this` value as its argument and always returns `Unit`. - */ -inline fun T.unit(block: (T) -> Any?) { - block(this) -} - -/** - * Can be used to transform a `when` receiver into an expression, making it **exhaustive**. - * @return `Unit` - */ -val Any?.unit inline get() = Unit - val Throwable.simpleMessage get() = "(${javaClass.simpleName}) ${message.trimToDefault(toString())}" -inline operator fun Boolean?.rem(block: () -> T): T? = this?.let { if (this) block() else null } - //fun StopWatch.stopIfRunning(): StopWatch { if (!isStopped) stop(); return this } fun Random.nextInt(range: IntRange): Int = if (range.first == range.last) range.first diff --git a/base/src/main/kotlin/klib/base/StringKit.kt b/base/src/main/kotlin/klib/base/StringKit.kt index a65679b..c91d599 100644 --- a/base/src/main/kotlin/klib/base/StringKit.kt +++ b/base/src/main/kotlin/klib/base/StringKit.kt @@ -1,5 +1,18 @@ package klib.base +import kotlin.random.Random + +val ALPHA_CHARS = ('a'..'z') + ('A'..'Z') +val ALPHANUM_CHARS = ALPHA_CHARS + ('0'..'9') +val COMMON_CHARS = ALPHANUM_CHARS + ".- ".toList() + +// TODO Remove once it gets out of experimental state in Kotlin stdlib +fun CharArray.concatToString() = String(this) + +fun Random.string(len: Int = 10, chars: Array = ALPHANUM_CHARS.toTypedArray()) = CharArray(len) { + Random.nextInt(0, ALPHANUM_CHARS.size).let(ALPHANUM_CHARS::get) +}.concatToString() + inline fun String.prefixWith(prefix: String?) = prefix?.plus(this) ?: this fun Any?.fmt(format: String, defaultWhenNull: String = ""): String = this?.let { @@ -22,6 +35,11 @@ fun String?.toNormalizedLowerCase() = this.trimToNull() ?.toLowerCase()?.replace(illegalNameCharsLowerCase, " ") ?.toNormalizedSpaces() +private val notAlpha: Regex by lazy { "[^0-9a-zA-Z]+".toRegex() } +fun String?.toNormalizedAlpha() = this + ?.replace(notAlpha, " ") + ?.toNormalizedSpaces() + private val noDigits: Regex by lazy { "[^0-9]+".toRegex() } val String?.tryLong get() = this?.replace(noDigits, "")?.toLongOrNull()