Skip to content

Commit

Permalink
refactored bit handling
Browse files Browse the repository at this point in the history
  • Loading branch information
gmuth committed Dec 11, 2024
1 parent 6bd071b commit b9a9aad
Showing 1 changed file with 11 additions and 14 deletions.
25 changes: 11 additions & 14 deletions src/main/kotlin/de/gmuth/ipp/attributes/PrinterType.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import java.util.logging.Logger
// https://www.cups.org/doc/spec-ipp.html
class PrinterType(val value: Int) : IppAttributeBuilder {

enum class Capability(val bit: Int, val description: String) {
enum class Capability(bitNumber: Int, val description: String) {
IsAPrinterClass(0, "Is a printer class."),
IsARemoteDestination(1, "Is a remote destination."),
CanPrintInBlack(2, "Can print in black."),
Expand Down Expand Up @@ -43,23 +43,22 @@ class PrinterType(val value: Int) : IppAttributeBuilder {
QueueWasAutomaticallyDiscoveredAndAdded(24, "Queue was automatically discovered and added."),
QueueIsAScannerWithNoPrintingCapabilities(25, "Queue is a scanner with no printing capabilities."),
QueueIsAPrinterWithScanningCapabilities(26, "Queue is a printer with scanning capabilities."),
QueueIsAPrinterWith3DCapabilities(27, "Queue is a printer with 3D capabilities.")
QueueIsAPrinterWith3DCapabilities(27, "Queue is a printer with 3D capabilities.");

val value: Int = 1 shl bitNumber // set relevant bit
}

fun toSet(): Set<Capability> = Capability
.values()
.filter { (value shr it.bit) and 1 == 1 }
.toSet()
fun toSet(): Set<Capability> =
Capability.values().filter { value.and(it.value) != 0 }.toSet()

fun contains(capability: Capability) = toSet().contains(capability)
fun contains(capability: Capability) =
toSet().contains(capability)

override fun toString() = "$value (${toSet().joinToString(",")})"

fun log(logger: Logger, level: Level = INFO) = logger.run {
log(level) { "PRINTER-TYPE 0x%08X capabilities:".format(value) }
for (capability in toSet()) {
log(level) { "* ${capability.description}" }
}
toSet().forEach { log(level) { "* ${it.description}" } }
}

override fun buildIppAttribute(printerAttributes: IppAttributesGroup) =
Expand All @@ -69,9 +68,7 @@ class PrinterType(val value: Int) : IppAttributeBuilder {
fun fromAttributes(attributes: IppAttributesGroup) =
PrinterType(attributes.getValue("printer-type"))

fun fromCapabilities(capabilities: Set<Capability>) = capabilities
.map { 1 shl it.bit }
.reduce { c1, c2 -> c1 + c2 }
.let { PrinterType(it) }
fun fromCapabilities(capabilities: Set<Capability>) =
capabilities.map { it.value }.reduce { c1, c2 -> c1 + c2 }.let { PrinterType(it) }
}
}

0 comments on commit b9a9aad

Please sign in to comment.