From 0da9928e9a16921de4f21e362b123d4139087dbd Mon Sep 17 00:00:00 2001 From: Romain BOISSELLE Date: Tue, 7 Jan 2025 19:51:10 +0100 Subject: [PATCH] refactor(#463): deprecate ScopeCloseable in benefit of kotlin AutoCloseable --- .../kotlin/org/kodein/di/bindings/scopes.kt | 21 +++++++++++-------- .../org/kodein/di/test/CloseableData.kt | 4 +--- .../org/kodein/di/GenericJvmTests_13_Scope.kt | 10 +++++++-- 3 files changed, 21 insertions(+), 14 deletions(-) diff --git a/kodein-di/src/commonMain/kotlin/org/kodein/di/bindings/scopes.kt b/kodein-di/src/commonMain/kotlin/org/kodein/di/bindings/scopes.kt index de6796609..fa92d7dee 100644 --- a/kodein-di/src/commonMain/kotlin/org/kodein/di/bindings/scopes.kt +++ b/kodein-di/src/commonMain/kotlin/org/kodein/di/bindings/scopes.kt @@ -9,15 +9,18 @@ import org.kodein.di.internal.synchronizedIfNotNull import org.kodein.di.internal.synchronizedIfNull import org.kodein.type.TypeToken -public interface ScopeCloseable { - public fun close() -} +@Deprecated( + message = "Use kotlin AutoCloseable instead.", + replaceWith = ReplaceWith("AutoCloseable", imports = arrayOf("AutoCloseable")) +) +public typealias ScopeCloseable = AutoCloseable private typealias RegKey = Any /** * A registry is responsible managing references inside a scope. */ +@Suppress("DEPRECATION") public sealed class ScopeRegistry : ScopeCloseable { /** * Get or create a value that correspond for the given key. @@ -74,7 +77,7 @@ public class StandardScopeRegistry : ScopeRegistry() { override fun values(): List Any?>> = _cache.map { it.toPair() } override fun remove(key: RegKey) { - (_cache.remove(key)?.invoke() as? ScopeCloseable)?.close() + (_cache.remove(key)?.invoke() as? AutoCloseable)?.close() } /** @@ -87,7 +90,7 @@ public class StandardScopeRegistry : ScopeRegistry() { refs } refs.forEach { - (it.invoke() as? ScopeCloseable)?.close() + (it.invoke() as? AutoCloseable)?.close() } } @@ -125,7 +128,7 @@ public class SingleItemScopeRegistry : ScopeRegistry() { oldRef to value } ) - (oldRef?.invoke() as? ScopeCloseable)?.close() + (oldRef?.invoke() as? AutoCloseable)?.close() return value } @@ -151,7 +154,7 @@ public class SingleItemScopeRegistry : ScopeRegistry() { } ) - (ref?.invoke() as? ScopeCloseable)?.close() + (ref?.invoke() as? AutoCloseable)?.close() } /** @@ -168,7 +171,7 @@ public class SingleItemScopeRegistry : ScopeRegistry() { } ) - (ref?.invoke() as? ScopeCloseable)?.close() + (ref?.invoke() as? AutoCloseable)?.close() } } @@ -221,7 +224,7 @@ public interface Scope { * * This is kind of equivalent to having no scope at all, except that you can call [clear]. */ -public open class UnboundedScope(public val registry: ScopeRegistry = StandardScopeRegistry()) : Scope, ScopeCloseable { +public open class UnboundedScope(public val registry: ScopeRegistry = StandardScopeRegistry()) : Scope, AutoCloseable { override fun getRegistry(context: Any?): ScopeRegistry = registry override fun close(): Unit = registry.clear() diff --git a/kodein-di/src/commonTest/kotlin/org/kodein/di/test/CloseableData.kt b/kodein-di/src/commonTest/kotlin/org/kodein/di/test/CloseableData.kt index 63c056def..25e38ea31 100644 --- a/kodein-di/src/commonTest/kotlin/org/kodein/di/test/CloseableData.kt +++ b/kodein-di/src/commonTest/kotlin/org/kodein/di/test/CloseableData.kt @@ -1,8 +1,6 @@ package org.kodein.di.test -import org.kodein.di.bindings.ScopeCloseable - -class CloseableData(val name: String? = null) : ScopeCloseable { +class CloseableData(val name: String? = null) : AutoCloseable { var closed = false private set diff --git a/kodein-di/src/jvmTest/kotlin/org/kodein/di/GenericJvmTests_13_Scope.kt b/kodein-di/src/jvmTest/kotlin/org/kodein/di/GenericJvmTests_13_Scope.kt index d6a104199..0674bd499 100644 --- a/kodein-di/src/jvmTest/kotlin/org/kodein/di/GenericJvmTests_13_Scope.kt +++ b/kodein-di/src/jvmTest/kotlin/org/kodein/di/GenericJvmTests_13_Scope.kt @@ -1,7 +1,13 @@ package org.kodein.di -import org.kodein.di.bindings.* +import org.kodein.di.bindings.Scope +import org.kodein.di.bindings.ScopeRegistry +import org.kodein.di.bindings.SingleItemScopeRegistry +import org.kodein.di.bindings.StandardScopeRegistry +import org.kodein.di.bindings.SubScope +import org.kodein.di.bindings.UnboundedScope import org.kodein.di.test.* +import java.lang.AutoCloseable import kotlin.test.* @FixMethodOrder(MethodSorters.NAME_ASCENDING) @@ -70,7 +76,7 @@ class GenericJvmTests_13_Scope { assertSame(a, b) } - class CloseableData(val name: String? = null) : ScopeCloseable { + class CloseableData(val name: String? = null) : AutoCloseable { var closed = false private set