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

xyz.shoaky.sourcedownloader.sdk.component.SdComponent.kt Maven / Gradle / Ivy

The newest version!
package xyz.shoaky.sourcedownloader.sdk.component

import com.google.common.base.CaseFormat
import xyz.shoaky.sourcedownloader.sdk.*
import java.nio.file.Path
import java.util.function.Consumer
import java.util.function.Predicate
import kotlin.io.path.createDirectories
import kotlin.io.path.exists
import kotlin.reflect.KClass
import kotlin.reflect.full.allSuperclasses

sealed interface SdComponent {

}

fun  KClass.componentSuperClasses(): List> {
    val result = mutableListOf(this)
    this.allSuperclasses
        .filter { it != SdComponent::class && it != Any::class }
        .filterIsInstanceTo(result)
    return result.toList()
}

enum class Components(
    val klass: KClass,
    val names: List
) {

    TRIGGER(Trigger::class, listOf("trigger")),
    SOURCE(Source::class, listOf("source")),
    DOWNLOADER(Downloader::class, listOf("downloader")),
    VARIABLE_PROVIDER(VariableProvider::class, listOf("provider", "variable-provider", "variableProvider")),
    FILE_MOVER(FileMover::class, listOf("mover", "file-mover", "fileMover")),
    RUN_AFTER_COMPLETION(RunAfterCompletion::class, listOf("run-after-completion", "run", "runAfterCompletion")),
    SOURCE_ITEM_FILTER(SourceItemFilter::class, listOf("source-item-filter", "item-filter", "sourceItemFilter", "itemFilter")),
    SOURCE_FILE_FILTER(SourceFileFilter::class, listOf("source-file-filter", "file-filter", "sourceFileFilter", "fileFilter"));

    fun lowerHyphenName(): String {
        return CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN,
            this.name
        )
    }

    companion object {

        private val nameMapping: Map = values().flatMap {
            it.names.map { name -> name to it }
        }.toMap()

        fun fromClass(klass: KClass): Components? {
            return values().firstOrNull { it.klass == klass }
        }

        fun fromName(name: String): Components? {
            return nameMapping[name]
        }
    }
}

interface Trigger : SdComponent {
    fun addTask(runnable: Runnable)

    fun start()

    fun stop()

    fun restart() {
        stop()
        start()
    }
}

interface Source : SdComponent {

    fun fetch(): List

}

interface Downloader : SdComponent {

    fun submit(task: DownloadTask)

    fun defaultDownloadPath(): Path

    /**
     * Resolve files from item
     * @return Relative paths in the download path
     */
    fun resolveFiles(sourceItem: SourceItem): List
}

interface VariableProvider : SdComponent {

    /**
     * 变量准确度 当和其他provider变量冲突时会根据该值来决定
     * 0:low
     * 1:med
     * 2:high
     *
     * 考虑过和单个变量绑定,但是感觉没必要
     */
    val accuracy: Int get() = 1

    fun createSourceGroup(sourceItem: SourceItem): SourceItemGroup
    fun support(item: SourceItem): Boolean

}

interface FileMover : SdComponent {

    fun rename(sourceContent: SourceContent): Boolean

    fun exists(paths: List): Boolean {
        return paths.all { it.exists() }
    }

    fun createDirectories(path: Path) {
        path.createDirectories()
    }
}

@FunctionalInterface
interface RunAfterCompletion : SdComponent, Consumer

/**
 * @return true if the item should be processed
 */
interface SourceItemFilter : SdComponent, Predicate

interface SourceFileFilter : SdComponent, Predicate

interface FileTagger : SdComponent {

    fun tag(fileContent: FileContent): List
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy