All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.it.unibo.tuprolog.solve.channel.impl.InputStoreImpl.kt Maven / Gradle / Ivy

There is a newer version: 1.0.4
Show newest version
package it.unibo.tuprolog.solve.channel.impl

import it.unibo.tuprolog.solve.channel.ChannelStore.Companion.CURRENT
import it.unibo.tuprolog.solve.channel.InputChannel
import it.unibo.tuprolog.solve.channel.InputStore
import it.unibo.tuprolog.solve.channel.InputStore.Companion.STDIN
import it.unibo.tuprolog.solve.channel.impl.ChannelStoreUtils.ensureAliasRefersToChannel
import it.unibo.tuprolog.solve.channel.impl.ChannelStoreUtils.setCurrent

internal class InputStoreImpl(
    override val stdIn: InputChannel,
    channels: Map> = emptyMap(),
) : InputStore, AbstractChannelStore, InputStore>(
        channels.toMutableMap()
            .ensureAliasRefersToChannel(STDIN, stdIn)
            .setCurrent(STDIN, stdIn),
    ) {
    override fun setStdIn(channel: InputChannel): InputStore =
        (channels - STDIN).let {
            if (current == stdIn) {
                InputStoreImpl(channel, it + (CURRENT to channel))
            } else {
                InputStoreImpl(channel, it)
            }
        }

    override fun setCurrent(alias: String): InputStore =
        when (val newCurrentChannel = get(alias)) {
            null -> this
            else -> InputStoreImpl(stdIn, mapOf(CURRENT to newCurrentChannel))
        }

    override fun setCurrent(channel: InputChannel): InputStore {
        val key =
            entries.firstOrNull { (_, v) -> v == channel }?.key
                ?: throw NoSuchElementException("Channel $channel has no alias")
        return setCurrent(key)
    }

    override fun plus(others: Map>): InputStore =
        InputStoreImpl(stdIn, (this as Map>) + others)

    override fun minus(others: Sequence): InputStore = InputStoreImpl(stdIn, channels - others)
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy