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

name.remal.gradle_plugins.dsl.artifact.CachedArtifactsCollection.kt Maven / Gradle / Ivy

There is a newer version: 1.9.2
Show newest version
package name.remal.gradle_plugins.dsl.artifact

import java.io.File
import java.io.InputStream
import java.util.*
import kotlin.collections.set

class CachedArtifactsCollection(val artifacts: Set) : BaseHasEntries() {

    constructor(files: Iterable) : this(files.mapTo(mutableSetOf(), ArtifactsCache::get))
    constructor(vararg files: Iterable) : this(files.toList().flatten())
    constructor(vararg files: File) : this(files.toList())
    constructor(file: File) : this(setOf(ArtifactsCache[file]))

    val entryMapping: Map by lazy {
        val result = TreeMap()
        artifacts.forEach { artifact ->
            artifact.entryNames.forEach {
                if (it !in result) result[it] = artifact
            }
        }
        return@lazy result
    }

    override val entryNames: Set by lazy { entryMapping.keys }

    override fun openStream(entryName: String): InputStream {
        val artifact = entryMapping[entryName] ?: throw ArtifactEntryNotFoundException("Artifact entry not found: $entryName")
        return artifact.openStream(entryName)
    }

    override fun forEachEntry(pattern: String?, action: (entry: HasEntries.Entry) -> Unit) {
        artifacts.forEach { it.forEachEntry(pattern, action) }
    }

    override val annotationClassNames: Set by lazy {
        sortedSetOf().also { result ->
            artifacts.forEach { artifact ->
                result.addAll(artifact.annotationClassNames)
            }
        }
    }

    override val annotationsMapping: Map> by lazy {
        mutableMapOf>().apply {
            artifacts.forEach { artifact ->
                artifact.annotationsMapping.forEach { annotationClassName, annotatedClassNames ->
                    computeIfAbsent(annotationClassName, { mutableSetOf() }).addAll(annotatedClassNames)
                }
            }
        }
    }

    override fun toString(): String {
        return "${javaClass.simpleName}[$artifacts]"
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy