commonMain.com.slack.circuit.runtime.internal.NoOpCollections.kt Maven / Gradle / Ivy
The newest version!
// Copyright (C) 2023 Slack Technologies, LLC
// SPDX-License-Identifier: Apache-2.0
package com.slack.circuit.runtime.internal
/**
* A no-op [MutableMap]. This mimics the behavior of Java's Collections.emptyMap(), which silently
* ignores mutating operations.
*/
internal object NoOpMap : MutableMap {
override val size: Int = 0
@Suppress("UNCHECKED_CAST")
override val entries: MutableSet> =
NoOpSet as MutableSet>
override val keys: MutableSet = NoOpSet
override val values: MutableCollection = NoOpSet
override fun clear() {}
override fun isEmpty() = true
override fun remove(key: Any) = null
override fun putAll(from: Map) {}
override fun put(key: Any, value: Any) = null
override fun get(key: Any) = null
override fun containsValue(value: Any) = false
override fun containsKey(key: Any) = false
}
/**
* A no-op [MutableSet]. This mimics the behavior of Java's Collections.emptySet(), which silently
* ignores mutating operations.
*/
private object NoOpSet : MutableSet {
override fun add(element: Any) = false
override fun addAll(elements: Collection) = false
override val size: Int = 0
override fun clear() {}
override fun isEmpty() = true
override fun containsAll(elements: Collection) = elements.isEmpty()
override fun contains(element: Any) = false
override fun iterator() = NoOpIterator
override fun retainAll(elements: Collection) = false
override fun removeAll(elements: Collection) = false
override fun remove(element: Any) = false
object NoOpIterator : MutableIterator {
override fun hasNext() = false
override fun next(): Any {
throw UnsupportedOperationException()
}
override fun remove() {
throw UnsupportedOperationException()
}
}
}