namespace Firebase.Database.Offline { using System; using System.Collections; using System.Collections.Generic; using System.Linq; internal class OfflineCacheAdapter : IDictionary, IDictionary { private readonly IDictionary database; public OfflineCacheAdapter(IDictionary database) { this.database = database; } public void CopyTo(Array array, int index) { throw new NotImplementedException(); } public int Count => this.database.Count; public bool IsSynchronized { get; } public object SyncRoot { get; } public bool IsReadOnly => this.database.IsReadOnly; object IDictionary.this[object key] { get { return this.database[key.ToString()].Deserialize(); } set { var keyString = key.ToString(); if (this.database.ContainsKey(keyString)) { this.database[keyString] = new OfflineEntry(keyString, value, this.database[keyString].Priority, this.database[keyString].SyncOptions); } else { this.database[keyString] = new OfflineEntry(keyString, value, 1, SyncOptions.None); } } } public ICollection Keys => this.database.Keys; ICollection IDictionary.Values { get; } ICollection IDictionary.Keys { get; } public ICollection Values => this.database.Values.Select(o => o.Deserialize()).ToList(); public T this[string key] { get { return this.database[key].Deserialize(); } set { if (this.database.ContainsKey(key)) { this.database[key] = new OfflineEntry(key, value, this.database[key].Priority, this.database[key].SyncOptions); } else { this.database[key] = new OfflineEntry(key, value, 1, SyncOptions.None); } } } public bool Contains(object key) { return this.ContainsKey(key.ToString()); } IDictionaryEnumerator IDictionary.GetEnumerator() { throw new NotImplementedException(); } public void Remove(object key) { this.Remove(key.ToString()); } public bool IsFixedSize => false; public IEnumerator> GetEnumerator() { return this.database.Select(d => new KeyValuePair(d.Key, d.Value.Deserialize())).GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { return this.GetEnumerator(); } public void Add(KeyValuePair item) { this.Add(item.Key, item.Value); } public void Add(object key, object value) { this.Add(key.ToString(), (T)value); } public void Clear() { this.database.Clear(); } public bool Contains(KeyValuePair item) { return this.ContainsKey(item.Key); } public void CopyTo(KeyValuePair[] array, int arrayIndex) { throw new NotImplementedException(); } public bool Remove(KeyValuePair item) { return this.database.Remove(item.Key); } public void Add(string key, T value) { this.database.Add(key, new OfflineEntry(key, value, 1, SyncOptions.None)); } public bool ContainsKey(string key) { return this.database.ContainsKey(key); } public bool Remove(string key) { return this.database.Remove(key); } public bool TryGetValue(string key, out T value) { OfflineEntry val; if (this.database.TryGetValue(key, out val)) { value = val.Deserialize(); return true; } value = default(T); return false; } } }