summaryrefslogtreecommitdiff
path: root/plugins/kotlin/gradle/gradle-tooling/src/org/jetbrains/kotlin/idea/gradleTooling/KotlinFragmentCache.kt
blob: a84533e74ed80710bc880fae6dc0beb3f5913c50 (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
// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package org.jetbrains.kotlin.idea.gradleTooling

import org.jetbrains.kotlin.idea.projectModel.KotlinFragment
import org.jetbrains.kotlin.idea.projectModel.KotlinModuleIdentifier

interface KotlinFragmentCache {
    fun withCache(
        kotlinModuleIdentifier: KotlinModuleIdentifier, fragmentName: String, createFragment: () -> KotlinFragment
    ): KotlinFragment

    object None : KotlinFragmentCache {
        override fun withCache(
            kotlinModuleIdentifier: KotlinModuleIdentifier,
            fragmentName: String,
            createFragment: () -> KotlinFragment
        ): KotlinFragment = createFragment()
    }
}

internal class DefaultKotlinFragmentCache : KotlinFragmentCache {
    private data class Key(val moduleIdentifier: KotlinModuleIdentifier, val fragmentName: String)

    private val fragments = mutableMapOf<Key, KotlinFragment>()

    override fun withCache(
        kotlinModuleIdentifier: KotlinModuleIdentifier,
        fragmentName: String,
        createFragment: () -> KotlinFragment
    ) = fragments.getOrPut(Key(kotlinModuleIdentifier, fragmentName), createFragment)
}