aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/utils/repopatterns/CommonPatternInfo.kt
blob: a465139ae2ad778e136fe2c58bd970e55966a320 (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
47
48
49
package at.hannibal2.skyhanni.utils.repopatterns

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

sealed class CommonPatternInfo<R, C> : ReadOnlyProperty<Any?, C> {
    abstract val isLoadedRemotely: Boolean

    abstract val wasOverridden: Boolean

    abstract val defaultPattern: R

    abstract val key: String

    abstract val parent: RepoPatternKeyOwner?

    abstract val shares: Boolean

    abstract val value: C

    /**
     * Whether the pattern has obtained a lock on a code location and a key.
     * Once set, no other code locations can access this repo pattern (and therefore the key).
     * @see RepoPatternManager.checkExclusivity
     */
    internal var hasObtainedLock = false

    override fun getValue(thisRef: Any?, property: KProperty<*>): C {
        verifyLock(thisRef, property)
        return value
    }

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

    abstract fun dump(): Map<String, String>
}