From f5fadc1f68fba4269c85dd46019bf72bba9e599f Mon Sep 17 00:00:00 2001 From: Sebastian Sellmair Date: Mon, 20 Mar 2023 11:00:44 +0100 Subject: [PATCH] [kotlin-tooling-core] Add Interner.clear API --- .../api/kotlin-tooling-core.api | 1 + .../jetbrains/kotlin/tooling/core/Interner.kt | 20 ++++++++++++++++++- .../kotlin/tooling/core/InternerTest.kt | 4 ++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/libraries/tools/kotlin-tooling-core/api/kotlin-tooling-core.api b/libraries/tools/kotlin-tooling-core/api/kotlin-tooling-core.api index 774292e3407..f0466bd3d64 100644 --- a/libraries/tools/kotlin-tooling-core/api/kotlin-tooling-core.api +++ b/libraries/tools/kotlin-tooling-core/api/kotlin-tooling-core.api @@ -145,6 +145,7 @@ public abstract interface class org/jetbrains/kotlin/tooling/core/HasMutableExtr } public abstract interface class org/jetbrains/kotlin/tooling/core/Interner { + public abstract fun clear ()V public abstract fun getOrPut (Ljava/lang/Object;)Ljava/lang/Object; } diff --git a/libraries/tools/kotlin-tooling-core/src/main/kotlin/org/jetbrains/kotlin/tooling/core/Interner.kt b/libraries/tools/kotlin-tooling-core/src/main/kotlin/org/jetbrains/kotlin/tooling/core/Interner.kt index 7de3b50645f..3f6e7bf24d2 100644 --- a/libraries/tools/kotlin-tooling-core/src/main/kotlin/org/jetbrains/kotlin/tooling/core/Interner.kt +++ b/libraries/tools/kotlin-tooling-core/src/main/kotlin/org/jetbrains/kotlin/tooling/core/Interner.kt @@ -20,6 +20,7 @@ fun Interner(lock: Lock? = null): Interner = InternerImpl(Strong(), lock) interface Interner { fun getOrPut(value: T): T + fun clear() } private class InternerImpl( @@ -29,6 +30,7 @@ private class InternerImpl( interface Store { fun getOrPut(value: T): T + fun clear() class Weak : Store { private val references = WeakHashMap>() @@ -39,6 +41,10 @@ private class InternerImpl( value }) as T } + + override fun clear() { + return references.clear() + } } @Suppress("UNCHECKED_CAST") @@ -47,10 +53,22 @@ private class InternerImpl( override fun getOrPut(value: T): T { return references.getOrPut(value) { value } as T } + + override fun clear() { + return references.clear() + } } } override fun getOrPut(value: T): T { - return lock?.withLock { store.getOrPut(value) } ?: store.getOrPut(value) + return withLockIfAny { store.getOrPut(value) } + } + + override fun clear() { + return withLockIfAny { store.clear() } + } + + private inline fun withLockIfAny(action: () -> T): T { + return if (lock != null) lock.withLock(action) else action() } } diff --git a/libraries/tools/kotlin-tooling-core/src/test/kotlin/org/jetbrains/kotlin/tooling/core/InternerTest.kt b/libraries/tools/kotlin-tooling-core/src/test/kotlin/org/jetbrains/kotlin/tooling/core/InternerTest.kt index 70671a5be72..88834ff318a 100644 --- a/libraries/tools/kotlin-tooling-core/src/test/kotlin/org/jetbrains/kotlin/tooling/core/InternerTest.kt +++ b/libraries/tools/kotlin-tooling-core/src/test/kotlin/org/jetbrains/kotlin/tooling/core/InternerTest.kt @@ -8,6 +8,7 @@ package org.jetbrains.kotlin.tooling.core import org.junit.Test +import kotlin.test.assertNotSame import kotlin.test.assertSame class InternerTest { @@ -36,5 +37,8 @@ class InternerTest { assertSame(sample1A, interner.getOrPut(sample1B)) assertSame(sample0A, interner.getOrPut(sample0A)) assertSame(sample0A, interner.getOrPut(sample0B)) + + interner.clear() + assertNotSame(sample0A, interner.getOrPut(Sample(0))) } } \ No newline at end of file