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

commonMain.it.unibo.tuprolog.solve.channel.impl.OutputStoreImpl.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.OutputChannel
import it.unibo.tuprolog.solve.channel.OutputStore
import it.unibo.tuprolog.solve.channel.OutputStore.Companion.STDERR
import it.unibo.tuprolog.solve.channel.OutputStore.Companion.STDOUT
import it.unibo.tuprolog.solve.channel.impl.ChannelStoreUtils.ensureAliasRefersToChannel
import it.unibo.tuprolog.solve.channel.impl.ChannelStoreUtils.setCurrent
import it.unibo.tuprolog.solve.exception.Warning

internal class OutputStoreImpl(
    override val stdOut: OutputChannel,
    override val stdErr: OutputChannel,
    override val warnings: OutputChannel = OutputChannel.warn(),
    outputChannels: Map> = emptyMap(),
) : AbstractChannelStore, OutputStore>(checkChannels(stdOut, stdErr, outputChannels)),
    OutputStore {
    override fun setStdOut(channel: OutputChannel): OutputStore =
        (channels - STDOUT).let {
            if (current == stdOut) {
                OutputStoreImpl(channel, stdErr, warnings, it + (CURRENT to channel))
            } else {
                OutputStoreImpl(channel, stdErr, warnings, it)
            }
        }

    override fun setStdErr(channel: OutputChannel): OutputStore =
        (channels - STDERR).let {
            if (current == stdErr) {
                OutputStoreImpl(stdOut, channel, warnings, it + (CURRENT to channel))
            } else {
                OutputStoreImpl(stdOut, channel, warnings, it)
            }
        }

    override fun setWarnings(channel: OutputChannel): OutputStore =
        OutputStoreImpl(
            stdOut,
            stdErr,
            channel,
            channels,
        )

    override fun setCurrent(alias: String): OutputStore =
        when (val newCurrentChannel = get(alias)) {
            null -> this
            else -> OutputStoreImpl(stdOut, stdErr, warnings, mapOf(CURRENT to newCurrentChannel))
        }

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

    override fun plus(others: Map>): OutputStore =
        OutputStoreImpl(stdOut, stdErr, warnings, (this as Map>) + others)

    override fun minus(others: Sequence): OutputStore =
        OutputStoreImpl(
            stdOut,
            stdErr,
            warnings,
            channels - others,
        )

    companion object {
        private fun checkChannels(
            stdOut: OutputChannel,
            stdErr: OutputChannel,
            channels: Map>,
        ): Map> {
            return channels.toMutableMap()
                .ensureAliasRefersToChannel(STDOUT, stdOut)
                .ensureAliasRefersToChannel(STDERR, stdErr)
                .setCurrent(STDOUT, stdOut)
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy