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

com.squareup.anvil.compiler.codegen.incremental.collections.Multimap.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.codegen.incremental.collections

import java.io.Serializable

internal interface Multimap : Serializable {
  val keys: Set

  operator fun get(key: K): Set

  operator fun contains(key: K): Boolean
  fun add(key: K, value: V): Boolean
  fun remove(key: K)
  fun remove(key: K, value: V)

  companion object {
    operator fun  invoke(): Multimap = MultimapImpl()
  }
}

internal open class MultimapImpl : Multimap {
  private val map: MutableMap> = HashMap()

  override val keys: Set get() = map.keys

  override operator fun get(key: K): Set = map[key].orEmpty()
  override operator fun contains(key: K): Boolean = map.containsKey(key)

  override fun add(key: K, value: V): Boolean = map.getOrPut(key) { mutableSetOf() }.add(value)

  override fun remove(key: K) {
    map.remove(key)?.toSet()
  }

  override fun remove(key: K, value: V) {
    val values = map[key] ?: return

    values.remove(value)
    if (values.isEmpty()) {
      map.remove(key)
    }
  }

  override fun toString(): String = map.entries.joinToString("\n") { "${it.key} : ${it.value}" }
  override fun equals(other: Any?): Boolean {
    if (this === other) return true
    if (other !is MultimapImpl<*, *>) return false
    if (map != other.map) return false
    return true
  }

  override fun hashCode(): Int = map.hashCode()
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy