ReadOnlyDictionary.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Collections.Generic;
  5. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities.Collections
  6. {
  7. internal abstract class ReadOnlyDictionary<K, V>
  8. : IDictionary<K, V>
  9. {
  10. public V this[K key]
  11. {
  12. get { return Lookup(key); }
  13. set { throw new NotSupportedException(); }
  14. }
  15. System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
  16. {
  17. return GetEnumerator();
  18. }
  19. public bool IsReadOnly => true;
  20. public void Add(K key, V value) => throw new NotSupportedException();
  21. public void Add(KeyValuePair<K, V> item) => throw new NotSupportedException();
  22. public void Clear() => throw new NotSupportedException();
  23. public bool Remove(K key) => throw new NotSupportedException();
  24. public bool Remove(KeyValuePair<K, V> item) => throw new NotSupportedException();
  25. public abstract bool Contains(KeyValuePair<K, V> item);
  26. public abstract bool ContainsKey(K key);
  27. public abstract void CopyTo(KeyValuePair<K, V>[] array, int arrayIndex);
  28. public abstract int Count { get; }
  29. public abstract IEnumerator<KeyValuePair<K, V>> GetEnumerator();
  30. public abstract ICollection<K> Keys { get; }
  31. public abstract bool TryGetValue(K key, out V value);
  32. public abstract ICollection<V> Values { get; }
  33. protected abstract V Lookup(K key);
  34. }
  35. internal class ReadOnlyDictionaryProxy<K, V>
  36. : ReadOnlyDictionary<K, V>
  37. {
  38. private readonly IDictionary<K, V> m_target;
  39. internal ReadOnlyDictionaryProxy(IDictionary<K, V> target)
  40. {
  41. if (target == null)
  42. throw new ArgumentNullException(nameof(target));
  43. m_target = target;
  44. }
  45. public override bool Contains(KeyValuePair<K, V> item) => m_target.Contains(item);
  46. public override bool ContainsKey(K key) => m_target.ContainsKey(key);
  47. public override void CopyTo(KeyValuePair<K, V>[] array, int arrayIndex) => m_target.CopyTo(array, arrayIndex);
  48. public override int Count => m_target.Count;
  49. public override IEnumerator<KeyValuePair<K, V>> GetEnumerator() => m_target.GetEnumerator();
  50. public override ICollection<K> Keys => new ReadOnlyCollectionProxy<K>(m_target.Keys);
  51. public override bool TryGetValue(K key, out V value) => m_target.TryGetValue(key, out value);
  52. public override ICollection<V> Values => new ReadOnlyCollectionProxy<V>(m_target.Values);
  53. protected override V Lookup(K key) => m_target[key];
  54. }
  55. }
  56. #pragma warning restore
  57. #endif