|
| 1 | +using System; |
| 2 | +using System.Collections.Frozen; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Diagnostics.CodeAnalysis; |
| 5 | +using Collections = System.Collections; |
| 6 | + |
| 7 | +namespace Anthropic.Client.Core; |
| 8 | + |
| 9 | +/// <summary> |
| 10 | +/// A dictionary that can be mutated and then frozen once no more mutations are expected.<br |
| 11 | +/// /><br /> |
| 12 | +/// |
| 13 | +/// This is useful for allowing a dictionary to be modified by a class's `init` properties, |
| 14 | +/// but then preventing it from being modified afterwards. |
| 15 | +/// </summary> |
| 16 | +class FreezableDictionary<K, V> : IDictionary<K, V> |
| 17 | + where K : notnull |
| 18 | +{ |
| 19 | + IDictionary<K, V> _dictionary = new Dictionary<K, V>(); |
| 20 | + |
| 21 | + Dictionary<K, V> _mutableDictionary |
| 22 | + { |
| 23 | + get |
| 24 | + { |
| 25 | + if (_dictionary is Dictionary<K, V> dict) |
| 26 | + { |
| 27 | + return dict; |
| 28 | + } |
| 29 | + |
| 30 | + throw new InvalidOperationException("Can't mutate after freezing."); |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + public FreezableDictionary() { } |
| 35 | + |
| 36 | + public FreezableDictionary(IReadOnlyDictionary<K, V> dictionary) |
| 37 | + { |
| 38 | + _dictionary = new Dictionary<K, V>(dictionary); |
| 39 | + } |
| 40 | + |
| 41 | + public FreezableDictionary(FrozenDictionary<K, V> frozen) |
| 42 | + { |
| 43 | + _dictionary = frozen; |
| 44 | + } |
| 45 | + |
| 46 | + public IReadOnlyDictionary<K, V> Freeze() |
| 47 | + { |
| 48 | + if (_dictionary is FrozenDictionary<K, V> dict) |
| 49 | + { |
| 50 | + return dict; |
| 51 | + } |
| 52 | + |
| 53 | + var dictionary = FrozenDictionary.ToFrozenDictionary(_dictionary); |
| 54 | + _dictionary = dictionary; |
| 55 | + |
| 56 | + return dictionary; |
| 57 | + } |
| 58 | + |
| 59 | + public V this[K key] |
| 60 | + { |
| 61 | + get => _dictionary[key]; |
| 62 | + set => _mutableDictionary[key] = value; |
| 63 | + } |
| 64 | + |
| 65 | + public ICollection<K> Keys |
| 66 | + { |
| 67 | + get { return _dictionary.Keys; } |
| 68 | + } |
| 69 | + |
| 70 | + public ICollection<V> Values |
| 71 | + { |
| 72 | + get { return _dictionary.Values; } |
| 73 | + } |
| 74 | + |
| 75 | + public int Count |
| 76 | + { |
| 77 | + get { return _dictionary.Count; } |
| 78 | + } |
| 79 | + |
| 80 | + public bool IsReadOnly |
| 81 | + { |
| 82 | + get { return _dictionary.IsReadOnly; } |
| 83 | + } |
| 84 | + |
| 85 | + public void Add(K key, V value) |
| 86 | + { |
| 87 | + _mutableDictionary.Add(key, value); |
| 88 | + } |
| 89 | + |
| 90 | + public void Add(KeyValuePair<K, V> item) |
| 91 | + { |
| 92 | + _mutableDictionary.Add(item.Key, item.Value); |
| 93 | + } |
| 94 | + |
| 95 | + public void Clear() |
| 96 | + { |
| 97 | + _mutableDictionary.Clear(); |
| 98 | + } |
| 99 | + |
| 100 | + public bool Contains(KeyValuePair<K, V> item) |
| 101 | + { |
| 102 | + return _dictionary.Contains(item); |
| 103 | + } |
| 104 | + |
| 105 | + public bool ContainsKey(K key) |
| 106 | + { |
| 107 | + return _dictionary.ContainsKey(key); |
| 108 | + } |
| 109 | + |
| 110 | + public void CopyTo(KeyValuePair<K, V>[] array, int arrayIndex) |
| 111 | + { |
| 112 | + _dictionary.CopyTo(array, arrayIndex); |
| 113 | + } |
| 114 | + |
| 115 | + public IEnumerator<KeyValuePair<K, V>> GetEnumerator() |
| 116 | + { |
| 117 | + return _dictionary.GetEnumerator(); |
| 118 | + } |
| 119 | + |
| 120 | + public bool Remove(K key) |
| 121 | + { |
| 122 | + return _mutableDictionary.Remove(key); |
| 123 | + } |
| 124 | + |
| 125 | + public bool Remove(KeyValuePair<K, V> item) |
| 126 | + { |
| 127 | + return _mutableDictionary.Remove(item.Key); |
| 128 | + } |
| 129 | + |
| 130 | + public bool TryGetValue(K key, [MaybeNullWhenAttribute(false)] out V value) |
| 131 | + { |
| 132 | + return _dictionary.TryGetValue(key, out value); |
| 133 | + } |
| 134 | + |
| 135 | + Collections::IEnumerator Collections::IEnumerable.GetEnumerator() |
| 136 | + { |
| 137 | + return _dictionary.GetEnumerator(); |
| 138 | + } |
| 139 | +} |
0 commit comments