diff --git a/src/main/kotlin/com/sainsburys/k2zpl/command/BarCode.kt b/src/main/kotlin/com/sainsburys/k2zpl/command/BarCode.kt index 412c3d0..1bcf8cf 100644 --- a/src/main/kotlin/com/sainsburys/k2zpl/command/BarCode.kt +++ b/src/main/kotlin/com/sainsburys/k2zpl/command/BarCode.kt @@ -13,20 +13,21 @@ internal data class BarCode( val height: Int, val line: Int, val lineAbove: ZplYesNo -) : ZplCommand { - init { - require(height in 1..32000) { "Height must be between 1 and 32000" } - require(line in 1..7) { "Line thickness must be between 1 and 7" } - } - - override val command: CharSequence = "^B1" - override val parameters: Map = addParameters( +) : ZplCommand( + parameters = listOf( "o" to orientation.code, "c" to checkDigit.toString(), "h" to height, "l" to line, "la" to lineAbove.toString() ) +) { + init { + require(height in 1..32000) { "Height must be between 1 and 32000" } + require(line in 1..7) { "Line thickness must be between 1 and 7" } + } + + override val command: CharSequence = "^B1" } /** diff --git a/src/main/kotlin/com/sainsburys/k2zpl/command/CustomCommand.kt b/src/main/kotlin/com/sainsburys/k2zpl/command/CustomCommand.kt index 7425782..4b9b9e0 100644 --- a/src/main/kotlin/com/sainsburys/k2zpl/command/CustomCommand.kt +++ b/src/main/kotlin/com/sainsburys/k2zpl/command/CustomCommand.kt @@ -1,3 +1,3 @@ package com.sainsburys.k2zpl.command -internal data class CustomCommand(override val command: CharSequence) : ZplCommand \ No newline at end of file +internal data class CustomCommand(override val command: CharSequence) : ZplCommand() \ No newline at end of file diff --git a/src/main/kotlin/com/sainsburys/k2zpl/command/EndFormat.kt b/src/main/kotlin/com/sainsburys/k2zpl/command/EndFormat.kt index d9b2683..61a6db8 100644 --- a/src/main/kotlin/com/sainsburys/k2zpl/command/EndFormat.kt +++ b/src/main/kotlin/com/sainsburys/k2zpl/command/EndFormat.kt @@ -2,7 +2,7 @@ package com.sainsburys.k2zpl.command import com.sainsburys.k2zpl.builder.ZplBuilder -internal data object EndFormat : ZplCommand { +internal data object EndFormat : ZplCommand() { override val command: CharSequence = "^XZ" } diff --git a/src/main/kotlin/com/sainsburys/k2zpl/command/FieldBlock.kt b/src/main/kotlin/com/sainsburys/k2zpl/command/FieldBlock.kt index 4632a77..785dc98 100644 --- a/src/main/kotlin/com/sainsburys/k2zpl/command/FieldBlock.kt +++ b/src/main/kotlin/com/sainsburys/k2zpl/command/FieldBlock.kt @@ -9,7 +9,15 @@ internal data class FieldBlock( val lineSpacing: Int, val alignment: ZplTextAlignment, val hangingIndent: Int -) : ZplCommand { +) : ZplCommand( + parameters = listOf( + "w" to width, + "l" to maxLines, + "s" to lineSpacing, + "a" to alignment.code, + "h" to hangingIndent + ) +) { init { require(width in 1..32000) { "Width must be between 1 and 32000" } require(maxLines in 1..9999) { "Lines must be between 1 and 9999" } @@ -18,13 +26,6 @@ internal data class FieldBlock( } override val command: CharSequence = "^FB" - override val parameters: Map = addParameters( - "w" to width, - "l" to maxLines, - "s" to lineSpacing, - "a" to alignment.code, - "h" to hangingIndent - ) } /** diff --git a/src/main/kotlin/com/sainsburys/k2zpl/command/FieldData.kt b/src/main/kotlin/com/sainsburys/k2zpl/command/FieldData.kt index cb05247..49bbe52 100644 --- a/src/main/kotlin/com/sainsburys/k2zpl/command/FieldData.kt +++ b/src/main/kotlin/com/sainsburys/k2zpl/command/FieldData.kt @@ -2,9 +2,11 @@ package com.sainsburys.k2zpl.command import com.sainsburys.k2zpl.builder.ZplBuilder -internal data class FieldData(val data: String) : ZplCommand { +internal data class FieldData(val data: String) : ZplCommand( + parameters = listOf("d" to data) +) { + override val command: CharSequence = "^FD" - override val parameters: Map = addParameters("d" to data) override fun build(stringBuilder: StringBuilder): StringBuilder { return stringBuilder diff --git a/src/main/kotlin/com/sainsburys/k2zpl/command/FieldOrigin.kt b/src/main/kotlin/com/sainsburys/k2zpl/command/FieldOrigin.kt index 1321442..37dc892 100644 --- a/src/main/kotlin/com/sainsburys/k2zpl/command/FieldOrigin.kt +++ b/src/main/kotlin/com/sainsburys/k2zpl/command/FieldOrigin.kt @@ -7,18 +7,19 @@ internal data class FieldOrigin( val x: Int, val y: Int, val justification: ZplJustification -) : ZplCommand { +) : ZplCommand( + parameters = listOf( + "x" to x, + "y" to y, + "j" to justification + ) +) { init { require(x in 0..32000) { "x must be within 0 to 32000" } require(y in 0..32000) { "y must be within 0 to 32000" } } override val command: CharSequence = "^FO" - override val parameters: Map = addParameters( - "x" to x, - "y" to y, - "j" to justification - ) } /** diff --git a/src/main/kotlin/com/sainsburys/k2zpl/command/FieldSeparator.kt b/src/main/kotlin/com/sainsburys/k2zpl/command/FieldSeparator.kt index 0f78098..775779a 100644 --- a/src/main/kotlin/com/sainsburys/k2zpl/command/FieldSeparator.kt +++ b/src/main/kotlin/com/sainsburys/k2zpl/command/FieldSeparator.kt @@ -2,7 +2,7 @@ package com.sainsburys.k2zpl.command import com.sainsburys.k2zpl.builder.ZplBuilder -internal data object FieldSeparator : ZplCommand { +internal data object FieldSeparator : ZplCommand() { override val command: CharSequence = "^FS" } diff --git a/src/main/kotlin/com/sainsburys/k2zpl/command/Font.kt b/src/main/kotlin/com/sainsburys/k2zpl/command/Font.kt index 11e6708..249fed4 100644 --- a/src/main/kotlin/com/sainsburys/k2zpl/command/Font.kt +++ b/src/main/kotlin/com/sainsburys/k2zpl/command/Font.kt @@ -9,16 +9,17 @@ internal data class Font( val orientation: ZplFieldOrientation, val height: Int, val width: Int -) : - ZplCommand { +) : ZplCommand( + parameters = listOf( + "o" to orientation, "h" to height, "w" to width + ) +) { init { require(height in 10..32000) { "Height must be between 10 and 32000" } require(width in 10..32000) { "Width must be between 10 and 32000" } } override val command: CharSequence = "^A${font}" - override val parameters: Map = - addParameters("o" to orientation, "h" to height, "w" to width) } diff --git a/src/main/kotlin/com/sainsburys/k2zpl/command/GraphicBox.kt b/src/main/kotlin/com/sainsburys/k2zpl/command/GraphicBox.kt index 092318c..77aa682 100644 --- a/src/main/kotlin/com/sainsburys/k2zpl/command/GraphicBox.kt +++ b/src/main/kotlin/com/sainsburys/k2zpl/command/GraphicBox.kt @@ -9,7 +9,14 @@ internal data class GraphicBox( val thickness: Int = 1, val color: ZplLineColor = ZplLineColor.BLACK, val rounding: Int = 0 -) : ZplCommand { +) : ZplCommand( + parameters = listOf( + "w" to width, + "h" to height, + "t" to thickness, "c" to color.code, + "r" to rounding + ) +) { init { require(width in 1..32000) { "Width must be between 1 and 32000" } require(height in 1..32000) { "Height must be between 1 and 32000" } @@ -18,13 +25,6 @@ internal data class GraphicBox( } override val command: CharSequence = "^GB" - override val parameters: Map = - addParameters( - "w" to width, - "h" to height, - "t" to thickness, "c" to color.code, - "r" to rounding - ) } /** diff --git a/src/main/kotlin/com/sainsburys/k2zpl/command/GraphicField.kt b/src/main/kotlin/com/sainsburys/k2zpl/command/GraphicField.kt index 1790078..9f7e412 100644 --- a/src/main/kotlin/com/sainsburys/k2zpl/command/GraphicField.kt +++ b/src/main/kotlin/com/sainsburys/k2zpl/command/GraphicField.kt @@ -9,7 +9,15 @@ internal data class GraphicField( val totalBytes: Int, val rowBytes: Int, val data: String -) : ZplCommand { +) : ZplCommand( + parameters = listOf( + "f" to format, + "db" to dataBytes, + "tb" to totalBytes, + "rb" to rowBytes, + "d" to data + ) +) { init { require(dataBytes in 1..999999) { "Data bytes must be between 1 and 999999" } require(totalBytes in 1..999999) { "Total bytes must be between 1 and 999999" } @@ -17,13 +25,6 @@ internal data class GraphicField( } override val command: CharSequence = "^GF" - override val parameters: Map = addParameters( - "f" to format, - "db" to dataBytes, - "tb" to totalBytes, - "rb" to rowBytes, - "d" to data - ) } /** diff --git a/src/main/kotlin/com/sainsburys/k2zpl/command/LabelHome.kt b/src/main/kotlin/com/sainsburys/k2zpl/command/LabelHome.kt index 2d48acf..93dc579 100644 --- a/src/main/kotlin/com/sainsburys/k2zpl/command/LabelHome.kt +++ b/src/main/kotlin/com/sainsburys/k2zpl/command/LabelHome.kt @@ -2,9 +2,10 @@ package com.sainsburys.k2zpl.command import com.sainsburys.k2zpl.builder.ZplBuilder -internal data class LabelHome(val x: Int, val y: Int) : ZplCommand { +internal data class LabelHome(val x: Int, val y: Int) : ZplCommand( + parameters = listOf("x" to x, "y" to y) +) { override val command: CharSequence = "^LH" - override val parameters: Map = addParameters("x" to x, "y" to y) } /** diff --git a/src/main/kotlin/com/sainsburys/k2zpl/command/LabelLength.kt b/src/main/kotlin/com/sainsburys/k2zpl/command/LabelLength.kt index 94df7d7..674f218 100644 --- a/src/main/kotlin/com/sainsburys/k2zpl/command/LabelLength.kt +++ b/src/main/kotlin/com/sainsburys/k2zpl/command/LabelLength.kt @@ -2,13 +2,14 @@ package com.sainsburys.k2zpl.command import com.sainsburys.k2zpl.builder.ZplBuilder -internal data class LabelLength(val length: Int) : ZplCommand { +internal data class LabelLength(val length: Int) : ZplCommand( + parameters = listOf("l" to length) +) { init { require(length in 1..32000) { "Length must be between 1 and 32000" } } override val command: CharSequence = "^LL" - override val parameters: Map = addParameters("l" to length) } /** diff --git a/src/main/kotlin/com/sainsburys/k2zpl/command/LazyCommand.kt b/src/main/kotlin/com/sainsburys/k2zpl/command/LazyCommand.kt index 18081f7..debfc7c 100644 --- a/src/main/kotlin/com/sainsburys/k2zpl/command/LazyCommand.kt +++ b/src/main/kotlin/com/sainsburys/k2zpl/command/LazyCommand.kt @@ -4,7 +4,7 @@ package com.sainsburys.k2zpl.command * Allow for lazy evaluation of a ZplCommand. This works by * passing in a block that will be executed when [build] is called. */ -internal class LazyCommand(private val commandBlock: () -> ZplCommand) : ZplCommand { +internal class LazyCommand(private val commandBlock: () -> ZplCommand) : ZplCommand() { override val command: CharSequence get() = "" override fun build(stringBuilder: StringBuilder): StringBuilder { diff --git a/src/main/kotlin/com/sainsburys/k2zpl/command/MediaMode.kt b/src/main/kotlin/com/sainsburys/k2zpl/command/MediaMode.kt index 15d7e4d..b833625 100644 --- a/src/main/kotlin/com/sainsburys/k2zpl/command/MediaMode.kt +++ b/src/main/kotlin/com/sainsburys/k2zpl/command/MediaMode.kt @@ -8,10 +8,10 @@ import com.sainsburys.k2zpl.command.options.ZplYesNo internal data class MediaMode( val mediaMode: ZplMediaMode, val prePeelSelect: ZplYesNo -) : ZplCommand { +) : ZplCommand( + parameters = listOf("m" to mediaMode, "p" to prePeelSelect) +) { override val command: CharSequence = "^MM" - override val parameters: Map = - addParameters("m" to mediaMode, "p" to prePeelSelect) } /** diff --git a/src/main/kotlin/com/sainsburys/k2zpl/command/PrintWidth.kt b/src/main/kotlin/com/sainsburys/k2zpl/command/PrintWidth.kt index 4fa69f5..be48a3c 100644 --- a/src/main/kotlin/com/sainsburys/k2zpl/command/PrintWidth.kt +++ b/src/main/kotlin/com/sainsburys/k2zpl/command/PrintWidth.kt @@ -2,13 +2,14 @@ package com.sainsburys.k2zpl.command import com.sainsburys.k2zpl.builder.ZplBuilder -internal data class PrintWidth(val width: Int) : ZplCommand { +internal data class PrintWidth(val width: Int) : ZplCommand( + parameters = listOf("w" to width) +) { init { require(width in 2 .. 32000) { "Width must be greater than 2. Value is also capped at width of the actual label." } } override val command: CharSequence = "^PW" - override val parameters: Map = addParameters("w" to width) } diff --git a/src/main/kotlin/com/sainsburys/k2zpl/command/StartFormat.kt b/src/main/kotlin/com/sainsburys/k2zpl/command/StartFormat.kt index bbac449..3e93093 100644 --- a/src/main/kotlin/com/sainsburys/k2zpl/command/StartFormat.kt +++ b/src/main/kotlin/com/sainsburys/k2zpl/command/StartFormat.kt @@ -2,7 +2,7 @@ package com.sainsburys.k2zpl.command import com.sainsburys.k2zpl.builder.ZplBuilder -internal data object StartFormat : ZplCommand { +internal data object StartFormat : ZplCommand() { override val command: CharSequence = "^XA" } diff --git a/src/main/kotlin/com/sainsburys/k2zpl/command/ZplCommand.kt b/src/main/kotlin/com/sainsburys/k2zpl/command/ZplCommand.kt index ec45921..59513d3 100644 --- a/src/main/kotlin/com/sainsburys/k2zpl/command/ZplCommand.kt +++ b/src/main/kotlin/com/sainsburys/k2zpl/command/ZplCommand.kt @@ -1,9 +1,15 @@ package com.sainsburys.k2zpl.command -interface ZplCommand { - val command: CharSequence - val parameters: Map get() = addParameters() - fun build(stringBuilder: StringBuilder) = stringBuilder.apply { +import java.util.Collections.unmodifiableMap + +abstract class ZplCommand( + parameters: List> = emptyList() +) { + val parameters: Map = unmodifiableMap(parameters.toMap()) + + abstract val command: CharSequence + + open fun build(stringBuilder: StringBuilder) = stringBuilder.apply { append(command) with(parameters.values.iterator()) { if (hasNext()) { @@ -24,9 +30,3 @@ interface ZplCommand { private fun Iterator.nextNotNull(block: (T) -> Unit) { next()?.let { block(it) } } - -/** - * A shortcut to adding parameters that helps to enforce use of [LinkedHashMap] - * so that entry order is preserved. - */ -internal fun ZplCommand.addParameters(vararg pairs: Pair) = linkedMapOf(*pairs) diff --git a/src/test/kotlin/com/sainsburys/k2zpl/command/ZplCommandTest.kt b/src/test/kotlin/com/sainsburys/k2zpl/command/ZplCommandTest.kt index 0046bb5..5f932a3 100644 --- a/src/test/kotlin/com/sainsburys/k2zpl/command/ZplCommandTest.kt +++ b/src/test/kotlin/com/sainsburys/k2zpl/command/ZplCommandTest.kt @@ -27,37 +27,39 @@ class ZplCommandTest : DescribeSpec({ } }) -class ZplCommandWithoutParameters : ZplCommand { +class ZplCommandWithoutParameters : ZplCommand() { override val command = "^ZC" } -class ZplCommandWithOneParameter : ZplCommand { +class ZplCommandWithOneParameter : ZplCommand( + parameters = listOf("param-one" to "value-one") +) { override val command = "^ZCP" - override val parameters: Map = addParameters( - "param-one" to "value-one" - ) } -class ZplCommandWithMultipleParameters : ZplCommand { - override val command = "^ZCPS" - override val parameters: Map = addParameters( +class ZplCommandWithMultipleParameters : ZplCommand( + parameters = listOf( "param-one" to "value-one", "param-two" to "value-two" ) +) { + override val command = "^ZCPS" } -class ZplCommandWitNullFirstParameter : ZplCommand { - override val command = "^ZCPN" - override val parameters: Map = addParameters( +class ZplCommandWitNullFirstParameter : ZplCommand( + parameters = listOf( "param-one" to null, "param-two" to "value-two" ) +) { + override val command = "^ZCPN" } -class ZplCommandWitNullSecondParameter : ZplCommand { - override val command = "^ZCPNS" - override val parameters: Map = addParameters( +class ZplCommandWitNullSecondParameter : ZplCommand( + parameters = listOf( "param-one" to "value-one", "param-two" to null ) +) { + override val command = "^ZCPNS" } \ No newline at end of file