com.squareup.anvil.compiler.RecordingCache.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of compiler Show documentation
Show all versions of compiler Show documentation
The core implementation module for Anvil, responsible for hooking into the Kotlin compiler and orchestrating code generation
package com.squareup.anvil.compiler
import kotlin.math.roundToInt
internal class RecordingCache(private val name: String) {
val cache: MutableMap = mutableMapOf()
private var hits = 0
private var misses = 0
operator fun contains(key: K): Boolean {
return key in cache
}
operator fun get(key: K): V? {
return cache[key]
}
operator fun set(key: K, value: V) {
cache[key] = value
}
fun getValue(key: K): V {
return cache.getValue(key)
}
fun hit() {
hits++
}
fun miss() {
misses++
}
fun clear() {
cache.clear()
}
fun statsString(): String {
val fidelity = if (hits + misses == 0) {
0
} else {
((hits.toDouble() / (hits + misses)) * 100).roundToInt()
}
return """
$name Cache
Size: ${cache.size}
Hits: $hits
Misses: $misses
Fidelity: $fidelity%
""".trimIndent()
}
operator fun plusAssign(values: Map) {
cache += values
}
}