Rename observed map to proxied map
This commit is contained in:
parent
3f8a96661f
commit
8f8d4b4ac7
@ -1,6 +1,6 @@
|
|||||||
package ru.dbotthepony.mc.otm.core
|
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 onClear()
|
||||||
protected abstract fun onValueAdded(key: K, value: V)
|
protected abstract fun onValueAdded(key: K, value: V)
|
||||||
protected abstract fun onValueRemoved(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 {
|
final override val entries: MutableSet<MutableMap.MutableEntry<K, V>> by lazy {
|
||||||
object : MutableSet<MutableMap.MutableEntry<K, V>> {
|
object : MutableSet<MutableMap.MutableEntry<K, V>> {
|
||||||
override fun add(element: MutableMap.MutableEntry<K, V>): Boolean {
|
override fun add(element: MutableMap.MutableEntry<K, V>): Boolean {
|
||||||
this@ObservedMap[element.key] = element.value
|
this@ProxiedMap[element.key] = element.value
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -40,10 +40,10 @@ abstract class ObservedMap<K, V>(protected val backingMap: MutableMap<K, V> = Ha
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun clear() {
|
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>> {
|
override fun iterator(): MutableIterator<MutableMap.MutableEntry<K, V>> {
|
||||||
return object : 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 {
|
final override val keys: MutableSet<K> by lazy {
|
||||||
object : MutableSet<K> {
|
object : MutableSet<K> {
|
||||||
val parent = this@ObservedMap.backingMap.keys
|
val parent = this@ProxiedMap.backingMap.keys
|
||||||
|
|
||||||
override fun add(element: K): Boolean {
|
override fun add(element: K): Boolean {
|
||||||
throw UnsupportedOperationException()
|
throw UnsupportedOperationException()
|
||||||
@ -129,7 +129,7 @@ abstract class ObservedMap<K, V>(protected val backingMap: MutableMap<K, V> = Ha
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun clear() {
|
override fun clear() {
|
||||||
this@ObservedMap.clear()
|
this@ProxiedMap.clear()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun iterator(): MutableIterator<K> {
|
override fun iterator(): MutableIterator<K> {
|
||||||
@ -147,7 +147,7 @@ abstract class ObservedMap<K, V>(protected val backingMap: MutableMap<K, V> = Ha
|
|||||||
|
|
||||||
override fun remove() {
|
override fun remove() {
|
||||||
val last = last ?: throw IllegalStateException("Never called next()")
|
val last = last ?: throw IllegalStateException("Never called next()")
|
||||||
val value = this@ObservedMap[last] ?: throw ConcurrentModificationException()
|
val value = this@ProxiedMap[last] ?: throw ConcurrentModificationException()
|
||||||
parentIterator.remove()
|
parentIterator.remove()
|
||||||
onValueRemoved(last, value)
|
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 {
|
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 {
|
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 {
|
final override val values: MutableCollection<V> by lazy {
|
||||||
object : MutableCollection<V> {
|
object : MutableCollection<V> {
|
||||||
private val parent = this@ObservedMap.backingMap.values
|
private val parent = this@ProxiedMap.backingMap.values
|
||||||
|
|
||||||
override val size: Int
|
override val size: Int
|
||||||
get() = parent.size
|
get() = parent.size
|
||||||
@ -227,7 +227,7 @@ abstract class ObservedMap<K, V>(protected val backingMap: MutableMap<K, V> = Ha
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun clear() {
|
override fun clear() {
|
||||||
this@ObservedMap.clear()
|
this@ProxiedMap.clear()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun iterator(): MutableIterator<V> {
|
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 {
|
override fun remove(element: V): Boolean {
|
||||||
val indexOf = this@ObservedMap.backingMap.firstNotNullOfOrNull { if (it.value == element) it.key else null } ?: return false
|
val indexOf = this@ProxiedMap.backingMap.firstNotNullOfOrNull { if (it.value == element) it.key else null } ?: return false
|
||||||
this@ObservedMap.remove(indexOf)
|
this@ProxiedMap.remove(indexOf)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
@ -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() {
|
override fun onClear() {
|
||||||
if (isRemote) {
|
if (isRemote) {
|
||||||
return
|
return
|
||||||
|
@ -8,7 +8,7 @@ import net.minecraft.nbt.ListTag
|
|||||||
import net.minecraft.nbt.Tag
|
import net.minecraft.nbt.Tag
|
||||||
import net.minecraft.world.level.saveddata.SavedData
|
import net.minecraft.world.level.saveddata.SavedData
|
||||||
import org.apache.logging.log4j.LogManager
|
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.core.set
|
||||||
import ru.dbotthepony.mc.otm.registry.WriteOnce
|
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()
|
var parent: SavedCountingMap<T> by WriteOnce()
|
||||||
|
|
||||||
override fun onClear() {
|
override fun onClear() {
|
||||||
@ -58,13 +58,13 @@ class SavedCountingMap<T> private constructor(
|
|||||||
val serializer: (map: SavedCountingMap<T>, value: T, index: Int) -> Tag,
|
val serializer: (map: SavedCountingMap<T>, value: T, index: Int) -> Tag,
|
||||||
val deserializer: (map: SavedCountingMap<T>, nbt: Tag, index: Int) -> T,
|
val deserializer: (map: SavedCountingMap<T>, nbt: Tag, index: Int) -> T,
|
||||||
val factory: (map: SavedCountingMap<T>, 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 {
|
) : SavedData(), MutableMap<Int, T> by map {
|
||||||
constructor(
|
constructor(
|
||||||
serializer: (map: SavedCountingMap<T>, value: T, index: Int) -> Tag,
|
serializer: (map: SavedCountingMap<T>, value: T, index: Int) -> Tag,
|
||||||
deserializer: (map: SavedCountingMap<T>, nbt: Tag, index: Int) -> T,
|
deserializer: (map: SavedCountingMap<T>, nbt: Tag, index: Int) -> T,
|
||||||
factory: (map: SavedCountingMap<T>, index: Int) -> T,
|
factory: (map: SavedCountingMap<T>, index: Int) -> T,
|
||||||
) : this(serializer, deserializer, factory, ObservedCountingMap())
|
) : this(serializer, deserializer, factory, ProxiedCountingMap())
|
||||||
|
|
||||||
init {
|
init {
|
||||||
map.parent = this
|
map.parent = this
|
||||||
|
Loading…
Reference in New Issue
Block a user