@@ -28,136 +28,66 @@ public sealed class CborElement
28
28
*/
29
29
@Serializable(with = CborPrimitiveSerializer ::class )
30
30
public sealed class CborPrimitive : CborElement () {
31
- /* *
32
- * Content of given element as string. For [CborNull], this method returns a "null" string.
33
- * [CborPrimitive.contentOrNull] should be used for [CborNull] to get a `null`.
34
- */
35
- public abstract val content: String
36
31
37
- public override fun toString (): String = content
38
32
}
39
33
40
34
/* *
41
- * Sealed class representing CBOR number value.
42
- * Can be either [Signed] or [Unsigned].
35
+ * Class representing signed CBOR integer (major type 1).
43
36
*/
44
- @Serializable(with = CborNumberSerializer ::class )
45
- public sealed class CborNumber : CborPrimitive () {
46
- /* *
47
- * Returns the value as a [Byte].
48
- */
49
- public abstract val byte: Byte
37
+ @Serializable(with = CborIntSerializer ::class )
38
+ public class CborNegativeInt (public val value : Long ) : CborPrimitive() {
39
+ init {
40
+ require(value < 0 ) { " Number must be negative: $value " }
41
+ }
50
42
51
- /* *
52
- * Returns the value as a [Short].
53
- */
54
- public abstract val short: Short
43
+ override fun equals (other : Any? ): Boolean {
44
+ if (this == = other) return true
45
+ if (other == null || this ::class != other::class ) return false
46
+ other as CborNegativeInt
47
+ return value == other.value
48
+ }
55
49
56
- /* *
57
- * Returns the value as an [Int].
58
- */
59
- public abstract val int: Int
50
+ override fun hashCode (): Int = value.hashCode()
51
+ }
60
52
61
- /* *
62
- * Returns the value as a [Long].
63
- */
64
- public abstract val long: Long
53
+ /* *
54
+ * Class representing unsigned CBOR integer (major type 0).
55
+ */
56
+ @Serializable(with = CborUIntSerializer ::class )
57
+ public class CborPositiveInt (public val value : ULong ) : CborPrimitive() {
65
58
66
- /* *
67
- * Returns the value as a [Float].
68
- */
69
- public abstract val float: Float
59
+ override fun equals (other : Any? ): Boolean {
60
+ if (this == = other) return true
61
+ if (other == null || this ::class != other::class ) return false
62
+ other as CborPositiveInt
63
+ return value == other.value
64
+ }
70
65
71
- /* *
72
- * Returns the value as a [Double].
73
- */
74
- public abstract val double: Double
66
+ override fun hashCode (): Int = value.hashCode()
67
+ }
75
68
76
- /* *
77
- * Class representing a signed CBOR number value.
78
- */
79
- public class Signed (@Contextual private val value : Number ) : CborNumber() {
80
- override val content: String get() = value.toString()
81
- override val byte: Byte get() = value.toByte()
82
- override val short: Short get() = value.toShort()
83
- override val int: Int get() = value.toInt()
84
- override val long: Long get() = value.toLong()
85
- override val float: Float get() = value.toFloat()
86
- override val double: Double get() = value.toDouble()
87
-
88
- override fun equals (other : Any? ): Boolean {
89
- if (this == = other) return true
90
- if (other == null ) return false
91
-
92
- when (other) {
93
- is Signed -> {
94
- // Compare as double to handle different numeric types
95
- return when {
96
- // For integers, compare as long to avoid precision loss
97
- value is Byte || value is Short || value is Int || value is Long ||
98
- other.value is Byte || other.value is Short || other.value is Int || other.value is Long -> {
99
- value.toLong() == other.value.toLong()
100
- }
101
- // For floating point, compare as double
102
- else -> {
103
- value.toDouble() == other.value.toDouble()
104
- }
105
- }
106
- }
107
- is Unsigned -> {
108
- // Only compare if both are non-negative integers
109
- if (value is Byte || value is Short || value is Int || value is Long ) {
110
- val longValue = value.toLong()
111
- return longValue >= 0 && longValue.toULong() == other.long.toULong()
112
- }
113
- return false
114
- }
115
- else -> return false
116
- }
117
- }
118
-
119
- override fun hashCode (): Int = value.hashCode()
120
- }
69
+ /* *
70
+ * Class representing CBOR floating point value (major type 7).
71
+ */
72
+ @Serializable(with = CborDoubleSerializer ::class )
73
+ public class CborDouble (public val value : Double ) : CborPrimitive() {
121
74
122
- /* *
123
- * Class representing an unsigned CBOR number value.
124
- */
125
- public class Unsigned (private val value : ULong ) : CborNumber() {
126
- override val content: String get() = value.toString()
127
- override val byte: Byte get() = value.toByte()
128
- override val short: Short get() = value.toShort()
129
- override val int: Int get() = value.toInt()
130
- override val long: Long get() = value.toLong()
131
- override val float: Float get() = value.toFloat()
132
- override val double: Double get() = value.toDouble()
133
-
134
- override fun equals (other : Any? ): Boolean {
135
- if (this == = other) return true
136
- if (other == null ) return false
137
-
138
- when (other) {
139
- is Unsigned -> {
140
- return value == other.long.toULong()
141
- }
142
- is Signed -> {
143
- // Only compare if the signed value is non-negative
144
- val otherLong = other.long
145
- return otherLong >= 0 && value == otherLong.toULong()
146
- }
147
- else -> return false
148
- }
149
- }
150
-
151
- override fun hashCode (): Int = value.hashCode()
75
+ override fun equals (other : Any? ): Boolean {
76
+ if (this == = other) return true
77
+ if (other == null || this ::class != other::class ) return false
78
+ other as CborDouble
79
+ return value == other.value
152
80
}
81
+
82
+ override fun hashCode (): Int = value.hashCode()
153
83
}
154
84
85
+
155
86
/* *
156
87
* Class representing CBOR string value.
157
88
*/
158
89
@Serializable(with = CborStringSerializer ::class )
159
- public class CborString (private val value : String ) : CborPrimitive() {
160
- override val content: String get() = value
90
+ public class CborString (public val value : String ) : CborPrimitive() {
161
91
162
92
override fun equals (other : Any? ): Boolean {
163
93
if (this == = other) return true
@@ -174,7 +104,6 @@ public class CborString(private val value: String) : CborPrimitive() {
174
104
*/
175
105
@Serializable(with = CborBooleanSerializer ::class )
176
106
public class CborBoolean (private val value : Boolean ) : CborPrimitive() {
177
- override val content: String get() = value.toString()
178
107
179
108
/* *
180
109
* Returns the boolean value.
@@ -196,7 +125,6 @@ public class CborBoolean(private val value: Boolean) : CborPrimitive() {
196
125
*/
197
126
@Serializable(with = CborByteStringSerializer ::class )
198
127
public class CborByteString (private val value : ByteArray ) : CborPrimitive() {
199
- override val content: String get() = value.contentToString()
200
128
201
129
/* *
202
130
* Returns the byte array value.
@@ -218,7 +146,6 @@ public class CborByteString(private val value: ByteArray) : CborPrimitive() {
218
146
*/
219
147
@Serializable(with = CborNullSerializer ::class )
220
148
public object CborNull : CborPrimitive() {
221
- override val content: String = " null"
222
149
}
223
150
224
151
/* *
@@ -237,6 +164,7 @@ public class CborMap(
237
164
other as CborMap
238
165
return content == other.content
239
166
}
167
+
240
168
public override fun hashCode (): Int = content.hashCode()
241
169
public override fun toString (): String {
242
170
return content.entries.joinToString(
@@ -262,6 +190,7 @@ public class CborList(private val content: List<CborElement>) : CborElement(), L
262
190
other as CborList
263
191
return content == other.content
264
192
}
193
+
265
194
public override fun hashCode (): Int = content.hashCode()
266
195
public override fun toString (): String = content.joinToString(prefix = " [" , postfix = " ]" , separator = " , " )
267
196
}
@@ -295,11 +224,25 @@ public val CborElement.cborNull: CborNull
295
224
get() = this as ? CborNull ? : error(" CborNull" )
296
225
297
226
/* *
298
- * Convenience method to get current element as [CborNumber]
299
- * @throws IllegalArgumentException if current element is not a [CborNumber]
227
+ * Convenience method to get current element as [CborNegativeInt]
228
+ * @throws IllegalArgumentException if current element is not a [CborNegativeInt]
229
+ */
230
+ public val CborElement .cborNegativeInt: CborNegativeInt
231
+ get() = this as ? CborNegativeInt ? : error(" CborNegativeInt" )
232
+
233
+ /* *
234
+ * Convenience method to get current element as [CborPositiveInt]
235
+ * @throws IllegalArgumentException if current element is not a [CborPositiveInt]
236
+ */
237
+ public val CborElement .cborPositiveInt: CborPositiveInt
238
+ get() = this as ? CborPositiveInt ? : error(" CborPositiveInt" )
239
+
240
+ /* *
241
+ * Convenience method to get current element as [CborDouble]
242
+ * @throws IllegalArgumentException if current element is not a [CborDouble]
300
243
*/
301
- public val CborElement .cborNumber : CborNumber
302
- get() = this as ? CborNumber ? : error(" CborNumber " )
244
+ public val CborElement .cborDouble : CborDouble
245
+ get() = this as ? CborDouble ? : error(" CborDouble " )
303
246
304
247
/* *
305
248
* Convenience method to get current element as [CborString]
@@ -322,11 +265,6 @@ public val CborElement.cborBoolean: CborBoolean
322
265
public val CborElement .cborByteString: CborByteString
323
266
get() = this as ? CborByteString ? : error(" CborByteString" )
324
267
325
- /* *
326
- * Content of the given element as string or `null` if current element is [CborNull]
327
- */
328
- public val CborPrimitive .contentOrNull: String? get() = if (this is CborNull ) null else content
329
-
330
268
/* *
331
269
* Creates a [CborMap] from the given map entries.
332
270
*/
@@ -338,4 +276,4 @@ public fun CborMap(vararg pairs: Pair<CborElement, CborElement>): CborMap = Cbor
338
276
public fun CborList (vararg elements : CborElement ): CborList = CborList (listOf (* elements))
339
277
340
278
private fun CborElement.error (element : String ): Nothing =
341
- throw IllegalArgumentException (" Element ${this ::class } is not a $element " )
279
+ throw IllegalArgumentException (" Element ${this ::class } is not a $element " )
0 commit comments