Cleaned up random ID allocator

This commit is contained in:
Robinson 2023-05-26 15:34:26 +02:00
parent 6200c5e887
commit 784d0ecf02
No known key found for this signature in database
GPG Key ID: 8E7DB78588BD6F5C

View File

@ -33,7 +33,6 @@ import java.security.SecureRandom
* @param max The maximum ID (exclusive) * @param max The maximum ID (exclusive)
*/ */
class RandomId65kAllocator(private val min: Int, max: Int) { class RandomId65kAllocator(private val min: Int, max: Int) {
constructor(size: Int): this(1, size + 1) constructor(size: Int): this(1, size + 1)
@ -41,17 +40,9 @@ class RandomId65kAllocator(private val min: Int, max: Int) {
private val maxAssignments: Int private val maxAssignments: Int
private val assigned = atomic(0) private val assigned = atomic(0)
// DEBUGGING
private val debugChecks = mutableListOf<Int>()
init { init {
// IllegalArgumentException // IllegalArgumentException
require(max >= min) { require(max >= min) { "Maximum value $max must be >= minimum value $min" }
"Maximum value $max must be >= minimum value $min"
}
val max65k = Short.MAX_VALUE * 2 val max65k = Short.MAX_VALUE * 2
maxAssignments = (max - min).coerceIn(1, max65k) maxAssignments = (max - min).coerceIn(1, max65k)
@ -82,7 +73,6 @@ class RandomId65kAllocator(private val min: Int, max: Int) {
assigned.getAndIncrement() assigned.getAndIncrement()
val id = cache.take() val id = cache.take()
debugChecks.add(id)
return id return id
} }
@ -94,10 +84,9 @@ class RandomId65kAllocator(private val min: Int, max: Int) {
fun free(id: Int) { fun free(id: Int) {
val assigned = assigned.decrementAndGet() val assigned = assigned.decrementAndGet()
if (assigned < 0) { if (assigned < 0) {
throw AllocationException("Unequal allocate/free method calls (too many 'free' calls).") throw AllocationException("Unequal allocate/free method calls attempting to free [$id] (too many 'free' calls).")
} }
cache.put(id) cache.put(id)
debugChecks.remove(id)
} }
fun isEmpty(): Boolean { fun isEmpty(): Boolean {
@ -105,6 +94,6 @@ class RandomId65kAllocator(private val min: Int, max: Int) {
} }
override fun toString(): String { override fun toString(): String {
return "$assigned (${debugChecks.joinToString()})" return "$assigned"
} }
} }