Bläddra i källkod

Add resource registry util

JunWang 8 år sedan
förälder
incheckning
e30ff59c50

+ 67 - 0
src/MineCase.Core/Registry/RegistryNamespaced.cs

@@ -0,0 +1,67 @@
+using System;
+using System.Collections;
+using System.Collections.Generic;
+using System.Text;
+
+namespace MineCase.Registry
+{
+    public class RegistryNamespaced<TK, TV> : RegistrySimple<TK, TV>
+    {
+        /** The backing store that maps Integers to objects. */
+        protected readonly Dictionary<int, TV> underlyingIntegerMap = new Dictionary<int, TV>();
+
+        protected readonly Dictionary<TV, int> underlyingInversedIntegerMap = new Dictionary<TV, int>();
+
+        /** A BiMap of objects (key) to their names (value). */
+        protected readonly Dictionary<TV, TK> inverseObjectRegistry;
+
+        public RegistryNamespaced()
+        {
+            // this.inverseObjectRegistry = ((BiMap)this.registryObjects).inverse();
+        }
+
+        public void Register(int id, TK key, TV value)
+        {
+            underlyingIntegerMap.Add(id, value);
+            underlyingInversedIntegerMap.Add(value, id);
+            PutObject(key, value);
+        }
+
+        public override TV GetObject(TK name)
+        {
+            return GetObject(name);
+        }
+
+        /**
+         * Gets the name we use to identify the given object.
+         */
+        public TK GetNameForObject(TV value)
+        {
+            return inverseObjectRegistry[value];
+        }
+
+        /**
+         * Does this registry contain an entry for the given key?
+         */
+        public new bool ContainsKey(TK key)
+        {
+            return base.ContainsKey(key);
+        }
+
+        /**
+         * Gets the integer ID we use to identify the given object.
+         */
+        public int GetIDForObject(TV value)
+        {
+            return underlyingInversedIntegerMap[value];
+        }
+
+        /**
+         * Gets the object identified by the given ID.
+         */
+        public TV GetObjectById(int id)
+        {
+            return underlyingIntegerMap[id];
+        }
+    }
+}

+ 82 - 0
src/MineCase.Core/Registry/RegistrySimple.cs

@@ -0,0 +1,82 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace MineCase.Registry
+{
+    public class RegistrySimple<TK, TV>
+    {
+        /** Objects registered on this registry. */
+        protected readonly Dictionary<TK, TV> registryObjects;
+        private TV[] values;
+
+        public RegistrySimple()
+        {
+            registryObjects = new Dictionary<TK, TV>();
+        }
+
+        public virtual TV GetObject( TK name)
+        {
+            return this.registryObjects[name];
+        }
+
+        /**
+         * Register an object on this registry.
+         */
+        public void PutObject(TK key, TV value)
+        {
+            if (key == null)
+            {
+                throw new ArgumentNullException();
+            }
+
+            if (value == null)
+            {
+                throw new ArgumentNullException();
+            }
+
+            this.values = null;
+
+            if (this.registryObjects.ContainsKey(key))
+            {
+                // LOGGER.debug("Adding duplicate key '{}' to registry", key);
+            }
+
+            registryObjects.Add(key, value);
+        }
+
+        /**
+         * Gets all the keys recognized by this registry.
+         */
+        public Dictionary<TK, TV>.KeyCollection GetKeys()
+        {
+            return registryObjects.Keys;
+        }
+
+        public TV GetRandomObject(Random random)
+        {
+            if (values == null)
+            {
+                var collection = registryObjects.Values;
+
+                if (collection.Count == 0)
+                {
+                    return default(TV);
+                }
+
+                values = new TV[collection.Count];
+                collection.CopyTo(values, 0);
+            }
+
+            return values[random.Next(values.Length)];
+        }
+
+        /**
+         * Does this registry contain an entry for the given key?
+         */
+        public bool ContainsKey(TK key)
+        {
+            return registryObjects.ContainsKey(key);
+        }
+    }
+}