using System; using System.Collections.Generic; using SUISS.Core.Utilities; namespace CIG { public class StorageDictionary { public StorageDictionary() { this._storage = new Dictionary(); } public StorageDictionary(Dictionary storage) { this._storage = storage; } public bool Contains(string key) { return this._storage.ContainsKey(key); } public void Remove(string key) { this._storage.Remove(key); } public void Set(string key, string value) { this._storage[key] = value; } public void Set(string key, bool value) { this._storage[key] = value; } public void Set(string key, char value) { this._storage[key] = value; } public void Set(string key, float value) { this._storage[key] = value; } public void Set(string key, double value) { this._storage[key] = value; } public void Set(string key, sbyte value) { this._storage[key] = value; } public void Set(string key, byte value) { this._storage[key] = value; } public void Set(string key, short value) { this._storage[key] = value; } public void Set(string key, ushort value) { this._storage[key] = value; } public void Set(string key, int value) { this._storage[key] = value; } public void Set(string key, uint value) { this._storage[key] = value; } public void Set(string key, long value) { this._storage[key] = value; } public void Set(string key, ulong value) { this._storage[key] = value; } public void Set(string key, decimal value) { this._storage[key] = value; } public void Set(string key, DateTime value) { this._storage[key] = value.ToBinary(); } public void Set(string key, T value) where T : ICanSerialize { this._storage[key] = value.Serialize()._storage; } public void SetOrRemove(string key, double? value) { if (value == null) { this._storage.Remove(key); } else { this.Set(key, value.Value); } } public void Set(string key, List values) { this.SetList(key, values); } public void Set(string key, List values) { this.SetList(key, values); } public void Set(string key, List values) { this.SetList(key, values); } public void Set(string key, List values) { this.SetList(key, values); } public void Set(string key, List values) { this.SetList(key, values); } public void Set(string key, List values) { this.SetList(key, values); } public void Set(string key, List values) { this.SetList(key, values); } public void Set(string key, List values) { this.SetList(key, values); } public void Set(string key, List values) { this.SetList(key, values); } public void Set(string key, List values) { this.SetList(key, values); } public void Set(string key, List values) { this.SetList(key, values); } public void Set(string key, List values) { this.SetList(key, values); } public void Set(string key, List values) { this.SetList(key, values); } public void Set(string key, List values) { this.SetList(key, values); } public void Set(string key, List values) { List list = new List(); int count = values.Count; for (int i = 0; i < count; i++) { list.Add(values[i].ToBinary()); } this._storage[key] = list; } public void Set(string key, Dictionary values) { this.SetDictionary(key, values); } public void Set(string key, Dictionary values) { this.SetDictionary(key, values); } public void Set(string key, Dictionary values) { this.SetDictionary(key, values); } public void Set(string key, List values, Func serialize) { List list = new List(); int count = values.Count; for (int i = 0; i < count; i++) { list.Add(serialize(values[i])._storage); } this._storage[key] = list; } public void Set(string key, Dictionary values) where T : struct, IConvertible { if (!typeof(T).IsEnum()) { throw new ArgumentException("T must be an Enum."); } Dictionary dictionary = new Dictionary(); foreach (KeyValuePair keyValuePair in values) { dictionary.Add(Convert.ToInt32(keyValuePair.Key).ToString(), keyValuePair.Value); } this._storage[key] = dictionary; } private void SetList(string key, List values) { List list = new List(); int count = values.Count; for (int i = 0; i < count; i++) { list.Add(values[i]); } this._storage[key] = list; } private void SetDictionary(string key, Dictionary values) { Dictionary dictionary = new Dictionary(); foreach (KeyValuePair keyValuePair in values) { dictionary.Add(keyValuePair.Key, keyValuePair.Value); } this._storage[key] = dictionary; } public TField Get(string key, TField defaultValue) { if (!this._storage.ContainsKey(key)) { return defaultValue; } if (!(this._storage[key] is TField)) { throw new InvalidOperationException(string.Format("Found {0} at _storage[{1}]. Expecting {2}", (this._storage[key] != null) ? this._storage[key].GetType().Name : "null", key, typeof(TField).Name)); } return (TField)((object)this._storage[key]); } public DateTime GetDateTime(string key, DateTime defaultValue) { if (!this._storage.ContainsKey(key)) { return defaultValue; } if (!(this._storage[key] is long)) { throw new InvalidOperationException(string.Format("Found {0} at _storage[{1}]. Expecting {2}", this._storage[key].GetType().Name, key, typeof(long).Name)); } return DateTime.FromBinary((long)this._storage[key]); } public List GetDateTimeList(string key) { if (!this._storage.ContainsKey(key)) { return new List(); } if (!(this._storage[key] is List)) { throw new InvalidOperationException(string.Format("Found {0} at _storage[{1}]. Expecting {2}", this._storage[key].GetType().Name, key, typeof(List).Name)); } List list = new List(); List list2 = (List)this._storage[key]; int count = list2.Count; for (int i = 0; i < count; i++) { if (!(list2[i] is long)) { throw new InvalidOperationException(string.Format("Found {0} at _storage[{1}][{2}]. Expecting {3}", new object[] { (list2[i] != null) ? list2[i].GetType().Name : "null", key, i, typeof(long).Name })); } list.Add(DateTime.FromBinary((long)list2[i])); } return list; } public StorageDictionary GetStorageDict(string key) { if (!this._storage.ContainsKey(key)) { return new StorageDictionary(); } if (!(this._storage[key] is Dictionary)) { throw new InvalidOperationException(string.Format("Found {0} at _storage[{1}]. Expecting {2}", this._storage[key].GetType().Name, key, typeof(List).Name)); } return new StorageDictionary((Dictionary)this._storage[key]); } public List GetList(string key) { if (!this._storage.ContainsKey(key)) { return new List(); } if (!(this._storage[key] is List)) { throw new InvalidOperationException(string.Format("Found {0} at _storage[{1}]. Expecting {2}", this._storage[key].GetType().Name, key, typeof(List).Name)); } List list = new List(); List list2 = (List)this._storage[key]; int count = list2.Count; for (int i = 0; i < count; i++) { if (!(list2[i] is TField)) { throw new InvalidOperationException(string.Format("Found {0} at _storage[{1}][{2}]. Expecting {3}", new object[] { (list2[i] != null) ? list2[i].GetType().Name : "null", key, i, typeof(TField).Name })); } list.Add((TField)((object)list2[i])); } return list; } public Dictionary GetDictionary(string key) { if (!this._storage.ContainsKey(key)) { return new Dictionary(); } if (!(this._storage[key] is Dictionary)) { throw new InvalidOperationException(string.Format("Found {0} at _storage[{1}]. Expecting {2}", this._storage[key].GetType().Name, key, typeof(Dictionary).Name)); } Dictionary dictionary = new Dictionary(); Dictionary dictionary2 = (Dictionary)this._storage[key]; foreach (KeyValuePair keyValuePair in dictionary2) { if (!(dictionary2[keyValuePair.Key] is TField)) { throw new InvalidOperationException(string.Format("Found {0} at _storage[{1}]. Expecting {2}", this._storage[key].GetType().Name, key, typeof(TField).Name)); } dictionary.Add(keyValuePair.Key, (TField)((object)dictionary2[keyValuePair.Key])); } return dictionary; } public Dictionary GetDictionary(string key) where TKey : struct, IConvertible { if (!typeof(TKey).IsEnum()) { throw new ArgumentException("T must be an Enum."); } if (!this._storage.ContainsKey(key)) { return new Dictionary(); } if (!(this._storage[key] is Dictionary)) { throw new InvalidOperationException(string.Format("Found {0} at _storage[{1}]. Expecting {2}", this._storage[key].GetType().Name, key, typeof(Dictionary).Name)); } Dictionary dictionary = new Dictionary(); Dictionary dictionary2 = (Dictionary)this._storage[key]; foreach (KeyValuePair keyValuePair in dictionary2) { if (!Enum.IsDefined(typeof(TKey), Convert.ToInt32(keyValuePair.Key))) { throw new InvalidOperationException(string.Format("{0} is not an underlying value of the {1} enum", keyValuePair.Key, typeof(TKey).Name)); } if (!(dictionary2[keyValuePair.Key] is TValue)) { throw new InvalidOperationException(string.Format("Found {0} at _storage[{1}]. Expecting {2}", this._storage[key].GetType().Name, key, typeof(TValue).Name)); } dictionary.Add((TKey)((object)Enum.Parse(typeof(TKey), keyValuePair.Key)), (TValue)((object)dictionary2[keyValuePair.Key])); } return dictionary; } public Dictionary GetDictionaryModels(string key, Func factoryMethod) { Dictionary> dictionary = this.GetDictionary>(key); Dictionary dictionary2 = new Dictionary(); foreach (KeyValuePair> keyValuePair in dictionary) { dictionary2.Add(keyValuePair.Key, factoryMethod(new StorageDictionary(keyValuePair.Value))); } return dictionary2; } public Dictionary GetDictionaryModelsWithNulls(string key, Func factoryMethod) { if (!this._storage.ContainsKey(key)) { return new Dictionary(); } if (!(this._storage[key] is Dictionary)) { throw new InvalidOperationException(string.Format("Found {0} at _storage[{1}]. Expecting {2}", this._storage[key].GetType().Name, key, typeof(List).Name)); } Dictionary dictionary = (Dictionary)this._storage[key]; Dictionary dictionary2 = new Dictionary(); foreach (KeyValuePair keyValuePair in dictionary) { if (keyValuePair.Value == null) { dictionary2.Add(keyValuePair.Key, default(TModel)); } else { if (!(dictionary[keyValuePair.Key] is Dictionary)) { throw new InvalidOperationException(string.Format("Found {0} at _storage[{1}]. Expecting {2}", this._storage[key].GetType().Name, key, typeof(Dictionary).Name)); } dictionary2.Add(keyValuePair.Key, factoryMethod(new StorageDictionary((Dictionary)keyValuePair.Value))); } } return dictionary2; } public List GetModels(string key, Func factoryMethod) { List> list = this.GetList>(key); List list2 = new List(); int count = list.Count; for (int i = 0; i < count; i++) { list2.Add(factoryMethod(new StorageDictionary(list[i]))); } return list2; } public List GetModelsWithNulls(string key, Func factoryMethod) { if (!(this._storage[key] is List)) { throw new InvalidOperationException(string.Format("Found {0} at _storage[{1}]. Expecting {2}", this._storage[key].GetType().Name, key, typeof(List).Name)); } List list = new List(); List list2 = (List)this._storage[key]; int count = list2.Count; for (int i = 0; i < count; i++) { if (list2[i] == null) { list.Add(default(TModel)); } else { if (!(list2[i] is Dictionary)) { throw new InvalidOperationException(string.Format("Found {0} at _storage[{1}][{2}]. Expecting {3}", new object[] { list2[i].GetType().Name, key, i, typeof(Dictionary).Name })); } list.Add(factoryMethod(new StorageDictionary((Dictionary)list2[i]))); } } return list; } public Dictionary InternalDictionary { get { return this._storage; } } private Dictionary _storage; } }