closeable-state.kt/src/commonMain/kotlin/ru/landgrafhomyak/utility/closeable_state/ChildCloseableState.kt

72 lines
1.9 KiB
Kotlin

package ru.landgrafhomyak.utility.closeable_state
import kotlin.jvm.JvmField
public sealed class ChildCloseableState<T : CloseableState> : CloseableState {
@JvmField
protected val _parent: CloseableState
@JvmField
protected val _state: T
protected constructor(parent: CloseableState, state: T) {
this._parent = parent
this._state = state
}
override fun throwClosed(): Nothing =
this._state.throwClosed()
override fun throwInUse(): Nothing =
this._state.throwInUse()
override fun assertNotClosed(): Unit =
this._state.assertNotClosed()
override val isClosed: Boolean
get() = this._state.isClosed
@ManualStateManipulation
override fun startUsage(): Unit =
this._state.startUsage()
@ManualStateManipulation
override fun finishUsage(): Unit =
this._state.finishUsage()
@ManualStateManipulation
override fun close() {
this._parent.tryFinishUsage {
this._state.finishUsage()
}
}
abstract override fun toString(): String
public class AllowsConcurrency : ChildCloseableState<CloseableState.AllowsConcurrency>, CloseableState.AllowsConcurrency {
public constructor(parent: CloseableState, state: CloseableState.AllowsConcurrency) : super(parent, state)
override fun toString(): String {
when {
this._state is OwnedUsagesCounter || this._state::class === UsagesCounter::class -> {
val base = this._state.toString()
return base.substring(0, base.length - 1) + " (child of ${this._parent})>"
}
else -> return "<child closeable state ${this._state} (parent if ${this._parent})>"
}
}
}
public class ExternallySynchronized : ChildCloseableState<CloseableState.ExternallySynchronized>, CloseableState.ExternallySynchronized {
public constructor(parent: CloseableState, state: CloseableState.ExternallySynchronized) : super(parent, state)
override val isInUse: Boolean
get() = this._state.isInUse
override fun toString(): String {
TODO("Not yet implemented")
}
}
}