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

.kovenant.kovenant-core.3.3.0.source-code.bulk-jvm.kt Maven / Gradle / Ivy

/*
 * Copyright (c) 2015 Mark Platvoet
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * THE SOFTWARE.
 */

package nl.komponents.kovenant

import java.util.*
import java.util.concurrent.atomic.AtomicInteger
import java.util.concurrent.atomic.AtomicReferenceArray


internal fun  concreteAll(vararg promises: Promise,
                           context: Context,
                           cancelOthersOnError: Boolean): Promise, Exception> {
    return concreteAll(promises.asSequence(), promises.size, context, cancelOthersOnError)
}

internal fun  concreteAll(promises: List>,
                           context: Context,
                           cancelOthersOnError: Boolean): Promise, Exception> {
    // this might fail with concurrent mutating list, revisit in the future
    // not really a Kovenant issue but can prevent this from ever completing
    return concreteAll(promises.asSequence(), promises.size, context, cancelOthersOnError)
}

internal fun  concreteAll(promises: Sequence>,
                           sequenceSize: Int,
                           context: Context,
                           cancelOthersOnError: Boolean): Promise, Exception> {
    if (sequenceSize == 0) throw IllegalArgumentException("no promises provided")

    val deferred = deferred, Exception>(context)
    val results = AtomicReferenceArray(sequenceSize)
    val successCount = AtomicInteger(sequenceSize)
    val failCount = AtomicInteger(0)
    promises.forEachIndexed {
        i, promise ->
        promise.success { v ->
            results.set(i, v)
            if (successCount.decrementAndGet() == 0) {

                deferred.resolve(results.asList())
            }
        }
        promise.fail { e ->
            if (failCount.incrementAndGet() == 1) {
                deferred.reject(e)
                if (cancelOthersOnError) {
                    promises.forEach {
                        if (it != promise && it is CancelablePromise) {
                            it.cancel(CancelException())
                        }
                    }
                }
            }
        }

    }

    return deferred.promise
}

internal fun  concreteAny(vararg promises: Promise,
                           context: Context,
                           cancelOthersOnSuccess: Boolean): Promise> {
    return concreteAny(promises.asSequence(), promises.size, context, cancelOthersOnSuccess)
}

internal fun  concreteAny(promises: List>,
                           context: Context,
                           cancelOthersOnSuccess: Boolean): Promise> {
    // this might fail with concurrent mutating list, revisit in the future
    // not really a Kovenant issue but can prevent this from ever completing
    return concreteAny(promises.asSequence(), promises.size, context, cancelOthersOnSuccess)
}

internal fun  concreteAny(promises: Sequence>,
                           sequenceSize: Int,
                           context: Context,
                           cancelOthersOnSuccess: Boolean): Promise> {
    if (sequenceSize == 0) throw IllegalArgumentException("no promises provided")

    val deferred = deferred>(context)
    val errors = AtomicReferenceArray(sequenceSize)
    val successCount = AtomicInteger(0)
    val failCount = AtomicInteger(sequenceSize)

    promises.forEachIndexed {
        i, promise ->
        promise.success { v ->
            if (successCount.incrementAndGet() == 1) {
                deferred.resolve(v)
                if (cancelOthersOnSuccess) {
                    promises.forEach {
                        if (it != promise && it is CancelablePromise) {
                            it.cancel(CancelException())
                        }
                    }
                }
            }
        }
        promise.fail { e ->
            errors.set(i, e)
            if (failCount.decrementAndGet() == 0) {
                deferred.reject(errors.asList())
            }
        }

    }

    return deferred.promise
}

private fun  AtomicReferenceArray.asList(): List {
    val list = ArrayList()
    for (i in 0..this.length() - 1) {
        list.add(this.get(i))
    }
    return list
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy