[FIR] Rename KEY and VALUE generics of FirCache to K and V
This commit is contained in:
@@ -5,11 +5,11 @@
|
||||
|
||||
package org.jetbrains.kotlin.fir.caches
|
||||
|
||||
abstract class FirCache<in KEY : Any, out VALUE, in CONTEXT> {
|
||||
abstract fun getValue(key: KEY, context: CONTEXT): VALUE
|
||||
abstract fun getValueIfComputed(key: KEY): VALUE?
|
||||
abstract class FirCache<in K : Any, out V, in CONTEXT> {
|
||||
abstract fun getValue(key: K, context: CONTEXT): V
|
||||
abstract fun getValueIfComputed(key: K): V?
|
||||
}
|
||||
|
||||
@Suppress("NOTHING_TO_INLINE")
|
||||
inline fun <KEY : Any, VALUE> FirCache<KEY, VALUE, Nothing?>.getValue(key: KEY): VALUE =
|
||||
inline fun <K : Any, V> FirCache<K, V, Nothing?>.getValue(key: K): V =
|
||||
getValue(key, null)
|
||||
|
||||
@@ -18,12 +18,12 @@ abstract class FirCachesFactory : FirSessionComponent {
|
||||
* Where:
|
||||
* [CONTEXT] -- type of value which be used to create value by [createValue]
|
||||
*/
|
||||
abstract fun <KEY : Any, VALUE, CONTEXT> createCache(createValue: (KEY, CONTEXT) -> VALUE): FirCache<KEY, VALUE, CONTEXT>
|
||||
abstract fun <K : Any, V, CONTEXT> createCache(createValue: (K, CONTEXT) -> V): FirCache<K, V, CONTEXT>
|
||||
|
||||
/**
|
||||
* Creates a cache with returns a caches value on demand if it is computed
|
||||
* Otherwise computes the value in two phases:
|
||||
* - [createValue] -- creates values and stores [VALUE] to cache and passes [VALUE] & [DATA] to [postCompute]
|
||||
* - [createValue] -- creates values and stores value of type [V] to cache and passes [V] & [DATA] to [postCompute]
|
||||
* - [postCompute] -- performs some operations on computed value after it placed into map
|
||||
*
|
||||
* [FirCache.getValue] can be safely called in postCompute from the same thread and correct value computed by [createValue] will be returned
|
||||
@@ -33,40 +33,40 @@ abstract class FirCachesFactory : FirSessionComponent {
|
||||
* [CONTEXT] -- type of value which be used to create value by [createValue]
|
||||
* [DATA] -- type of additional data which will be passed from [createValue] to [postCompute]
|
||||
*/
|
||||
abstract fun <KEY : Any, VALUE, CONTEXT, DATA> createCacheWithPostCompute(
|
||||
createValue: (KEY, CONTEXT) -> Pair<VALUE, DATA>,
|
||||
postCompute: (KEY, VALUE, DATA) -> Unit
|
||||
): FirCache<KEY, VALUE, CONTEXT>
|
||||
abstract fun <K : Any, V, CONTEXT, DATA> createCacheWithPostCompute(
|
||||
createValue: (K, CONTEXT) -> Pair<V, DATA>,
|
||||
postCompute: (K, V, DATA) -> Unit
|
||||
): FirCache<K, V, CONTEXT>
|
||||
}
|
||||
|
||||
val FirSession.firCachesFactory: FirCachesFactory by FirSession.sessionComponentAccessor()
|
||||
|
||||
inline fun <KEY : Any, VALUE> FirCachesFactory.createCache(
|
||||
crossinline createValue: (KEY) -> VALUE,
|
||||
): FirCache<KEY, VALUE, Nothing?> = createCache(
|
||||
inline fun <K : Any, V> FirCachesFactory.createCache(
|
||||
crossinline createValue: (K) -> V,
|
||||
): FirCache<K, V, Nothing?> = createCache(
|
||||
createValue = { key, _ -> createValue(key) },
|
||||
)
|
||||
|
||||
inline fun <KEY : Any, VALUE, CONTEXT> FirCachesFactory.createCacheWithPostCompute(
|
||||
crossinline createValue: (KEY, CONTEXT) -> VALUE,
|
||||
crossinline postCompute: (KEY, VALUE) -> Unit
|
||||
): FirCache<KEY, VALUE, CONTEXT> = createCacheWithPostCompute(
|
||||
inline fun <K : Any, V, CONTEXT> FirCachesFactory.createCacheWithPostCompute(
|
||||
crossinline createValue: (K, CONTEXT) -> V,
|
||||
crossinline postCompute: (K, V) -> Unit
|
||||
): FirCache<K, V, CONTEXT> = createCacheWithPostCompute(
|
||||
createValue = { key, context -> createValue(key, context) to null },
|
||||
postCompute = { key, value, _ -> postCompute(key, value) }
|
||||
)
|
||||
|
||||
inline fun <KEY : Any, VALUE> FirCachesFactory.createCacheWithPostCompute(
|
||||
crossinline createValue: (KEY) -> VALUE,
|
||||
crossinline postCompute: (KEY, VALUE) -> Unit
|
||||
): FirCache<KEY, VALUE, Nothing?> = createCacheWithPostCompute(
|
||||
inline fun <K : Any, V> FirCachesFactory.createCacheWithPostCompute(
|
||||
crossinline createValue: (K) -> V,
|
||||
crossinline postCompute: (K, V) -> Unit
|
||||
): FirCache<K, V, Nothing?> = createCacheWithPostCompute(
|
||||
createValue = { key, _ -> createValue(key) to null },
|
||||
postCompute = { key, value, _ -> postCompute(key, value) }
|
||||
)
|
||||
|
||||
inline fun <KEY : Any, VALUE, DATA> FirCachesFactory.createCacheWithPostCompute(
|
||||
crossinline createValue: (KEY) -> Pair<VALUE, DATA>,
|
||||
crossinline postCompute: (KEY, VALUE, DATA) -> Unit
|
||||
): FirCache<KEY, VALUE, Nothing?> = createCacheWithPostCompute(
|
||||
inline fun <K : Any, V, DATA> FirCachesFactory.createCacheWithPostCompute(
|
||||
crossinline createValue: (K) -> Pair<V, DATA>,
|
||||
crossinline postCompute: (K, V, DATA) -> Unit
|
||||
): FirCache<K, V, Nothing?> = createCacheWithPostCompute(
|
||||
createValue = { key, _ -> createValue(key) },
|
||||
postCompute = { key, value, data -> postCompute(key, value, data) }
|
||||
)
|
||||
|
||||
+21
-21
@@ -6,41 +6,41 @@
|
||||
package org.jetbrains.kotlin.fir.caches
|
||||
|
||||
object FirThreadUnsafeCachesFactory : FirCachesFactory() {
|
||||
override fun <KEY : Any, VALUE, CONTEXT> createCache(createValue: (KEY, CONTEXT) -> VALUE): FirCache<KEY, VALUE, CONTEXT> =
|
||||
override fun <K : Any, V, CONTEXT> createCache(createValue: (K, CONTEXT) -> V): FirCache<K, V, CONTEXT> =
|
||||
FirThreadUnsafeCache(createValue)
|
||||
|
||||
override fun <KEY : Any, VALUE, CONTEXT, DATA> createCacheWithPostCompute(
|
||||
createValue: (KEY, CONTEXT) -> Pair<VALUE, DATA>,
|
||||
postCompute: (KEY, VALUE, DATA) -> Unit
|
||||
): FirCache<KEY, VALUE, CONTEXT> =
|
||||
override fun <K : Any, V, CONTEXT, DATA> createCacheWithPostCompute(
|
||||
createValue: (K, CONTEXT) -> Pair<V, DATA>,
|
||||
postCompute: (K, V, DATA) -> Unit
|
||||
): FirCache<K, V, CONTEXT> =
|
||||
FirThreadUnsafeCacheWithPostCompute(createValue, postCompute)
|
||||
}
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
private class FirThreadUnsafeCache<KEY : Any, VALUE, CONTEXT>(
|
||||
private val createValue: (KEY, CONTEXT) -> VALUE
|
||||
) : FirCache<KEY, VALUE, CONTEXT>() {
|
||||
private val map = NullableMap<KEY, VALUE>()
|
||||
private class FirThreadUnsafeCache<K : Any, V, CONTEXT>(
|
||||
private val createValue: (K, CONTEXT) -> V
|
||||
) : FirCache<K, V, CONTEXT>() {
|
||||
private val map = NullableMap<K, V>()
|
||||
|
||||
override fun getValue(key: KEY, context: CONTEXT): VALUE =
|
||||
override fun getValue(key: K, context: CONTEXT): V =
|
||||
map.getOrElse(key) {
|
||||
createValue(key, context).also { createdValue ->
|
||||
map[key] = createdValue
|
||||
}
|
||||
}
|
||||
|
||||
override fun getValueIfComputed(key: KEY): VALUE? =
|
||||
map.getOrElse(key) { null as VALUE }
|
||||
override fun getValueIfComputed(key: K): V? =
|
||||
map.getOrElse(key) { null as V }
|
||||
}
|
||||
|
||||
|
||||
private class FirThreadUnsafeCacheWithPostCompute<KEY : Any, VALUE, CONTEXT, DATA>(
|
||||
private val createValue: (KEY, CONTEXT) -> Pair<VALUE, DATA>,
|
||||
private val postCompute: (KEY, VALUE, DATA) -> Unit
|
||||
) : FirCache<KEY, VALUE, CONTEXT>() {
|
||||
private val map = NullableMap<KEY, VALUE>()
|
||||
private class FirThreadUnsafeCacheWithPostCompute<K : Any, V, CONTEXT, DATA>(
|
||||
private val createValue: (K, CONTEXT) -> Pair<V, DATA>,
|
||||
private val postCompute: (K, V, DATA) -> Unit
|
||||
) : FirCache<K, V, CONTEXT>() {
|
||||
private val map = NullableMap<K, V>()
|
||||
|
||||
override fun getValue(key: KEY, context: CONTEXT): VALUE =
|
||||
override fun getValue(key: K, context: CONTEXT): V =
|
||||
map.getOrElse(key) {
|
||||
val (createdValue, data) = createValue(key, context)
|
||||
map[key] = createdValue
|
||||
@@ -50,6 +50,6 @@ private class FirThreadUnsafeCacheWithPostCompute<KEY : Any, VALUE, CONTEXT, DAT
|
||||
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
override fun getValueIfComputed(key: KEY): VALUE? =
|
||||
map.getOrElse(key) { null as VALUE }
|
||||
}
|
||||
override fun getValueIfComputed(key: K): V? =
|
||||
map.getOrElse(key) { null as V }
|
||||
}
|
||||
|
||||
+7
-7
@@ -8,14 +8,14 @@ package org.jetbrains.kotlin.idea.fir.low.level.api.fir.caches
|
||||
import org.jetbrains.kotlin.fir.caches.FirCache
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
|
||||
internal class FirThreadSafeCache<KEY : Any, VALUE, CONTEXT>(
|
||||
private val createValue: (KEY, CONTEXT) -> VALUE
|
||||
) : FirCache<KEY, VALUE, CONTEXT>() {
|
||||
private val map = ConcurrentHashMap<KEY, Any>()
|
||||
internal class FirThreadSafeCache<K : Any, V, CONTEXT>(
|
||||
private val createValue: (K, CONTEXT) -> V
|
||||
) : FirCache<K, V, CONTEXT>() {
|
||||
private val map = ConcurrentHashMap<K, Any>()
|
||||
|
||||
override fun getValue(key: KEY, context: CONTEXT): VALUE =
|
||||
override fun getValue(key: K, context: CONTEXT): V =
|
||||
map.computeIfAbsentWithNullableValue(key) { createValue(it, context) }
|
||||
|
||||
override fun getValueIfComputed(key: KEY): VALUE? =
|
||||
override fun getValueIfComputed(key: K): V? =
|
||||
map[key]?.nullValueToNull()
|
||||
}
|
||||
}
|
||||
|
||||
+8
-8
@@ -8,14 +8,14 @@ package org.jetbrains.kotlin.idea.fir.low.level.api.fir.caches
|
||||
import org.jetbrains.kotlin.fir.caches.FirCache
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
|
||||
internal class FirThreadSafeCacheWithPostCompute<KEY : Any, VALUE, CONTEXT, DATA>(
|
||||
private val createValue: (KEY, CONTEXT) -> Pair<VALUE, DATA>,
|
||||
private val postCompute: (KEY, VALUE, DATA) -> Unit
|
||||
) : FirCache<KEY, VALUE, CONTEXT>() {
|
||||
private val map = ConcurrentHashMap<KEY, ValueWithPostCompute<KEY, VALUE, DATA>>()
|
||||
internal class FirThreadSafeCacheWithPostCompute<K : Any, V, CONTEXT, DATA>(
|
||||
private val createValue: (K, CONTEXT) -> Pair<V, DATA>,
|
||||
private val postCompute: (K, V, DATA) -> Unit
|
||||
) : FirCache<K, V, CONTEXT>() {
|
||||
private val map = ConcurrentHashMap<K, ValueWithPostCompute<K, V, DATA>>()
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
override fun getValue(key: KEY, context: CONTEXT): VALUE =
|
||||
override fun getValue(key: K, context: CONTEXT): V =
|
||||
map.computeIfAbsent(key) {
|
||||
ValueWithPostCompute(
|
||||
key,
|
||||
@@ -24,6 +24,6 @@ internal class FirThreadSafeCacheWithPostCompute<KEY : Any, VALUE, CONTEXT, DATA
|
||||
)
|
||||
}.getValue()
|
||||
|
||||
override fun getValueIfComputed(key: KEY): VALUE? =
|
||||
override fun getValueIfComputed(key: K): V? =
|
||||
map[key]?.getValueIfComputed()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user