com.hp.jipp.encoding.AttributeGroupImpl.kt Maven / Gradle / Ivy
// Copyright 2017 HP Development Company, L.P.
// SPDX-License-Identifier: MIT
package com.hp.jipp.encoding
import com.hp.jipp.util.BuildError
import java.util.HashSet
/**
* An implementation of [AttributeGroup].
*/
@Suppress("TooManyFunctions") // Necessary
internal class AttributeGroupImpl(
override val tag: Tag,
private val attributes: List>
) : AttributeGroup, List> by attributes {
init {
if (!tag.isDelimiter) {
throw BuildError("Group tag $tag must be a delimiter")
}
// RFC2910: Within an attribute group, if two or more attributes have the same name, the attribute group
// is malformed (see [RFC2911] section 3.1.3). Throw if someone attempts this.
val names = HashSet()
for (attribute in attributes) {
val name = attribute.name
if (names.contains(name)) {
throw BuildError("Attribute Group contains more than one '$name` in $attributes")
}
names.add(name)
}
}
/** Return the attribute corresponding to the specified [name]. */
override operator fun get(name: String): Attribute<*>? = firstOrNull { it.name == name }
/** Return the attribute as conforming to the supplied attribute type. */
override operator fun get(type: AttributeType): Attribute? =
get(type.name)?.let {
type.coerce(it)
}
override fun equals(other: Any?) =
when {
other === this -> true
other is AttributeGroup -> other.tag == tag && attributes == other
other is List<*> -> attributes == other
else -> false
}
override fun hashCode(): Int {
// Note: tag is not included because we might need to hash this with other List objects
return attributes.hashCode()
}
override fun toString(): String {
return "AttributeGroup($tag, $attributes)"
}
}