aboutsummaryrefslogtreecommitdiff
path: root/core/src/main/kotlin/model/CompositeSourceSetID.kt
blob: 363186332392f60b96d4946248ecd1064dc9555b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*
 * Copyright 2014-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
 */

package org.jetbrains.dokka.model

import org.jetbrains.dokka.DokkaConfiguration
import org.jetbrains.dokka.DokkaSourceSetID

/**
 * A unique composite key of multiple [DokkaSourceSetID] that identifies [DisplaySourceSet].
 * Consists of multiple (non-zero) [DokkaSourceSetID] that the corresponding [DisplaySourceSet] was built from.
 *
 * Should not be constructed or copied outside of [DisplaySourceSet] instantiation.
 */
public data class CompositeSourceSetID(
    private val children: Set<DokkaSourceSetID>
) {
    public constructor(sourceSetIDs: Iterable<DokkaSourceSetID>) : this(sourceSetIDs.toSet())
    public constructor(sourceSetId: DokkaSourceSetID) : this(setOf(sourceSetId))

    init {
        require(children.isNotEmpty()) { "Expected at least one source set id" }
    }

    public val merged: DokkaSourceSetID = children.sortedBy { it.scopeId + it.sourceSetName }.let { sortedChildren ->
        DokkaSourceSetID(
            scopeId = sortedChildren.joinToString(separator = "+") { it.scopeId },
            sourceSetName = sortedChildren.joinToString(separator = "+") { it.sourceSetName }
        )
    }

    public val all: Set<DokkaSourceSetID> = setOf(merged, *children.toTypedArray())

    operator fun contains(sourceSetId: DokkaSourceSetID): Boolean {
        return sourceSetId in all
    }

    operator fun contains(sourceSet: DokkaConfiguration.DokkaSourceSet): Boolean {
        return sourceSet.sourceSetID in this
    }

    operator fun plus(other: DokkaSourceSetID): CompositeSourceSetID {
        return copy(children = children + other)
    }
}