@@ -2,47 +2,60 @@ package com.mapk.core
22
33import kotlin.reflect.KParameter
44
5- internal class BucketGenerator (
6- private val bucket : Array <Any ?>,
7- private val instancePair : Pair <KParameter , Any ?>? ,
8- private val initializationStatus : Int ,
9- private val initializeMask : List <Int >
10- ) {
11- private val completionValue: Int = initializeMask.reduce { l, r -> l or r }
12- private val bucketSize = bucket.size
13-
14- fun generate (): ArgumentBucket {
15- val tempMap = HashMap <KParameter , Any ?>(bucketSize, 1.0f )
16- instancePair?.run { tempMap[this .first] = this .second }
17-
18- return ArgumentBucket (
19- bucket.copyOf(),
20- tempMap,
21- initializationStatus,
22- initializeMask,
23- completionValue
24- )
25- }
26- }
27-
285class ArgumentBucket internal constructor(
29- internal val bucket : Array <Any ?>,
30- internal val bucketMap : MutableMap < KParameter , Any ?>,
6+ private val keyArray : Array <KParameter ?>,
7+ internal val valueArray : Array < Any ?>,
318 private var initializationStatus : Int ,
329 private val initializeMask : List <Int >,
3310 private val completionValue : Int
34- ) {
11+ ) : Map<KParameter, Any?> {
12+ // インスタンス有りなら1、そうでなければ0スタート
13+ private var count: Int = initializationStatus
14+
3515 val isInitialized: Boolean get() = initializationStatus == completionValue
3616
37- fun setArgument (kParameter : KParameter , argument : Any? ) {
38- val index = kParameter.index
17+ class MutableEntry internal constructor(
18+ override val key : KParameter ,
19+ override var value : Any?
20+ ) : Map.Entry<KParameter, Any?>
21+
22+ override val size: Int get() = count
23+
24+ override fun containsKey (key : KParameter ): Boolean {
25+ // NOTE: もしかしたらステータスを見た方が速いかも
26+ return keyArray[key.index] != null
27+ }
28+
29+ override fun containsValue (value : Any? ): Boolean {
30+ throw UnsupportedOperationException ()
31+ }
32+
33+ override fun get (key : KParameter ): Any? = valueArray[key.index]
34+ fun getByIndex (key : Int ): Any? =
35+ if (initializationStatus and initializeMask[key] != 0 ) valueArray[key]
36+ else throw IllegalStateException (" This argument is not initialized." )
37+
38+ override fun isEmpty (): Boolean = count == 0
39+
40+ override val entries: Set <Map .Entry <KParameter , Any ?>>
41+ get() = keyArray.mapNotNull { it?.let { MutableEntry (it, valueArray[it.index]) } }.toSet()
42+ override val keys: MutableSet <KParameter >
43+ get() = keyArray.filterNotNull().toMutableSet()
44+ override val values: MutableCollection <Any ?>
45+ get() = throw UnsupportedOperationException ()
46+
47+ fun putIfAbsent (key : KParameter , value : Any? ) {
48+ val index = key.index
3949 val temp = initializationStatus or initializeMask[index]
4050
4151 // 先に入ったものを優先するため、初期化済みなら何もしない
4252 if (initializationStatus == temp) return
4353
44- bucketMap[kParameter] = argument
45- bucket[index] = argument
54+ count + = 1
4655 initializationStatus = temp
56+ keyArray[index] = key
57+ valueArray[index] = value
58+
59+ return
4760 }
4861}
0 commit comments