commonMain.it.unibo.tuprolog.solve.channel.impl.AbstractChannel.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of solve-jvm Show documentation
Show all versions of solve-jvm Show documentation
Resolution-agnostic API for logic solvers
package it.unibo.tuprolog.solve.channel.impl
import it.unibo.tuprolog.solve.channel.Channel
import it.unibo.tuprolog.solve.channel.Listener
import it.unibo.tuprolog.utils.synchronizedOnSelf
import kotlin.concurrent.Volatile
abstract class AbstractChannel : Channel {
companion object {
@Volatile
private var instanceCount: Long = 0
private fun nextId(): String = (instanceCount++).toString(16).padStart(16, '0')
}
@Suppress("ktlint:standard:property-naming")
private val _listeners: MutableList> = mutableListOf()
protected val id = nextId()
override fun addListener(listener: Listener): Unit =
synchronizedOnSelf {
_listeners.add(listener)
}
override fun removeListener(listener: Listener): Unit =
synchronizedOnSelf {
_listeners.remove(listener)
}
override fun clearListeners() =
synchronizedOnSelf {
_listeners.clear()
}
protected fun notify(value: T?) {
_listeners.forEach { it(value) }
}
override fun close() =
synchronizedOnSelf {
if (!isClosed) {
isClosed = true
} else {
throw IllegalStateException("Channel is already closed")
}
}
final override var isClosed: Boolean = false
get() = synchronizedOnSelf { field }
private set
}