Skip to content

Commit

Permalink
refactored methods to getValueAs...
Browse files Browse the repository at this point in the history
  • Loading branch information
gmuth committed Sep 11, 2024
1 parent ae2002b commit 352a846
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 36 deletions.
4 changes: 2 additions & 2 deletions src/main/kotlin/de/gmuth/ipp/client/IppDocument.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package de.gmuth.ipp.client

/**
* Copyright (c) 2021-2023 Gerhard Muth
* Copyright (c) 2021-2024 Gerhard Muth
*/

import de.gmuth.ipp.core.IppAttributesGroup
Expand Down Expand Up @@ -53,7 +53,7 @@ class IppDocument(
if (numberOfDocuments > 1) append("-doc-$number")
job.getOriginatingUserNameOrAppleJobOwnerOrNull()?.let { append("-$it") }
if (attributes.containsKey("com.apple.print.JobInfo.PMApplicationName")) {
append("-${attributes.getTextValue("com.apple.print.JobInfo.PMApplicationName")}")
append("-${attributes.getValueAsString("com.apple.print.JobInfo.PMApplicationName")}")
}
job.getJobNameOrDocumentNameSuppliedOrAppleJobNameOrNull()?.let {
append("-${it.take(100)}")
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/de/gmuth/ipp/client/IppEventNotification.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package de.gmuth.ipp.client

/**
* Copyright (c) 2021-2023 Gerhard Muth
* Copyright (c) 2021-2024 Gerhard Muth
*/

import de.gmuth.ipp.attributes.JobState
Expand Down Expand Up @@ -65,7 +65,7 @@ class IppEventNotification(

// let a Recipient know when the Event Notification occurred (RFC 3996 5.2.2)
val printerUpTime: ZonedDateTime
get() = attributes.getZonedDateTimeValue("printer-up-time")
get() = attributes.getValueAsZonedDateTime("printer-up-time")

// Get job of event origin
fun getJob() = subscription.printer.getJob(jobId)
Expand Down
10 changes: 5 additions & 5 deletions src/main/kotlin/de/gmuth/ipp/client/IppJob.kt
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,16 @@ class IppJob(
get() = attributes.getValue("document-name-supplied")

val timeAtCreation: ZonedDateTime
get() = attributes.getZonedDateTimeValue("time-at-creation")
get() = attributes.getValueAsZonedDateTime("time-at-creation")

val timeAtProcessing: ZonedDateTime
get() = attributes.getZonedDateTimeValue("time-at-processing")
get() = attributes.getValueAsZonedDateTime("time-at-processing")

val timeAtCompleted: ZonedDateTime
get() = attributes.getZonedDateTimeValue("time-at-completed")
get() = attributes.getValueAsZonedDateTime("time-at-completed")

val appleJobOwner: String // only supported by Apple CUPS
get() = attributes.getTextValue("com.apple.print.JobInfo.PMJobOwner")
get() = attributes.getValueAsString("com.apple.print.JobInfo.PMJobOwner")

fun isPending(updateStateAttributes: Boolean = false) = stateIs(updateStateAttributes, Pending)
fun isAborted(updateStateAttributes: Boolean = false) = stateIs(updateStateAttributes, Aborted)
Expand Down Expand Up @@ -127,7 +127,7 @@ class IppJob(
fun getJobNameOrDocumentNameSuppliedOrAppleJobNameOrNull() = when {
attributes.containsKey("job-name") -> name.text
attributes.containsKey("document-name-supplied") -> documentNameSupplied.text
attributes.containsKey("com.apple.print.JobInfo.PMJobName") -> attributes.getTextValue("com.apple.print.JobInfo.PMJobName")
attributes.containsKey("com.apple.print.JobInfo.PMJobName") -> attributes.getValueAsString("com.apple.print.JobInfo.PMJobName")
else -> null
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/de/gmuth/ipp/client/IppPrinter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ class IppPrinter(
get() = attributes.getValues("media-source-supported")

val mediaTypeSupported: List<String>
get() = attributes.getStringValues("media-type-supported")
get() = attributes.getValuesAsListOfStrings("media-type-supported")

val versionsSupported: List<String>
get() = attributes.getValues("ipp-versions-supported")
Expand Down Expand Up @@ -221,7 +221,7 @@ class IppPrinter(
printerType.contains(capability)

val cupsVersion: String
get() = attributes.getTextValue("cups-version")
get() = attributes.getValueAsString("cups-version")

val supportedAttributes: Collection<IppAttribute<*>> = attributes.values
.filter { it.name.endsWith("-supported") }
Expand Down
11 changes: 6 additions & 5 deletions src/main/kotlin/de/gmuth/ipp/client/IppSubscription.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class IppSubscription(
get() = attributes.getValue("notify-subscription-id")

val leaseDuration: Duration
get() = attributes.getDurationOfSecondsValue("notify-lease-duration")
get() = attributes.getValueAsDurationOfSeconds("notify-lease-duration")

val events: List<String>
get() = attributes.getValues("notify-events")
Expand All @@ -51,12 +51,12 @@ class IppSubscription(
get() = attributes.getValue("notify-subscriber-user-name")

val timeInterval: Duration
get() = attributes.getDurationOfSecondsValue("notify-time-interval")
get() = attributes.getValueAsDurationOfSeconds("notify-time-interval")

private fun expires() = when {
leaseDuration.isZero -> "(never expires)"
leaseStartedAt == null -> ""
else -> "(expires $expiresAt)"
leaseStartedAt != null -> "(expires $expiresAt)"
else -> ""
}

//----------------------------
Expand Down Expand Up @@ -135,7 +135,8 @@ class IppSubscription(
}

fun expired() = expiresAt != null && now().isAfter(expiresAt)
fun expiryAvailable() = leaseStartedAt != null && attributes.containsKey("notify-lease-duration") && !leaseDuration.isZero
fun expiryAvailable() =
leaseStartedAt != null && attributes.containsKey("notify-lease-duration") && !leaseDuration.isZero

@JvmOverloads
fun pollAndHandleNotifications(
Expand Down
10 changes: 5 additions & 5 deletions src/main/kotlin/de/gmuth/ipp/core/IppAttribute.kt
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ data class IppAttribute<T>(val name: String, val tag: IppTag) : IppAttributeBuil

override fun buildIppAttribute(printerAttributes: IppAttributesGroup) = this

fun getZonedDateTimeValue() = Instant
fun getValueAsZonedDateTime() = Instant
.ofEpochSecond((value as Int).toLong())
.atZone(ZoneOffset.UTC) // epoch always uses UTC

fun getDurationOfSecondsValue(): Duration =
fun getValueAsDurationOfSeconds(): Duration =
Duration.ofSeconds((value as Int).toLong())

override fun toString() = StringBuilder(name).run {
Expand All @@ -98,16 +98,16 @@ data class IppAttribute<T>(val name: String, val tag: IppTag) : IppAttributeBuil
|| name.endsWith("time-out")
|| name.endsWith("printer-up-time")) -> {
// should indicate the up-time in seconds (RFC 8011 5.4.29)
val duration = getDurationOfSecondsValue()
val duration = getValueAsDurationOfSeconds()
if (duration > Duration.ofDays(10 * 365)) { // looks like epoc time
"$value (${getZonedDateTimeValue()})"
"$value (${getValueAsZonedDateTime()})"
} else {
"$value ($duration)"
}
}

tag == Integer && (name.endsWith("time") || name.startsWith("time")) ->
"$value (${getZonedDateTimeValue()})"
"$value (${getValueAsZonedDateTime()})"

value is ByteArray -> with(value as ByteArray) { if (isEmpty()) "" else "$size bytes" }
else -> enumNameOrValue(value as Any).toString()
Expand Down
16 changes: 8 additions & 8 deletions src/main/kotlin/de/gmuth/ipp/core/IppAttributesGroup.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package de.gmuth.ipp.core

/**
* Copyright (c) 2020-2023 Gerhard Muth
* Copyright (c) 2020-2024 Gerhard Muth
*/

import java.io.BufferedWriter
Expand Down Expand Up @@ -52,20 +52,20 @@ class IppAttributesGroup(val tag: IppTag) : LinkedHashMap<String, IppAttribute<*
fun <T> getValues(name: String) =
get(name)?.values as T ?: throw attributeNotFoundException(name)

fun getUriValue(name: String) =
fun getValueAsURI(name: String) =
getValue<URI>(name)

fun getTextValue(name: String) =
fun getValueAsString(name: String) =
getValue<IppString>(name).text

fun getStringValues(name: String) =
fun getValuesAsListOfStrings(name: String) =
get(name)?.getStringValues() ?: throw attributeNotFoundException(name)

fun getZonedDateTimeValue(name: String, zoneId: ZoneId = ZoneId.systemDefault()): ZonedDateTime =
get(name)?.getZonedDateTimeValue()?.withZoneSameInstant(zoneId) ?: throw attributeNotFoundException(name)
fun getValueAsZonedDateTime(name: String, zoneId: ZoneId = ZoneId.systemDefault()): ZonedDateTime =
get(name)?.getValueAsZonedDateTime()?.withZoneSameInstant(zoneId) ?: throw attributeNotFoundException(name)

fun getDurationOfSecondsValue(name: String): Duration =
get(name)?.getDurationOfSecondsValue() ?: throw attributeNotFoundException(name)
fun getValueAsDurationOfSeconds(name: String): Duration =
get(name)?.getValueAsDurationOfSeconds() ?: throw attributeNotFoundException(name)

fun put(attributesGroup: IppAttributesGroup) =
attributesGroup.values.forEach { put(it) }
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/de/gmuth/ipp/core/IppMessage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ abstract class IppMessage() {
get() = operationGroup.getValue("attributes-natural-language")

val requestingUserName: String
get() = operationGroup.getTextValue("requesting-user-name")
get() = operationGroup.getValueAsString("requesting-user-name")

// --------
// ENCODING
Expand Down
6 changes: 3 additions & 3 deletions src/main/kotlin/de/gmuth/ipp/core/IppRequest.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package de.gmuth.ipp.core

/**
* Copyright (c) 2020-2023 Gerhard Muth
* Copyright (c) 2020-2024 Gerhard Muth
*/

import de.gmuth.ipp.core.IppTag.*
Expand All @@ -18,8 +18,8 @@ class IppRequest : IppMessage {
@SuppressWarnings("kotlin:S1192")
get() = operationGroup.run {
when {
containsKey("printer-uri") -> getUriValue("printer-uri")
containsKey("job-uri") -> getUriValue("job-uri")
containsKey("printer-uri") -> getValueAsURI("printer-uri")
containsKey("job-uri") -> getValueAsURI("job-uri")
else -> throw IppException("Missing 'printer-uri' or 'job-uri' in IppRequest")
.also { log(logger, Level.WARNING) }
}
Expand Down
6 changes: 3 additions & 3 deletions src/test/kotlin/de/gmuth/ipp/core/IppAttributesGroupTests.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package de.gmuth.ipp.core

/**
* Copyright (c) 2020-2023 Gerhard Muth
* Copyright (c) 2020-2024 Gerhard Muth
*/

import de.gmuth.ipp.core.IppTag.*
Expand Down Expand Up @@ -80,15 +80,15 @@ class IppAttributesGroupTests {
@Test
fun getTextValue() {
group.attribute("foo", TextWithoutLanguage, IppString("bar"))
assertEquals("bar", group.getTextValue("foo"))
assertEquals("bar", group.getValueAsString("foo"))
}

@Test
fun getEpochTimeValue() {
group.attribute("epoch-seconds", Integer, 62)
assertEquals(
"1970-01-01T00:01:02",
group.getZonedDateTimeValue("epoch-seconds")
group.getValueAsZonedDateTime("epoch-seconds")
.withZoneSameInstant(ZoneId.of("UTC"))
.toLocalDateTime().toString()
)
Expand Down

0 comments on commit 352a846

Please sign in to comment.