com.hp.jipp.encoding.MutableAttributeGroup.kt Maven / Gradle / Ivy
// Copyright 2017 HP Development Company, L.P.
// SPDX-License-Identifier: MIT
package com.hp.jipp.encoding
import com.hp.jipp.encoding.AttributeGroup.Companion.groupOf
/**
* A group of attributes which may be altered.
*/
@Suppress("TooManyFunctions")
abstract class MutableAttributeGroup : AttributeGroup, AbstractList>() {
abstract override var tag: Tag
/** Add an attribute to this group. */
abstract fun add(attribute: Attribute)
/** Add attributes to this group. */
fun addAll(attributes: Collection>) {
attributes.forEach { add(it) }
}
/** Add or replace the attribute value for [type]. */
inline operator fun set(type: AttributeType, value: T) {
add(type.of(value))
}
/** Add or replace the attribute value for [type]. */
inline operator fun set(type: AttributeType, values: List) {
add(type.of(values))
}
operator fun plusAssign(attribute: Attribute) = add(attribute)
operator fun plusAssign(attributes: Collection>) = addAll(attributes)
/** Remove an attribute of the specified [type], returning true if an attribute was removed. */
abstract fun remove(type: AttributeType): Attribute?
/** Return a copy of this object as a non-mutable [AttributeGroup]. */
fun toGroup(): AttributeGroup = groupOf(tag, toList())
/** Add a list of attributes to append or replace in the current context. */
fun attr(toAdd: List>) {
addAll(toAdd)
}
/** Add one or more attributes to be appended or replaced in the current context. */
fun attr(vararg attribute: Attribute<*>) {
attr(attribute.toList())
}
/** Add or replace an attribute to the group having one or more values. */
fun attr(attributeType: AttributeType, value: T, vararg values: T) {
if (values.isEmpty()) {
// Note: must be listOf here or we end up with List