Rename observed map to proxied map

This commit is contained in:
DBotThePony 2022-09-25 21:07:30 +07:00
parent 3f8a96661f
commit 8f8d4b4ac7
Signed by: DBot
GPG Key ID: DCC23B5715498507
3 changed files with 17 additions and 17 deletions

View File

@ -1,6 +1,6 @@
package ru.dbotthepony.mc.otm.core
abstract class ObservedMap<K, V>(protected val backingMap: MutableMap<K, V> = HashMap()) : MutableMap<K, V> {
abstract class ProxiedMap<K, V>(protected val backingMap: MutableMap<K, V> = HashMap()) : MutableMap<K, V> {
protected abstract fun onClear()
protected abstract fun onValueAdded(key: K, value: V)
protected abstract fun onValueRemoved(key: K, value: V)
@ -27,7 +27,7 @@ abstract class ObservedMap<K, V>(protected val backingMap: MutableMap<K, V> = Ha
final override val entries: MutableSet<MutableMap.MutableEntry<K, V>> by lazy {
object : MutableSet<MutableMap.MutableEntry<K, V>> {
override fun add(element: MutableMap.MutableEntry<K, V>): Boolean {
this@ObservedMap[element.key] = element.value
this@ProxiedMap[element.key] = element.value
return true
}
@ -40,10 +40,10 @@ abstract class ObservedMap<K, V>(protected val backingMap: MutableMap<K, V> = Ha
}
override fun clear() {
this@ObservedMap.clear()
this@ProxiedMap.clear()
}
private val setParent = this@ObservedMap.backingMap.entries
private val setParent = this@ProxiedMap.backingMap.entries
override fun iterator(): MutableIterator<MutableMap.MutableEntry<K, V>> {
return object : MutableIterator<MutableMap.MutableEntry<K, V>> {
@ -118,7 +118,7 @@ abstract class ObservedMap<K, V>(protected val backingMap: MutableMap<K, V> = Ha
final override val keys: MutableSet<K> by lazy {
object : MutableSet<K> {
val parent = this@ObservedMap.backingMap.keys
val parent = this@ProxiedMap.backingMap.keys
override fun add(element: K): Boolean {
throw UnsupportedOperationException()
@ -129,7 +129,7 @@ abstract class ObservedMap<K, V>(protected val backingMap: MutableMap<K, V> = Ha
}
override fun clear() {
this@ObservedMap.clear()
this@ProxiedMap.clear()
}
override fun iterator(): MutableIterator<K> {
@ -147,7 +147,7 @@ abstract class ObservedMap<K, V>(protected val backingMap: MutableMap<K, V> = Ha
override fun remove() {
val last = last ?: throw IllegalStateException("Never called next()")
val value = this@ObservedMap[last] ?: throw ConcurrentModificationException()
val value = this@ProxiedMap[last] ?: throw ConcurrentModificationException()
parentIterator.remove()
onValueRemoved(last, value)
}
@ -155,7 +155,7 @@ abstract class ObservedMap<K, V>(protected val backingMap: MutableMap<K, V> = Ha
}
override fun remove(element: K): Boolean {
return this@ObservedMap.remove(element) != null
return this@ProxiedMap.remove(element) != null
}
override fun removeAll(elements: Collection<K>): Boolean {
@ -201,7 +201,7 @@ abstract class ObservedMap<K, V>(protected val backingMap: MutableMap<K, V> = Ha
final override val values: MutableCollection<V> by lazy {
object : MutableCollection<V> {
private val parent = this@ObservedMap.backingMap.values
private val parent = this@ProxiedMap.backingMap.values
override val size: Int
get() = parent.size
@ -227,7 +227,7 @@ abstract class ObservedMap<K, V>(protected val backingMap: MutableMap<K, V> = Ha
}
override fun clear() {
this@ObservedMap.clear()
this@ProxiedMap.clear()
}
override fun iterator(): MutableIterator<V> {
@ -249,8 +249,8 @@ abstract class ObservedMap<K, V>(protected val backingMap: MutableMap<K, V> = Ha
}
override fun remove(element: V): Boolean {
val indexOf = this@ObservedMap.backingMap.firstNotNullOfOrNull { if (it.value == element) it.key else null } ?: return false
this@ObservedMap.remove(indexOf)
val indexOf = this@ProxiedMap.backingMap.firstNotNullOfOrNull { if (it.value == element) it.key else null } ?: return false
this@ProxiedMap.remove(indexOf)
return true
}

View File

@ -452,7 +452,7 @@ class FieldSynchronizer {
}
}
override val value: MutableMap<K, V> = object : ObservedMap<K, V>(backingMap) {
override val value: MutableMap<K, V> = object : ProxiedMap<K, V>(backingMap) {
override fun onClear() {
if (isRemote) {
return

View File

@ -8,7 +8,7 @@ import net.minecraft.nbt.ListTag
import net.minecraft.nbt.Tag
import net.minecraft.world.level.saveddata.SavedData
import org.apache.logging.log4j.LogManager
import ru.dbotthepony.mc.otm.core.ObservedMap
import ru.dbotthepony.mc.otm.core.ProxiedMap
import ru.dbotthepony.mc.otm.core.set
import ru.dbotthepony.mc.otm.registry.WriteOnce
@ -38,7 +38,7 @@ class SavedMapDelegate<V>(val parent: SavedCountingMap<SavedMapDelegate<V>>?, va
}
}
private class ObservedCountingMap<T> : ObservedMap<Int, T>(Int2ObjectAVLTreeMap()) {
private class ProxiedCountingMap<T> : ProxiedMap<Int, T>(Int2ObjectAVLTreeMap()) {
var parent: SavedCountingMap<T> by WriteOnce()
override fun onClear() {
@ -58,13 +58,13 @@ class SavedCountingMap<T> private constructor(
val serializer: (map: SavedCountingMap<T>, value: T, index: Int) -> Tag,
val deserializer: (map: SavedCountingMap<T>, nbt: Tag, index: Int) -> T,
val factory: (map: SavedCountingMap<T>, index: Int) -> T,
private val map: ObservedCountingMap<T>
private val map: ProxiedCountingMap<T>
) : SavedData(), MutableMap<Int, T> by map {
constructor(
serializer: (map: SavedCountingMap<T>, value: T, index: Int) -> Tag,
deserializer: (map: SavedCountingMap<T>, nbt: Tag, index: Int) -> T,
factory: (map: SavedCountingMap<T>, index: Int) -> T,
) : this(serializer, deserializer, factory, ObservedCountingMap())
) : this(serializer, deserializer, factory, ProxiedCountingMap())
init {
map.parent = this