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

com.hp.jipp.encoding.Codec.kt Maven / Gradle / Ivy

There is a newer version: 0.7.16
Show newest version
// Copyright 2017 HP Development Company, L.P.
// SPDX-License-Identifier: MIT

package com.hp.jipp.encoding

/** Reads/writes values of [T]. */
interface Codec {
    val cls: Class

    /** Return true if this codec handles the specified tag. */
    fun handlesTag(tag: ValueTag): Boolean

    /** Read a value from the input stream */
    fun readValue(input: IppInputStream, startTag: ValueTag): T

    /** Write value (assuming it is an instance of [T]). */
    fun writeValue(output: IppOutputStream, value: Any)

    /** The tag to use for a particular value. */
    fun tagOf(value: Any): ValueTag

    companion object {
        /** Construct a codec handling [TaggedValue] values, covering any number of [ValueTag] input values. */
        inline operator fun  invoke(
            crossinline handlesTagFunc: (ValueTag) -> Boolean,
            crossinline readAttrFunc: IppInputStream.(startTag: ValueTag) -> T,
            crossinline writeAttrFunc: IppOutputStream.(value: T) -> Unit
        ) =
            object : Codec {
                override val cls: Class = T::class.java
                override fun tagOf(value: Any) = (value as T).tag
                override fun handlesTag(tag: ValueTag) = handlesTagFunc(tag)
                override fun readValue(input: IppInputStream, startTag: ValueTag): T =
                    input.readAttrFunc(startTag)
                override fun writeValue(output: IppOutputStream, value: Any) {
                    output.writeAttrFunc(value as T)
                }
            }

        /** Construct a codec handling values encoded by a particular [ValueTag]. */
        inline operator fun  invoke(
            valueTag: ValueTag,
            crossinline readAttrFunc: IppInputStream.(startTag: ValueTag) -> T,
            crossinline writeAttrFunc: IppOutputStream.(value: T) -> Unit
        ) =
            object : Codec {
                override val cls: Class = T::class.java
                override fun tagOf(value: Any) = valueTag
                override fun handlesTag(tag: ValueTag) = valueTag == tag
                override fun readValue(input: IppInputStream, startTag: ValueTag): T =
                    input.readAttrFunc(startTag)
                override fun writeValue(output: IppOutputStream, value: Any) {
                    output.writeAttrFunc(value as T)
                }
            }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy