using System; namespace UnityAtoms.BaseAtoms { /// /// A SerializableDictionary of type StringReference and AtomBaseVariable. Used by AtomCollection. /// [Serializable] public class StringReferenceAtomBaseVariableDictionary : SerializableDictionary, IAtomCollection { /// /// Generic getter. /// /// The key associated with the value you want to retrieve. /// The expected type of the value you want to retrieve. /// The value of type T if found, otherwise null. public T Get(string key) where T : AtomBaseVariable { var enumerator = GetEnumerator(); T toReturn = null; try { while (enumerator.MoveNext()) { var value = enumerator.Current.Value; if (enumerator.Current.Key == key) { toReturn = (T)enumerator.Current.Value; break; } } } finally { enumerator.Dispose(); } return toReturn; } /// /// Generic getter. /// /// The key associated with the value you want to retrieve. /// The expected type of the value you want to retrieve. /// The value of type T if found, otherwise null. public T Get(AtomBaseVariable key) where T : AtomBaseVariable { if (key == null) throw new ArgumentNullException("key"); return Get(key.Value); } /// /// Add value and its associated key to the dictionary. /// /// The key associated with the value. /// The value to add. public void Add(string key, AtomBaseVariable value) { var strRef = new StringReference(); strRef.Value = key; base.Add(strRef, value); } /// /// Add value and its associated key to the dictionary. /// /// The key associated with the value. /// The value to add. public void Add(AtomBaseVariable key, AtomBaseVariable value) { if (key == null) throw new ArgumentNullException("key"); Add(key.Value, value); } /// /// Remove item by key from the collection. /// /// The key that you want to remove. /// True if it removed a value from the collection, otherwise false. public bool Remove(string key) { var strRef = new StringReference(); strRef.Value = key; return base.Remove(strRef); } /// /// Remove item by key from the collection. /// /// The key that you want to remove. /// True if it removed a value from the collection, otherwise false. public bool Remove(AtomBaseVariable key) { if (key == null) throw new ArgumentNullException("key"); return Remove(key.Value); } } }