Skip to content

Commit

Permalink
eliminated IppObject - unnecessary abstraction
Browse files Browse the repository at this point in the history
  • Loading branch information
gmuth committed Nov 1, 2023
1 parent 52267bd commit 0931d57
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 49 deletions.
17 changes: 11 additions & 6 deletions src/main/kotlin/de/gmuth/ipp/client/IppDocument.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ import java.io.File
import java.io.IOException
import java.io.InputStream
import java.io.OutputStream
import java.util.logging.Level
import java.util.logging.Logger
import java.util.logging.Logger.getLogger
import kotlin.io.path.createTempDirectory

class IppDocument(
private val job: IppJob,
documentAttributes: IppAttributesGroup,
val job: IppJob,
private val attributes: IppAttributesGroup,
private val inputStream: InputStream
) : IppObject(job, documentAttributes) {
) {

private val log = getLogger(javaClass.name)

Expand Down Expand Up @@ -79,12 +81,15 @@ class IppDocument(
fun runCommand(commandToHandleFile: String) =
Runtime.getRuntime().exec(arrayOf(commandToHandleFile, file!!.absolutePath))

override fun objectName() = "Document #$number"

override fun toString() = StringBuilder(objectName()).run {
override fun toString() = StringBuilder("Document #$number").run {
append(" ($format) of job #${job.id}")
if (attributes.containsKey("document-name")) append(" '$name'")
if (file != null) append(": $file (${file!!.length()} bytes)")
toString()
}

@JvmOverloads
fun log(logger: Logger, level: Level = Level.INFO) =
attributes.log(logger, level, title = "DOCUMENT #$number")

}
17 changes: 11 additions & 6 deletions src/main/kotlin/de/gmuth/ipp/client/IppEventNotification.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ import de.gmuth.ipp.attributes.JobState
import de.gmuth.ipp.attributes.PrinterState
import de.gmuth.ipp.core.IppAttributesGroup
import de.gmuth.ipp.core.IppString
import java.util.logging.Level
import java.util.logging.Logger

class IppEventNotification(
private val subscription: IppSubscription,
eventNotificationAttributes: IppAttributesGroup
) : IppObject(subscription, eventNotificationAttributes) {
val subscription: IppSubscription,
private val attributes: IppAttributesGroup
) {

val sequenceNumber: Int
get() = attributes.getValue("notify-sequence-number")
Expand Down Expand Up @@ -53,11 +55,9 @@ class IppEventNotification(
// Get job from printer
fun getJob() = subscription.printer.getJob(jobId)

override fun objectName() = "Event notification #$sequenceNumber $subscribedEvent"

@SuppressWarnings("kotlin:S3776")
override fun toString() = StringBuilder().run {
append("Event #$sequenceNumber:")
append("EventNotification #$sequenceNumber:")
append(" [$subscribedEvent] $text")
with(attributes) {
if (containsKey("notify-job-id")) append(", job #$jobId")
Expand All @@ -69,4 +69,9 @@ class IppEventNotification(
}
toString()
}

@JvmOverloads
fun log(logger: Logger, level: Level = Level.INFO) =
attributes.log(logger, level, title = "EVENTNOTIFICATION #$sequenceNumber $subscribedEvent")

}
15 changes: 10 additions & 5 deletions src/main/kotlin/de/gmuth/ipp/client/IppJob.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@ import java.io.InputStream
import java.net.URI
import java.time.Duration
import java.time.ZonedDateTime
import java.util.logging.Level
import java.util.logging.Logger
import java.util.logging.Logger.getLogger

@SuppressWarnings("kotlin:S1192")
class IppJob(
val printer: IppPrinter,
jobAttributes: IppAttributesGroup,
val attributes: IppAttributesGroup,
subscriptionAttributes: IppAttributesGroup? = null
) : IppObject(printer, jobAttributes) {
) : IppExchange by printer {

companion object {
var defaultDelay: Duration = Duration.ofSeconds(1)
Expand Down Expand Up @@ -341,11 +343,9 @@ class IppJob(
// Logging
// -------

override fun objectName() = "Job #$id"

@SuppressWarnings("kotlin:S3776")
override fun toString(): String = attributes.run {
StringBuilder(objectName()).run {
StringBuilder("Job #$id").run {
if (containsKey("job-state")) append(", state=$state")
if (containsKey("job-state-reasons")) append(" (reasons=${stateReasons.joinToString(",")})")
if (containsKey("job-name")) append(", name=$name")
Expand All @@ -359,4 +359,9 @@ class IppJob(
toString()
}
}

@JvmOverloads
fun log(logger: Logger, level: Level = Level.INFO) =
attributes.log(logger, level, title = "JOB #$id")

}
22 changes: 0 additions & 22 deletions src/main/kotlin/de/gmuth/ipp/client/IppObject.kt

This file was deleted.

14 changes: 9 additions & 5 deletions src/main/kotlin/de/gmuth/ipp/client/IppPrinter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,21 @@ import de.gmuth.ipp.iana.IppRegistrationsSection2
import java.io.*
import java.net.URI
import java.time.Duration
import java.util.logging.Level
import java.util.logging.Level.*
import java.util.logging.Logger
import java.util.logging.Logger.getLogger
import kotlin.io.path.createTempDirectory

@SuppressWarnings("kotlin:S1192")
class IppPrinter(
val printerUri: URI,
printerAttributes: IppAttributesGroup = IppAttributesGroup(Printer),
internal val attributes: IppAttributesGroup = IppAttributesGroup(Printer),
ippConfig: IppConfig = IppConfig(),
internal val ippClient: IppClient = IppClient(ippConfig),
getPrinterAttributesOnInit: Boolean = true,
requestedAttributesOnInit: List<String>? = null
) : IppObject(ippClient, printerAttributes) {
) : IppExchange {

private val log = getLogger(javaClass.name)
var workDirectory: File = createTempDirectory().toFile()
Expand Down Expand Up @@ -476,7 +478,7 @@ class IppPrinter(
) = ippClient
.ippRequest(operation, printerUri, requestedAttributes, userName)

override fun exchange(request: IppRequest) = super.exchange(request.apply {
override fun exchange(request: IppRequest) = ippClient.exchange(request.apply {
checkIfValueIsSupported("ipp-versions-supported", version!!)
checkIfValueIsSupported("operations-supported", code!!.toInt())
checkIfValueIsSupported("charset-supported", attributesCharset)
Expand All @@ -500,8 +502,6 @@ class IppPrinter(
// Logging
// -------

override fun objectName() = "Printer $name ($makeAndModel)"

override fun toString() = StringBuilder("Printer").run {
if (attributes.containsKey("printer-name")) append(" $name")
if (attributes.containsKey("printer-make-and-model")) append(" ($makeAndModel)")
Expand All @@ -513,6 +513,10 @@ class IppPrinter(
toString()
}

@JvmOverloads
fun log(logger: Logger, level: Level = INFO) =
attributes.log(logger, level, title = "PRINTER $name ($makeAndModel)")

// ------------------------------------------------------
// attribute value checking based on printer capabilities
// ------------------------------------------------------
Expand Down
15 changes: 10 additions & 5 deletions src/main/kotlin/de/gmuth/ipp/client/IppSubscription.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ import java.time.Duration
import java.time.Duration.ofSeconds
import java.time.LocalDateTime
import java.time.LocalDateTime.now
import java.util.logging.Level
import java.util.logging.Logger
import java.util.logging.Logger.getLogger

class IppSubscription(
val printer: IppPrinter,
subscriptionAttributes: IppAttributesGroup
) : IppObject(printer, subscriptionAttributes) {
private val attributes: IppAttributesGroup
) : IppExchange by printer {

private val log = getLogger(javaClass.name)

Expand Down Expand Up @@ -145,13 +147,16 @@ class IppSubscription(
// Logging
// -------

override fun objectName() = "Subscription #$id"

override fun toString() = StringBuilder(objectName()).run {
override fun toString() = StringBuilder("Subscription #$id").run {
if (attributes.containsKey("notify-job-id")) append(", job #$jobId")
if (attributes.containsKey("notify-events")) append(" events=${events.joinToString(",")}")
if (attributes.containsKey("notify-time-interval")) append(" time-interval=$timeInterval")
if (attributes.containsKey("notify-lease-duration")) append(" lease-duration=$leaseDuration (expires at $expiresAt)")
toString()
}

@JvmOverloads
fun log(logger: Logger, level: Level = Level.INFO) =
attributes.log(logger, level, title = "SUBSCRIPTION #$id")

}

0 comments on commit 0931d57

Please sign in to comment.