All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.squareup.anvil.compiler.RecordingCache.kt Maven / Gradle / Ivy

Go to download

The core implementation module for Anvil, responsible for hooking into the Kotlin compiler and orchestrating code generation

There is a newer version: 0.4.0
Show newest version
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
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy