using System; using System.Collections.Generic; using System.Collections.ObjectModel; using DPumpHydr.WinFrmUI.WenSkin.Json.Utilities; namespace DPumpHydr.WinFrmUI.WenSkin.Json.Linq { internal class JPropertyKeyedCollection : Collection { private static readonly IEqualityComparer Comparer = StringComparer.Ordinal; private Dictionary _dictionary; public JToken this[string key] { get { if (key == null) { throw new ArgumentNullException("key"); } if (_dictionary != null) { return _dictionary[key]; } throw new KeyNotFoundException(); } } public ICollection Keys { get { EnsureDictionary(); return _dictionary.Keys; } } public ICollection Values { get { EnsureDictionary(); return _dictionary.Values; } } public JPropertyKeyedCollection() : base((IList)new List()) { } private void AddKey(string key, JToken item) { EnsureDictionary(); _dictionary[key] = item; } protected void ChangeItemKey(JToken item, string newKey) { if (!ContainsItem(item)) { throw new ArgumentException("The specified item does not exist in this KeyedCollection."); } string keyForItem = GetKeyForItem(item); if (!Comparer.Equals(keyForItem, newKey)) { if (newKey != null) { AddKey(newKey, item); } if (keyForItem != null) { RemoveKey(keyForItem); } } } protected override void ClearItems() { base.ClearItems(); if (_dictionary != null) { _dictionary.Clear(); } } public bool Contains(string key) { if (key == null) { throw new ArgumentNullException("key"); } if (_dictionary != null) { return _dictionary.ContainsKey(key); } return false; } private bool ContainsItem(JToken item) { if (_dictionary == null) { return false; } string keyForItem = GetKeyForItem(item); JToken value; return _dictionary.TryGetValue(keyForItem, out value); } private void EnsureDictionary() { if (_dictionary == null) { _dictionary = new Dictionary(Comparer); } } private string GetKeyForItem(JToken item) { return ((JProperty)item).Name; } protected override void InsertItem(int index, JToken item) { AddKey(GetKeyForItem(item), item); base.InsertItem(index, item); } public bool Remove(string key) { if (key == null) { throw new ArgumentNullException("key"); } if (_dictionary != null) { if (_dictionary.ContainsKey(key)) { return Remove(_dictionary[key]); } return false; } return false; } protected override void RemoveItem(int index) { string keyForItem = GetKeyForItem(base.Items[index]); RemoveKey(keyForItem); base.RemoveItem(index); } private void RemoveKey(string key) { if (_dictionary != null) { _dictionary.Remove(key); } } protected override void SetItem(int index, JToken item) { string keyForItem = GetKeyForItem(item); string keyForItem2 = GetKeyForItem(base.Items[index]); if (Comparer.Equals(keyForItem2, keyForItem)) { if (_dictionary != null) { _dictionary[keyForItem] = item; } } else { AddKey(keyForItem, item); if (keyForItem2 != null) { RemoveKey(keyForItem2); } } base.SetItem(index, item); } public bool TryGetValue(string key, out JToken value) { if (_dictionary == null) { value = null; return false; } return _dictionary.TryGetValue(key, out value); } public int IndexOfReference(JToken t) { return ((List)base.Items).IndexOfReference(t); } public bool Compare(JPropertyKeyedCollection other) { if (this == other) { return true; } Dictionary dictionary = _dictionary; Dictionary dictionary2 = other._dictionary; if (dictionary == null && dictionary2 == null) { return true; } if (dictionary == null) { return dictionary2.Count == 0; } if (dictionary2 == null) { return dictionary.Count == 0; } if (dictionary.Count != dictionary2.Count) { return false; } foreach (KeyValuePair item in dictionary) { if (!dictionary2.TryGetValue(item.Key, out var value)) { return false; } JProperty jProperty = (JProperty)item.Value; JProperty jProperty2 = (JProperty)value; if (jProperty.Value == null) { return jProperty2.Value == null; } if (!jProperty.Value.DeepEquals(jProperty2.Value)) { return false; } } return true; } } }