KStarbound/src/main/kotlin/ru/dbotthepony/kstarbound/ObjectRegistry.kt
DBotThePony 15abdba2c5
Starbound теперь более не синглтон, а настоящий класс
удалил кучу устаревших классов ибо они совсем не имеют смысла
2023-02-06 17:17:42 +07:00

177 lines
7.4 KiB
Kotlin

package ru.dbotthepony.kstarbound
import it.unimi.dsi.fastutil.ints.Int2ObjectMap
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap
import it.unimi.dsi.fastutil.ints.IntCollection
import it.unimi.dsi.fastutil.ints.IntIterator
import it.unimi.dsi.fastutil.ints.IntSet
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap
import it.unimi.dsi.fastutil.objects.ObjectCollection
import it.unimi.dsi.fastutil.objects.ObjectIterator
import it.unimi.dsi.fastutil.objects.ObjectSet
import org.apache.logging.log4j.LogManager
import java.util.*
class ObjectRegistry<T>(val name: String, val key: (T) -> String, val intKey: ((T) -> Int)? = null) {
private val objects = Object2ObjectOpenHashMap<String, T>()
private val intObjects = Int2ObjectOpenHashMap<T>()
private val origins = Object2ObjectOpenHashMap<String, Any>()
private val intOrigins = Int2ObjectOpenHashMap<Any>()
val view: Map<String, T> = Collections.unmodifiableMap(objects)
val intView = object : Int2ObjectMap<T> {
override fun get(key: Int): T? = intObjects[key]
override fun containsKey(key: Int) = intObjects.containsKey(key)
override fun defaultReturnValue(rv: T) = throw UnsupportedOperationException()
override fun defaultReturnValue(): T = throw UnsupportedOperationException()
override fun containsValue(value: T) = intObjects.containsValue(value)
override fun isEmpty() = intObjects.isEmpty()
override fun putAll(from: Map<out Int, T>) = throw UnsupportedOperationException()
private val int2ObjectEntrySet: ObjectSet<Int2ObjectMap.Entry<T>> = object : ObjectSet<Int2ObjectMap.Entry<T>> {
override val size: Int
get() = intObjects.int2ObjectEntrySet().size
override fun add(element: Int2ObjectMap.Entry<T>) = throw UnsupportedOperationException()
override fun addAll(elements: Collection<Int2ObjectMap.Entry<T>>) = throw UnsupportedOperationException()
override fun clear() = throw UnsupportedOperationException()
override fun iterator(): ObjectIterator<Int2ObjectMap.Entry<T>> {
return object : ObjectIterator<Int2ObjectMap.Entry<T>> {
val iterator = intObjects.int2ObjectEntrySet().iterator()
override fun hasNext() = iterator.hasNext()
override fun remove() = throw UnsupportedOperationException()
override fun next() = iterator.next()
}
}
override fun contains(element: Int2ObjectMap.Entry<T>) = intObjects.int2ObjectEntrySet().contains(element)
override fun containsAll(elements: Collection<Int2ObjectMap.Entry<T>>) = intObjects.int2ObjectEntrySet().containsAll(elements)
override fun isEmpty() = intObjects.int2ObjectEntrySet().isEmpty()
override fun remove(element: Int2ObjectMap.Entry<T>) = throw UnsupportedOperationException()
override fun removeAll(elements: Collection<Int2ObjectMap.Entry<T>>) = throw UnsupportedOperationException()
override fun retainAll(elements: Collection<Int2ObjectMap.Entry<T>>) = throw UnsupportedOperationException()
}
override fun int2ObjectEntrySet(): ObjectSet<Int2ObjectMap.Entry<T>> {
return int2ObjectEntrySet
}
override val keys: IntSet = object : IntSet {
override fun add(key: Int) = throw UnsupportedOperationException()
override fun addAll(c: IntCollection) = throw UnsupportedOperationException()
override fun addAll(elements: Collection<Int>) = throw UnsupportedOperationException()
override fun clear() = throw UnsupportedOperationException()
override fun iterator(): IntIterator {
return object : IntIterator {
val iterator = intObjects.keys.iterator()
override fun hasNext() = iterator.hasNext()
override fun remove() = throw UnsupportedOperationException()
override fun nextInt() = iterator.nextInt()
}
}
override fun remove(k: Int) = throw UnsupportedOperationException()
override fun removeAll(c: IntCollection) = throw UnsupportedOperationException()
override fun removeAll(elements: Collection<Int>) = throw UnsupportedOperationException()
override fun retainAll(c: IntCollection) = throw UnsupportedOperationException()
override fun retainAll(elements: Collection<Int>) = throw UnsupportedOperationException()
override fun contains(key: Int) = intObjects.keys.contains(key)
override fun containsAll(c: IntCollection) = intObjects.keys.containsAll(c)
override fun containsAll(elements: Collection<Int>) = intObjects.keys.containsAll(elements)
override fun isEmpty() = intObjects.keys.isEmpty()
override fun toArray(a: IntArray) = intObjects.keys.toArray(a)
override fun toIntArray() = intObjects.keys.toIntArray()
override val size: Int
get() = intObjects.keys.size
}
override val values: ObjectCollection<T> = object : ObjectCollection<T> {
override val size: Int
get() = intObjects.values.size
override fun add(element: T) = throw UnsupportedOperationException()
override fun addAll(elements: Collection<T>) = throw UnsupportedOperationException()
override fun clear() = throw UnsupportedOperationException()
override fun iterator(): ObjectIterator<T> {
return object : ObjectIterator<T> {
val iterator = intObjects.values.iterator()
override fun hasNext() = iterator.hasNext()
override fun next(): T = iterator.next()
override fun remove() = throw UnsupportedOperationException()
}
}
override fun contains(element: T) = intObjects.values.contains(element)
override fun containsAll(elements: Collection<T>) = intObjects.values.containsAll(elements)
override fun isEmpty() = intObjects.values.isEmpty()
override fun remove(element: T) = throw UnsupportedOperationException()
override fun removeAll(elements: Collection<T>) = throw UnsupportedOperationException()
override fun retainAll(elements: Collection<T>) = throw UnsupportedOperationException()
}
override val size: Int
get() = intObjects.size
}
fun clearOrigins() {
origins.clear()
intOrigins.clear()
}
fun clear() {
clearOrigins()
objects.clear()
intObjects.clear()
}
fun add(value: T, origin: Any? = null): Boolean {
val key = this.key.invoke(value)
val existing = objects.put(key, value)
if (existing != null) {
val oldOrigin = origins[key]
if (origin == null && oldOrigin == null)
LOGGER.warn("Registry $name already has object with key $key! Overwriting.")
else if (origin == null)
LOGGER.warn("Registry $name already has object with key $key! Overwriting. (old originated from $oldOrigin)")
else
LOGGER.warn("Registry $name already has object with key $key! Overwriting. (old originated from $oldOrigin, new originate from $origin).")
}
if (origin == null)
origins.remove(key)
else
origins[key] = origin
if (this.intKey == null)
return existing != null
val intKey = this.intKey.invoke(value)
val intExisting = intObjects.put(intKey, value)
if (intExisting != null) {
val oldOrigin = intOrigins[intKey]
if (origin == null && oldOrigin == null)
LOGGER.warn("Registry $name already has object with ID $intKey (new $key, old ${this.key.invoke(intExisting)})! Overwriting.")
else if (origin == null)
LOGGER.warn("Registry $name already has object with ID $intKey (new $key, old ${this.key.invoke(intExisting)})! Overwriting. (old originated from $oldOrigin)")
else
LOGGER.warn("Registry $name already has object with ID $intKey (new $key, old ${this.key.invoke(intExisting)})! Overwriting. (old originated from $oldOrigin, new originate from $origin).")
}
return existing != null || intExisting != null
}
companion object {
private val LOGGER = LogManager.getLogger()
}
}