From 0f196f59c6a4cb76ab8409da62ff1f35505f94a8 Mon Sep 17 00:00:00 2001 From: Dico Karssiens Date: Sun, 11 Nov 2018 14:06:45 +0000 Subject: Changes I made before breaking my local repository. Hoping this works. --- src/main/kotlin/io/dico/parcels2/util/ext/Misc.kt | 162 +++++++++++----------- 1 file changed, 81 insertions(+), 81 deletions(-) (limited to 'src/main/kotlin/io/dico/parcels2/util/ext/Misc.kt') diff --git a/src/main/kotlin/io/dico/parcels2/util/ext/Misc.kt b/src/main/kotlin/io/dico/parcels2/util/ext/Misc.kt index 75aba35..e5e8aa9 100644 --- a/src/main/kotlin/io/dico/parcels2/util/ext/Misc.kt +++ b/src/main/kotlin/io/dico/parcels2/util/ext/Misc.kt @@ -1,81 +1,81 @@ -package io.dico.parcels2.util.ext - -import io.dico.dicore.Formatting -import io.dico.parcels2.logger -import java.io.File - -fun File.tryCreate(): Boolean { - if (exists()) { - return !isDirectory - } - val parent = parentFile - if (parent == null || !(parent.exists() || parent.mkdirs()) || !createNewFile()) { - logger.warn("Failed to create file $canonicalPath") - return false - } - return true -} - -inline fun Boolean.alsoIfTrue(block: () -> Unit): Boolean = also { if (it) block() } -inline fun Boolean.alsoIfFalse(block: () -> Unit): Boolean = also { if (!it) block() } - -inline fun Any.synchronized(block: () -> R): R = synchronized(this, block) - -//inline fun T?.isNullOr(condition: T.() -> Boolean): Boolean = this == null || condition() -//inline fun T?.isPresentAnd(condition: T.() -> Boolean): Boolean = this != null && condition() -inline fun T?.ifNullRun(block: () -> Unit): T? { - if (this == null) block() - return this -} - -inline fun MutableMap.editLoop(block: EditLoopScope.(T, U) -> Unit) { - return EditLoopScope(this).doEditLoop(block) -} - -inline fun MutableMap.editLoop(block: EditLoopScope.() -> Unit) { - return EditLoopScope(this).doEditLoop(block) -} - -class EditLoopScope(val _map: MutableMap) { - private var iterator: MutableIterator>? = null - lateinit var _entry: MutableMap.MutableEntry - - inline val key get() = _entry.key - inline var value - get() = _entry.value - set(target) = run { _entry.setValue(target) } - - inline fun doEditLoop(block: EditLoopScope.() -> Unit) { - val it = _initIterator() - while (it.hasNext()) { - _entry = it.next() - block() - } - } - - inline fun doEditLoop(block: EditLoopScope.(T, U) -> Unit) { - val it = _initIterator() - while (it.hasNext()) { - val entry = it.next().also { _entry = it } - block(entry.key, entry.value) - } - } - - fun remove() { - iterator!!.remove() - } - - fun _initIterator(): MutableIterator> { - iterator?.let { throw IllegalStateException() } - return _map.entries.iterator().also { iterator = it } - } - -} - -operator fun Formatting.plus(other: Formatting) = toString() + other -operator fun Formatting.plus(other: String) = toString() + other - -inline fun Pair.forEach(block: (T) -> Unit) { - block(first) - block(second) -} +package io.dico.parcels2.util.ext + +import io.dico.dicore.Formatting +import io.dico.parcels2.logger +import java.io.File + +fun File.tryCreate(): Boolean { + if (exists()) { + return !isDirectory + } + val parent = parentFile + if (parent == null || !(parent.exists() || parent.mkdirs()) || !createNewFile()) { + logger.warn("Failed to create file $canonicalPath") + return false + } + return true +} + +inline fun Boolean.alsoIfTrue(block: () -> Unit): Boolean = also { if (it) block() } +inline fun Boolean.alsoIfFalse(block: () -> Unit): Boolean = also { if (!it) block() } + +inline fun Any.synchronized(block: () -> R): R = synchronized(this, block) + +//inline fun T?.isNullOr(condition: T.() -> Boolean): Boolean = this == null || condition() +//inline fun T?.isPresentAnd(condition: T.() -> Boolean): Boolean = this != null && condition() +inline fun T?.ifNullRun(block: () -> Unit): T? { + if (this == null) block() + return this +} + +inline fun MutableMap.editLoop(block: EditLoopScope.(T, U) -> Unit) { + return EditLoopScope(this).doEditLoop(block) +} + +inline fun MutableMap.editLoop(block: EditLoopScope.() -> Unit) { + return EditLoopScope(this).doEditLoop(block) +} + +class EditLoopScope(val _map: MutableMap) { + private var iterator: MutableIterator>? = null + lateinit var _entry: MutableMap.MutableEntry + + inline val key get() = _entry.key + inline var value + get() = _entry.value + set(target) = run { _entry.setValue(target) } + + inline fun doEditLoop(block: EditLoopScope.() -> Unit) { + val it = _initIterator() + while (it.hasNext()) { + _entry = it.next() + block() + } + } + + inline fun doEditLoop(block: EditLoopScope.(T, U) -> Unit) { + val it = _initIterator() + while (it.hasNext()) { + val entry = it.next().also { _entry = it } + block(entry.key, entry.value) + } + } + + fun remove() { + iterator!!.remove() + } + + fun _initIterator(): MutableIterator> { + iterator?.let { throw IllegalStateException() } + return _map.entries.iterator().also { iterator = it } + } + +} + +operator fun Formatting.plus(other: Formatting) = toString() + other +operator fun Formatting.plus(other: String) = toString() + other + +inline fun Pair.forEach(block: (T) -> Unit) { + block(first) + block(second) +} -- cgit v1.2.3