All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.partiql.lang.util.ThreadInterruptUtils.kt Maven / Gradle / Ivy

There is a newer version: 1.0.0-perf.1
Show newest version
package org.partiql.lang.util

/** Throws [InterruptedException] if [Thread.interrupted] is set. */
internal fun checkThreadInterrupted() {
    if (Thread.interrupted()) {
        throw InterruptedException()
    }
}

/**
 * Like a regular [map], but checks [Thread.interrupted] before each iteration and throws
 * [InterruptedException] if it is set.
 *
 * This should be used instead of the regular [map] where there is a potential for a large
 * number of items in the receiver [List] to allow long running operations to be aborted
 * by the caller.
 */
internal inline fun  List.interruptibleMap(crossinline block: (T) -> R): List =
    this.map { checkThreadInterrupted(); block(it) }

/**
 * Like a regular [fold], but checks [Thread.interrupted] before each iteration and throws
 * [InterruptedException] if it is set.
 *
 * This should be used instead of the regular [fold] where there is a potential for a large
 * number of items in the receiver [List] to allow long running operations to be aborted
 * by the caller.
 */
internal inline fun  List.interruptibleFold(initial: A, crossinline block: (A, T) -> A) =
    this.fold(initial) { acc, curr -> checkThreadInterrupted(); block(acc, curr) }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy