@@ -6,74 +6,58 @@ import kotlin.reflect.KParameter
6
6
class ArgumentBucket (
7
7
private val keyList : List <KParameter >,
8
8
private val valueArray : Array <Any ?>,
9
- private val initializationStatuses : BooleanArray ,
9
+ private var count : Int ,
10
10
private val valueArrayGetter : (Array <Any ?>) -> Array <Any ?>
11
11
) : Map<KParameter, Any?> {
12
- fun isFullInitialized (): Boolean = initializationStatuses.all { it }
12
+ fun isFullInitialized (): Boolean = count == keyList.size
13
13
14
14
// getValueArrayは内部処理でしか利用しないためinternal化
15
15
internal fun getValueArray (): Array <Any ?> = valueArrayGetter(valueArray)
16
16
17
- operator fun set (key : KParameter , value : Any? ): Any? {
18
- return valueArray[key.index].apply {
19
- valueArray[key.index] = value
20
- initializationStatuses[key.index] = true
21
- }
22
- }
17
+ operator fun set (key : KParameter , value : Any? ): Any? = set(key.index, value)
23
18
24
- operator fun set (index : Int , value : Any? ): Any? {
25
- return valueArray[index].apply {
26
- valueArray[index] = value
27
- initializationStatuses[index] = true
28
- }
19
+ operator fun set (index : Int , value : Any? ): Any? = valueArray[index].apply {
20
+ if (this == = ABSENT_VALUE ) count++
21
+ valueArray[index] = value
29
22
}
30
23
31
24
/* *
32
25
* If the specified key is not already associated with a value associates it with the given value and returns
33
26
* {@code null}, else returns the current value.
34
27
*/
35
- fun setIfAbsent (key : KParameter , value : Any? ): Any? {
36
- return if (initializationStatuses[key.index])
37
- valueArray[key.index]
38
- else
39
- null .apply {
40
- valueArray[key.index] = value
41
- initializationStatuses[key.index] = true
42
- }
43
- }
28
+ fun setIfAbsent (key : KParameter , value : Any? ): Any? = setIfAbsent(key.index, value)
44
29
45
30
/* *
46
31
* If the specified key is not already associated with a value associates it with the given value and returns
47
32
* {@code null}, else returns the current value.
48
33
*/
49
- fun setIfAbsent (index : Int , value : Any? ): Any? {
50
- return if (initializationStatuses[index])
51
- valueArray[index]
52
- else
53
- null .apply {
54
- valueArray[index] = value
55
- initializationStatuses[index] = true
56
- }
34
+ fun setIfAbsent (index : Int , value : Any? ): Any? = valueArray[index].apply {
35
+ if (this == = ABSENT_VALUE ) {
36
+ count++
37
+ valueArray[index] = value
38
+ }
57
39
}
58
40
41
+ private fun isInitialized (index : Int ): Boolean = valueArray[index] != = ABSENT_VALUE
42
+
59
43
override val entries: Set <Map .Entry <KParameter , Any ?>>
60
44
get() = keyList.fold(HashSet ()) { acc, cur ->
61
- acc.apply { if (initializationStatuses[ cur.index] ) add(Entry (cur, valueArray[cur.index])) }
45
+ acc.apply { if (isInitialized( cur.index) ) add(Entry (cur, valueArray[cur.index])) }
62
46
}
63
47
override val keys: Set <KParameter >
64
48
get() = keyList.fold(HashSet ()) { acc, cur ->
65
- acc.apply { if (initializationStatuses[ cur.index] ) add(cur) }
49
+ acc.apply { if (isInitialized( cur.index) ) add(cur) }
66
50
}
67
- override val size: Int get() = initializationStatuses. count { it }
51
+ override val size: Int get() = count
68
52
override val values: Collection <Any ?>
69
- get() = valueArray.filterIndexed { idx, _ -> initializationStatuses[idx] }
53
+ get() = valueArray.filter { it != = ABSENT_VALUE }
70
54
71
55
// keyはインスタンスの一致と初期化状態を見る
72
56
override fun containsKey (key : KParameter ): Boolean =
73
- keyList[key.index] == = key && initializationStatuses[ key.index]
57
+ keyList[key.index] == = key && isInitialized( key.index)
74
58
75
59
override fun containsValue (value : Any? ): Boolean = valueArray.withIndex()
76
- .any { initializationStatuses[ it.index] && it.value == value }
60
+ .any { isInitialized( it.index) && it.value == value }
77
61
78
62
override fun get (key : KParameter ): Any? = valueArray[key.index]
79
63
operator fun get (index : Int ): Any? = valueArray[index]
0 commit comments