@@ -40,142 +40,156 @@ public sealed class CborElement(
40
40
public var tags: ULongArray = tags
41
41
internal set
42
42
43
+ override fun equals (other : Any? ): Boolean {
44
+ if (this == = other) return true
45
+ if (other !is CborElement ) return false
46
+
47
+ if (! tags.contentEquals(other.tags)) return false
48
+
49
+ return true
50
+ }
51
+
52
+ override fun hashCode (): Int {
53
+ return tags.contentHashCode()
54
+ }
55
+
43
56
}
44
57
45
58
/* *
46
59
* Class representing CBOR primitive value.
47
60
* CBOR primitives include numbers, strings, booleans, byte arrays and special null value [CborNull].
48
61
*/
49
62
@Serializable(with = CborPrimitiveSerializer ::class )
50
- public sealed class CborPrimitive (
63
+ public sealed class CborPrimitive <T : Any >(
64
+ public val value : T ,
51
65
tags : ULongArray = ulongArrayOf()
52
- ) : CborElement(tags)
66
+ ) : CborElement(tags) {
67
+ override fun equals (other : Any? ): Boolean {
68
+ if (this == = other) return true
69
+ if (other !is CborPrimitive <* >) return false
70
+ if (! super .equals(other)) return false
71
+
72
+ if (value != other.value) return false
73
+
74
+ return true
75
+ }
76
+
77
+ override fun hashCode (): Int {
78
+ var result = super .hashCode()
79
+ result = 31 * result + value.hashCode()
80
+ return result
81
+ }
82
+
83
+ override fun toString (): String {
84
+ return " CborPrimitive(" +
85
+ " kind=${value::class .simpleName} , " +
86
+ " tags=${tags.joinToString()} , " +
87
+ " value=$value " +
88
+ " )"
89
+ }
90
+ }
91
+
92
+ public sealed class CborInt <T : Any >(
93
+ tags : ULongArray = ulongArrayOf(),
94
+ value : T ,
95
+ ) : CborPrimitive<T>(value, tags) {
96
+ public companion object {
97
+ public operator fun invoke (
98
+ value : Long ,
99
+ tags : ULongArray = ulongArrayOf()
100
+ ): CborInt <* > = if (value >= 0 ) CborPositiveInt (value.toULong(), tags) else CborNegativeInt (value, tags)
101
+
102
+ public operator fun invoke (
103
+ value : ULong ,
104
+ tags : ULongArray = ulongArrayOf()
105
+ ): CborInt <ULong > = CborPositiveInt (value, tags)
106
+ }
107
+ }
53
108
54
109
/* *
55
110
* Class representing signed CBOR integer (major type 1).
56
111
*/
57
112
@Serializable(with = CborIntSerializer ::class )
58
113
public class CborNegativeInt (
59
- public val value : Long ,
114
+ value : Long ,
60
115
tags : ULongArray = ulongArrayOf()
61
- ) : CborPrimitive (tags) {
116
+ ) : CborInt<Long> (tags, value ) {
62
117
init {
63
118
require(value < 0 ) { " Number must be negative: $value " }
64
119
}
65
-
66
- override fun equals (other : Any? ): Boolean =
67
- other is CborNegativeInt && other.value == value && other.tags.contentEquals(tags)
68
-
69
- override fun hashCode (): Int = value.hashCode() * 31 + tags.contentHashCode()
70
120
}
71
121
72
122
/* *
73
123
* Class representing unsigned CBOR integer (major type 0).
74
124
*/
75
125
@Serializable(with = CborUIntSerializer ::class )
76
126
public class CborPositiveInt (
77
- public val value : ULong ,
127
+ value : ULong ,
78
128
tags : ULongArray = ulongArrayOf()
79
- ) : CborPrimitive(tags) {
80
-
81
- override fun equals (other : Any? ): Boolean =
82
- other is CborPositiveInt && other.value == value && other.tags.contentEquals(tags)
83
-
84
- override fun hashCode (): Int = value.hashCode() * 31 + tags.contentHashCode()
85
- }
86
-
87
- public fun CborInt (
88
- value : Long ,
89
- tags : ULongArray = ulongArrayOf()
90
- ): CborPrimitive = if (value >= 0 ) CborPositiveInt (value.toULong(), tags) else CborNegativeInt (value, tags)
129
+ ) : CborInt<ULong>(tags, value)
91
130
92
131
/* *
93
132
* Class representing CBOR floating point value (major type 7).
94
133
*/
95
134
@Serializable(with = CborDoubleSerializer ::class )
96
135
public class CborDouble (
97
- public val value : Double ,
136
+ value : Double ,
98
137
tags : ULongArray = ulongArrayOf()
99
- ) : CborPrimitive(tags) {
100
-
101
- override fun equals (other : Any? ): Boolean =
102
- other is CborDouble && other.value == value && other.tags.contentEquals(tags)
103
-
104
- override fun hashCode (): Int = value.hashCode() * 31 + tags.contentHashCode()
105
- }
138
+ ) : CborPrimitive<Double>(value, tags)
106
139
107
140
/* *
108
141
* Class representing CBOR string value.
109
142
*/
110
143
@Serializable(with = CborStringSerializer ::class )
111
144
public class CborString (
112
- public val value : String ,
145
+ value : String ,
113
146
tags : ULongArray = ulongArrayOf()
114
- ) : CborPrimitive(tags) {
115
-
116
- override fun equals (other : Any? ): Boolean =
117
- other is CborString && other.value == value && other.tags.contentEquals(tags)
118
-
119
- override fun hashCode (): Int = value.hashCode() * 31 + tags.contentHashCode()
120
- }
147
+ ) : CborPrimitive<String>(value, tags)
121
148
122
149
/* *
123
150
* Class representing CBOR boolean value.
124
151
*/
125
152
@Serializable(with = CborBooleanSerializer ::class )
126
153
public class CborBoolean (
127
- private val value : Boolean ,
154
+ value : Boolean ,
128
155
tags : ULongArray = ulongArrayOf()
129
- ) : CborPrimitive(tags) {
130
-
131
- /* *
132
- * Returns the boolean value.
133
- */
134
- public val boolean: Boolean get() = value
135
-
136
- override fun equals (other : Any? ): Boolean =
137
- other is CborBoolean && other.value == value && other.tags.contentEquals(tags)
138
-
139
- override fun hashCode (): Int = value.hashCode() * 31 + tags.contentHashCode()
140
- }
156
+ ) : CborPrimitive<Boolean>(value, tags)
141
157
142
158
/* *
143
159
* Class representing CBOR byte string value.
144
160
*/
145
161
@Serializable(with = CborByteStringSerializer ::class )
146
162
public class CborByteString (
147
- private val value : ByteArray ,
163
+ value : ByteArray ,
148
164
tags : ULongArray = ulongArrayOf()
149
- ) : CborPrimitive(tags) {
150
-
151
- /* *
152
- * Returns the byte array value.
153
- */
154
- public val bytes: ByteArray get() = value.copyOf()
165
+ ) : CborPrimitive<ByteArray>(value, tags) {
166
+ override fun equals (other : Any? ): Boolean {
167
+ if (this == = other) return true
168
+ if (other !is CborByteString ) return false
169
+ if (! super .equals(other)) return false
170
+ return value.contentEquals(other.value)
171
+ }
172
+ override fun toString (): String {
173
+ return " CborPrimitive(" +
174
+ " kind=${value::class .simpleName} , " +
175
+ " tags=${tags.joinToString()} , " +
176
+ " value=h'${value.toHexString()} " +
177
+ " )"
178
+ }
155
179
156
- override fun equals (other : Any? ): Boolean =
157
- other is CborByteString && other.value.contentEquals(value) && other.tags.contentEquals(tags)
180
+ override fun hashCode (): Int {
181
+ var result = super .hashCode()
182
+ result = 31 * result + (value.contentHashCode())
183
+ return result
184
+ }
158
185
159
- override fun hashCode (): Int = value.contentHashCode() * 31 + tags.contentHashCode()
160
186
}
161
187
162
188
/* *
163
189
* Class representing CBOR `null` value
164
190
*/
165
191
@Serializable(with = CborNullSerializer ::class )
166
- public class CborNull (tags : ULongArray = ulongArrayOf()) : CborPrimitive(tags) {
167
- // Note: CborNull is an object, so it cannot have constructor parameters for tags
168
- // If tags are needed for null values, this would need to be changed to a class
169
- override fun equals (other : Any? ): Boolean {
170
- if (this == = other) return true
171
- if (other !is CborNull ) return false
172
- return true
173
- }
174
-
175
- override fun hashCode (): Int {
176
- return this ::class .hashCode()
177
- }
178
- }
192
+ public class CborNull (tags : ULongArray = ulongArrayOf()) : CborPrimitive<Unit>(Unit , tags)
179
193
180
194
/* *
181
195
* Class representing CBOR map, consisting of key-value pairs, where both key and value are arbitrary [CborElement]
@@ -193,8 +207,13 @@ public class CborMap(
193
207
other is CborMap && other.content == content && other.tags.contentEquals(tags)
194
208
195
209
public override fun hashCode (): Int = content.hashCode() * 31 + tags.contentHashCode()
210
+ override fun toString (): String {
211
+ return " CborMap(" +
212
+ " tags=${tags.joinToString()} , " +
213
+ " content=$content " +
214
+ " )"
215
+ }
196
216
197
- public override fun toString (): String = content.toString()
198
217
}
199
218
200
219
/* *
@@ -213,6 +232,11 @@ public class CborList(
213
232
other is CborList && other.content == content && other.tags.contentEquals(tags)
214
233
215
234
public override fun hashCode (): Int = content.hashCode() * 31 + tags.contentHashCode()
235
+ override fun toString (): String {
236
+ return " CborList(" +
237
+ " tags=${tags.joinToString()} , " +
238
+ " content=$content " +
239
+ " )"
240
+ }
216
241
217
- public override fun toString (): String = content.toString()
218
242
}
0 commit comments