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

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

// Copyright 2017 HP Development Company, L.P.
// SPDX-License-Identifier: MIT

package com.hp.jipp.encoding

/**
 * An implementation of [MutableAttributeGroup].
 */
internal class MutableAttributeGroupImpl(
    override var tag: Tag,
    attributes: List>
) : MutableAttributeGroup() {

    private val map: LinkedHashMap, Attribute> = linkedMapOf()

    init {
        map.putAll(attributes.map { it.type!! to it })
    }

    override val size: Int
        get() = map.size

    override fun get(index: Int): Attribute<*> = map.values.elementAt(index)

    override operator fun get(name: String) =
        map.keys.firstOrNull { it.name == name }?.let { key -> map[key] }

    @Suppress("UNCHECKED_CAST") // We know type corresponds to T because that's all we allow in.
    override fun  get(type: AttributeType): Attribute? =
        map[type] as Attribute?

    /** Add attributes to this group. */
    override fun add(attribute: Attribute) {
        map[attribute.type!!] = attribute
    }

    /** Remove an attribute of the specified [type], returning true if an attribute was removed. */
    @Suppress("UNCHECKED_CAST") // We know type corresponds to T because that's all we allow in.
    override fun  remove(type: AttributeType): Attribute? =
        map.remove(type) as Attribute?

    override fun write(output: IppOutputStream) {
        toGroup().write(output)
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy