Skip to content
This repository was archived by the owner on Oct 15, 2024. It is now read-only.

Commit a630de0

Browse files
committed
refactor(bson): changed BsonDocument and BsonArray to be immutable
- added helper types aliases BsonList and BsonMap BREAKING-CHANGE: - replaced MutableBsonDocument with MutableBsonMap - replaced MutableBsonArray with MutableBsonList - replaced `BsonArray(List)` constructor with `List.toBsonArray()` copy extension - replaced `BsonDocument(Map)` constructor with `Map.toBsonDocument()` copy extension - renamed `MutableBsonDocumentField` to `MutableBsonMapField`
1 parent 4a5cf36 commit a630de0

12 files changed

Lines changed: 276 additions & 202 deletions

File tree

bson/src/main/kotlin/BsonArray.kt

Lines changed: 67 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -20,56 +20,23 @@ import java.math.BigDecimal
2020
/* ============= ------------------ ============= */
2121

2222
/**
23-
* A default implementation of [BsonArray].
23+
* A list containing only items of type [BsonElement].
2424
*
25-
* @author LSafer
2625
* @since 2.0.0
2726
*/
28-
internal class BsonArrayImpl(
29-
private val content: List<BsonElement>
30-
) : BsonArray, List<BsonElement> by content {
31-
override fun equals(other: Any?) =
32-
content == other
33-
34-
override fun hashCode() =
35-
content.hashCode()
36-
37-
override fun toString() =
38-
content.joinToString(",", "[", "]")
39-
}
27+
typealias BsonList = List<BsonElement>
4028

4129
/* ============= ------------------ ============= */
4230

4331
/**
44-
* A block of code building a bson array.
32+
* A mutable list containing only items of type [BsonElement].
4533
*
46-
* @since 2.0.0
47-
*/
48-
typealias BsonArrayBlock = MutableBsonArray.() -> Unit
49-
50-
/**
51-
* A mutable implementation of [BsonArray].
34+
* This interface will be replaced with a `typealias` once
35+
* kotlin context receivers is stable.
5236
*
53-
* @author LSafer
5437
* @since 2.0.0
5538
*/
56-
class MutableBsonArray(
57-
/**
58-
* The array currently building.
59-
*/
60-
private val content: MutableList<BsonElement> = mutableListOf()
61-
) : BsonArray, MutableList<BsonElement> by content {
62-
/* ============= ------------------ ============= */
63-
64-
override fun equals(other: Any?) =
65-
content == other
66-
67-
override fun hashCode() =
68-
content.hashCode()
69-
70-
override fun toString() =
71-
content.joinToString(",", "[", "]")
72-
39+
interface MutableBsonList : BsonList, MutableList<BsonElement> {
7340
/* ============= ------------------ ============= */
7441

7542
/**
@@ -79,8 +46,8 @@ class MutableBsonArray(
7946
*/
8047
@BsonConstructorMarker
8148
fun by(value: BsonElement?) {
82-
value ?: return run { content += bnull }
83-
content += value
49+
value ?: return run { this += bnull }
50+
this += value
8451
}
8552

8653
/**
@@ -90,8 +57,8 @@ class MutableBsonArray(
9057
*/
9158
@BsonConstructorMarker
9259
fun by(value: BsonDocument?) {
93-
value ?: return run { content += bnull }
94-
content += value
60+
value ?: return run { this += bnull }
61+
this += value
9562
}
9663

9764
/**
@@ -101,8 +68,8 @@ class MutableBsonArray(
10168
*/
10269
@BsonConstructorMarker
10370
fun by(value: BsonArray?) {
104-
value ?: return run { content += bnull }
105-
content.add(value)
71+
value ?: return run { this += bnull }
72+
this.add(value)
10673
}
10774

10875
/* ============= ------------------ ============= */
@@ -116,8 +83,8 @@ class MutableBsonArray(
11683
*/
11784
@BsonConstructorMarker
11885
fun by(value: Map<String, BsonElement>?) {
119-
value ?: return run { content += bnull }
120-
content += value.toBsonDocument()
86+
value ?: return run { this += bnull }
87+
this += value.toBsonDocument()
12188
}
12289

12390
/**
@@ -129,8 +96,8 @@ class MutableBsonArray(
12996
*/
13097
@BsonConstructorMarker
13198
fun by(value: List<BsonElement>?) {
132-
value ?: return run { content += bnull }
133-
content.add(value.toBsonArray())
99+
value ?: return run { this += bnull }
100+
this.add(value.toBsonArray())
134101
}
135102

136103
/**
@@ -142,8 +109,8 @@ class MutableBsonArray(
142109
*/
143110
@BsonConstructorMarker
144111
fun by(value: String?) {
145-
value ?: return run { content += bnull }
146-
content += value.b
112+
value ?: return run { this += bnull }
113+
this += value.b
147114
}
148115

149116
/**
@@ -155,8 +122,8 @@ class MutableBsonArray(
155122
*/
156123
@BsonConstructorMarker
157124
fun by(value: ObjectId?) {
158-
value ?: return run { content += bnull }
159-
content += value.b
125+
value ?: return run { this += bnull }
126+
this += value.b
160127
}
161128

162129
/**
@@ -168,8 +135,8 @@ class MutableBsonArray(
168135
*/
169136
@BsonConstructorMarker
170137
fun by(value: Id<*>?) {
171-
value ?: return run { content += bnull }
172-
content += value.b
138+
value ?: return run { this += bnull }
139+
this += value.b
173140
}
174141

175142
/**
@@ -179,11 +146,12 @@ class MutableBsonArray(
179146
*
180147
* The given [value] will be wrapped using [BsonArray] and [Id.b].
181148
*/
149+
@Suppress("INAPPLICABLE_JVM_NAME")
182150
@JvmName("byIdList")
183151
@BsonConstructorMarker
184152
fun by(value: List<Id<*>>?) {
185-
value ?: return run { content += bnull }
186-
content.add(value.map { it.b }.toBsonArray())
153+
value ?: return run { this += bnull }
154+
this.add(value.map { it.b }.toBsonArray())
187155
}
188156

189157
/**
@@ -195,8 +163,8 @@ class MutableBsonArray(
195163
*/
196164
@BsonConstructorMarker
197165
fun by(value: Decimal128?) {
198-
value ?: return run { content += bnull }
199-
content += value.b
166+
value ?: return run { this += bnull }
167+
this += value.b
200168
}
201169

202170
/**
@@ -208,8 +176,8 @@ class MutableBsonArray(
208176
*/
209177
@BsonConstructorMarker
210178
fun by(value: BigDecimal?) {
211-
value ?: return run { content += bnull }
212-
content += value.b
179+
value ?: return run { this += bnull }
180+
this += value.b
213181
}
214182

215183
/* ============= ------------------ ============= */
@@ -223,8 +191,8 @@ class MutableBsonArray(
223191
*/
224192
@BsonConstructorMarker
225193
fun by(value: Boolean?) {
226-
value ?: return run { content += bnull }
227-
content += value.b
194+
value ?: return run { this += bnull }
195+
this += value.b
228196
}
229197

230198
/**
@@ -236,8 +204,8 @@ class MutableBsonArray(
236204
*/
237205
@BsonConstructorMarker
238206
fun by(value: Int?) {
239-
value ?: return run { content += bnull }
240-
content += value.b
207+
value ?: return run { this += bnull }
208+
this += value.b
241209
}
242210

243211
/**
@@ -249,8 +217,8 @@ class MutableBsonArray(
249217
*/
250218
@BsonConstructorMarker
251219
fun by(value: Long?) {
252-
value ?: return run { content += bnull }
253-
content += value.b
220+
value ?: return run { this += bnull }
221+
this += value.b
254222
}
255223

256224
/**
@@ -262,8 +230,8 @@ class MutableBsonArray(
262230
*/
263231
@BsonConstructorMarker
264232
fun by(value: Double?) {
265-
value ?: return run { content += bnull }
266-
content += value.b
233+
value ?: return run { this += bnull }
234+
this += value.b
267235
}
268236

269237
/* ============= ------------------ ============= */
@@ -273,7 +241,7 @@ class MutableBsonArray(
273241
*/
274242
@BsonConstructorMarker
275243
fun by(block: BsonDocumentBlock) {
276-
content += BsonDocument(block)
244+
this += BsonDocument(block)
277245
}
278246

279247
/* ============= ------------------ ============= */
@@ -283,10 +251,38 @@ class MutableBsonArray(
283251
*/
284252
@BsonConstructorMarker
285253
fun byAll(list: List<BsonElement>) {
286-
content += list
254+
this += list
287255
}
288256

289257
/* ============= ------------------ ============= */
290258
}
291259

260+
/**
261+
* Return an empty new [MutableBsonList].
262+
*
263+
* This function will be obsolete once kotlin
264+
* context receivers is stable.
265+
*
266+
* @see mutableListOf
267+
* @since 2.0.0
268+
*/
269+
fun mutableBsonListOf(): MutableBsonList {
270+
return mutableListOf<BsonElement>()
271+
.asMutableBsonList()
272+
}
273+
274+
/**
275+
* Returns a new [MutableBsonList] with the given elements.
276+
*
277+
* This function will be obsolete once kotlin
278+
* context receivers is stable.
279+
*
280+
* @see mutableListOf
281+
* @since 2.0.0
282+
*/
283+
fun mutableBsonListOf(vararg elements: BsonElement): MutableBsonList {
284+
return mutableListOf(*elements)
285+
.asMutableBsonList()
286+
}
287+
292288
/* ============= ------------------ ============= */

0 commit comments

Comments
 (0)