From 34703df24fd58374626bc91888b63985942fe1ae Mon Sep 17 00:00:00 2001 From: wrongwrong Date: Sun, 10 Jan 2021 14:43:27 +0900 Subject: [PATCH] Add argument bucket --- .../jackson/module/kotlin/ArgumentBucket.kt | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 src/main/kotlin/com/fasterxml/jackson/module/kotlin/ArgumentBucket.kt diff --git a/src/main/kotlin/com/fasterxml/jackson/module/kotlin/ArgumentBucket.kt b/src/main/kotlin/com/fasterxml/jackson/module/kotlin/ArgumentBucket.kt new file mode 100644 index 000000000..b27422c72 --- /dev/null +++ b/src/main/kotlin/com/fasterxml/jackson/module/kotlin/ArgumentBucket.kt @@ -0,0 +1,47 @@ +package com.fasterxml.jackson.module.kotlin + +import java.util.AbstractMap.SimpleEntry as Entry +import kotlin.reflect.KParameter + +internal class ArgumentBucket(private val parameters: List) : Map { + // This array is sorted by KParameter::index. + val valueArray: Array = arrayOfNulls(parameters.size) + private val initializationStatuses: BooleanArray = BooleanArray(parameters.size) + private var count = 0 + + operator fun set(key: KParameter, value: Any?): Any? { + return valueArray[key.index].apply { + valueArray[key.index] = value + + if (!initializationStatuses[key.index]) { + initializationStatuses[key.index] = true + count++ + } + } + } + + fun isFullInitialized(): Boolean = parameters.size == count + + override val entries: Set> + get() = valueArray.withIndex() + .filter { (i, _) -> initializationStatuses[i] } + .map { (i, arg) -> Entry(parameters[i], arg) } + .toSet() + override val keys: Set + get() = parameters + .filterIndexed { i, _ -> initializationStatuses[i] } + .toSet() + override val size: Int + get() = count + override val values: Collection + get() = valueArray.filterIndexed { i, _ -> initializationStatuses[i] } + + override fun containsKey(key: KParameter): Boolean = initializationStatuses[key.index] + + override fun containsValue(value: Any?): Boolean = valueArray.withIndex() + .any { (i, arg) -> initializationStatuses[i] && value == arg } + + override fun get(key: KParameter): Any? = valueArray[key.index] + + override fun isEmpty(): Boolean = count == 0 +}