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

ai.platon.pulsar.skeleton.crawl.CrawlLoops.kt Maven / Gradle / Ivy

There is a newer version: 2.1.0
Show newest version
package ai.platon.pulsar.skeleton.crawl

import ai.platon.pulsar.common.StartStopRunnable
import java.time.Duration
import java.util.concurrent.atomic.AtomicBoolean
import java.util.function.Predicate

class CrawlLoops(val loops: MutableList) : StartStopRunnable {
    companion object {
        val filters = mutableListOf>()
    }

    private val started = AtomicBoolean()

    val isStarted get() = started.get()

    constructor(loop: CrawlLoop): this(mutableListOf(loop))

    fun first() = loops.first()

    fun last() = loops.last()

    inline fun  firstIsInstance() = loops.filterIsInstance().first()

    inline fun  lastIsInstance() = loops.filterIsInstance().last()

    /**
     * Start all loops matching the filter if not started.
     * */
    override fun start() {
        if (started.compareAndSet(false, true)) {
            loops.filter { loop -> filters.isEmpty() || filters.all { it.test(loop) } }
                .forEach { it.start() }
        }
    }

    /**
     * Stop all loops matching the filter if started.
     * */
    override fun stop() {
        if (started.compareAndSet(true, false)) {
            loops.filter { loop -> filters.isEmpty() || filters.all { it.test(loop) } }
                .forEach { it.stop() }
        }
    }

    @Throws(InterruptedException::class)
    override fun await() {
        loops.forEach { it.await() }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy