Skip to content

Commit 6af2f69

Browse files
committed
Generate IDs locally in ObjectPools
1 parent f780bab commit 6af2f69

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

adapter/src/main/kotlin/org/javacs/ktda/util/ObjectPool.kt

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ private data class ObjectMapping<O, V> (
1212
val value: V
1313
)
1414

15-
private var currentID = 1L
16-
1715
/**
1816
* Maps objects of owners to multiple owned values.
1917
* To store and retrieve objects, unique ids are used.
2018
*/
2119
class ObjectPool<O, V> {
2220
private val mappingsByID = mutableMapOf<Long, ObjectMapping<O, V>>()
2321
private val mappingsByOwner = mutableMapOf<O, MutableSet<ObjectMapping<O, V>>>()
22+
23+
private var currentID = 1L
2424

2525
val empty: Boolean
2626
get() = mappingsByID.isEmpty()
@@ -76,12 +76,10 @@ class ObjectPool<O, V> {
7676
fun containsID(id: Long) = mappingsByID.contains(id)
7777

7878
private fun nextID(): Long {
79-
var id = currentID
79+
do {
80+
currentID++
81+
} while (containsID(currentID));
8082

81-
while (containsID(id)) {
82-
id += 1
83-
}
84-
85-
return id
83+
return currentID
8684
}
8785
}

0 commit comments

Comments
 (0)