aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/utils/repopatterns/RepoPatternExclusiveGroupInfo.kt
blob: d7b4717efa7f2833164eadd53a1a632f1a36959f (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
package at.hannibal2.skyhanni.utils.repopatterns

import kotlin.properties.ReadOnlyProperty
import kotlin.reflect.KProperty

/**
 * A utility class for allowing easier definitions of [RepoPattern]s with a common prefix.
 */
class RepoPatternExclusiveGroupInfo internal constructor(val prefix: String, val parent: RepoPatternKeyOwner?) :
    ReadOnlyProperty<Any?, RepoPatternExclusiveGroup> {

    internal var hasObtainedLock = false
    private lateinit var owner: RepoPatternKeyOwner

    init {
        RepoPatternManager.verifyKeyShape(prefix)
    }

    override fun getValue(thisRef: Any?, property: KProperty<*>): RepoPatternExclusiveGroup {
        verifyLock(thisRef, property)
        return RepoPatternExclusiveGroup(prefix, owner)
    }

    /**
     * Try to lock the [key] to this key location.
     * @see RepoPatternManager.checkExclusivity
     */
    private fun verifyLock(thisRef: Any?, property: KProperty<*>) {
        if (hasObtainedLock) return
        hasObtainedLock = true
        owner = RepoPatternKeyOwner(thisRef?.javaClass, property, false, parent)
        RepoPatternManager.checkNameSpaceExclusivity(owner, prefix)
    }
}