| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Collections; |
| | 6 | | using System.Collections.Generic; |
| | 7 | |
|
| | 8 | | #nullable enable |
| | 9 | |
|
| | 10 | | namespace Azure.Core |
| | 11 | | { |
| | 12 | | internal class ChangeTrackingDictionary<TKey, TValue> : IDictionary<TKey, TValue>, IReadOnlyDictionary<TKey, TValue> |
| | 13 | | { |
| | 14 | | private IDictionary<TKey, TValue>? _innerDictionary; |
| | 15 | |
|
| 0 | 16 | | public ChangeTrackingDictionary() |
| | 17 | | { |
| 0 | 18 | | } |
| | 19 | |
|
| 0 | 20 | | public ChangeTrackingDictionary(Optional<IReadOnlyDictionary<TKey, TValue>> optionalDictionary) : this(optionalD |
| | 21 | | { |
| 0 | 22 | | } |
| | 23 | |
|
| 24 | 24 | | public ChangeTrackingDictionary(Optional<IDictionary<TKey, TValue>> optionalDictionary) : this(optionalDictionar |
| | 25 | | { |
| 24 | 26 | | } |
| | 27 | |
|
| 24 | 28 | | private ChangeTrackingDictionary(IDictionary<TKey, TValue> dictionary) |
| | 29 | | { |
| 48 | 30 | | if (dictionary == null) return; |
| | 31 | |
|
| 0 | 32 | | _innerDictionary = new Dictionary<TKey, TValue>(dictionary); |
| 0 | 33 | | } |
| | 34 | |
|
| 0 | 35 | | private ChangeTrackingDictionary(IReadOnlyDictionary<TKey, TValue> dictionary) |
| | 36 | | { |
| 0 | 37 | | if (dictionary == null) return; |
| | 38 | |
|
| 0 | 39 | | _innerDictionary = new Dictionary<TKey, TValue>(); |
| 0 | 40 | | foreach (KeyValuePair<TKey, TValue> pair in dictionary) |
| | 41 | | { |
| 0 | 42 | | _innerDictionary.Add(pair); |
| | 43 | | } |
| 0 | 44 | | } |
| | 45 | |
|
| 0 | 46 | | public bool IsUndefined => _innerDictionary == null; |
| | 47 | |
|
| | 48 | | public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator() |
| | 49 | | { |
| 0 | 50 | | if (IsUndefined) |
| | 51 | | { |
| | 52 | | IEnumerator<KeyValuePair<TKey, TValue>> GetEmptyEnumerator() |
| | 53 | | { |
| 0 | 54 | | yield break; |
| | 55 | | } |
| 0 | 56 | | return GetEmptyEnumerator(); |
| | 57 | | } |
| 0 | 58 | | return EnsureDictionary().GetEnumerator(); |
| | 59 | | } |
| | 60 | |
|
| | 61 | | IEnumerator IEnumerable.GetEnumerator() |
| | 62 | | { |
| 0 | 63 | | return GetEnumerator(); |
| | 64 | | } |
| | 65 | |
|
| | 66 | | public void Add(KeyValuePair<TKey, TValue> item) |
| | 67 | | { |
| 0 | 68 | | EnsureDictionary().Add(item); |
| 0 | 69 | | } |
| | 70 | |
|
| | 71 | | public void Clear() |
| | 72 | | { |
| 0 | 73 | | EnsureDictionary().Clear(); |
| 0 | 74 | | } |
| | 75 | |
|
| | 76 | | public bool Contains(KeyValuePair<TKey, TValue> item) |
| | 77 | | { |
| 0 | 78 | | if (IsUndefined) |
| | 79 | | { |
| 0 | 80 | | return false; |
| | 81 | | } |
| | 82 | |
|
| 0 | 83 | | return EnsureDictionary().Contains(item); |
| | 84 | | } |
| | 85 | |
|
| | 86 | | public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex) |
| | 87 | | { |
| 0 | 88 | | if (IsUndefined) |
| | 89 | | { |
| 0 | 90 | | return; |
| | 91 | | } |
| | 92 | |
|
| 0 | 93 | | EnsureDictionary().CopyTo(array, arrayIndex); |
| 0 | 94 | | } |
| | 95 | |
|
| | 96 | | public bool Remove(KeyValuePair<TKey, TValue> item) |
| | 97 | | { |
| 0 | 98 | | if (IsUndefined) |
| | 99 | | { |
| 0 | 100 | | return false; |
| | 101 | | } |
| | 102 | |
|
| 0 | 103 | | return EnsureDictionary().Remove(item); |
| | 104 | | } |
| | 105 | |
|
| | 106 | | public int Count |
| | 107 | | { |
| | 108 | | get |
| | 109 | | { |
| 0 | 110 | | if (IsUndefined) |
| | 111 | | { |
| 0 | 112 | | return 0; |
| | 113 | | } |
| | 114 | |
|
| 0 | 115 | | return EnsureDictionary().Count; |
| | 116 | | } |
| | 117 | | } |
| | 118 | |
|
| | 119 | | public bool IsReadOnly |
| | 120 | | { |
| | 121 | | get |
| | 122 | | { |
| 0 | 123 | | if (IsUndefined) |
| | 124 | | { |
| 0 | 125 | | return false; |
| | 126 | | } |
| 0 | 127 | | return EnsureDictionary().IsReadOnly; |
| | 128 | | } |
| | 129 | | } |
| | 130 | |
|
| | 131 | | public void Add(TKey key, TValue value) |
| | 132 | | { |
| 0 | 133 | | EnsureDictionary().Add(key, value); |
| 0 | 134 | | } |
| | 135 | |
|
| | 136 | | public bool ContainsKey(TKey key) |
| | 137 | | { |
| 0 | 138 | | if (IsUndefined) |
| | 139 | | { |
| 0 | 140 | | return false; |
| | 141 | | } |
| | 142 | |
|
| 0 | 143 | | return EnsureDictionary().ContainsKey(key); |
| | 144 | | } |
| | 145 | |
|
| | 146 | | public bool Remove(TKey key) |
| | 147 | | { |
| 0 | 148 | | if (IsUndefined) |
| | 149 | | { |
| 0 | 150 | | return false; |
| | 151 | | } |
| | 152 | |
|
| 0 | 153 | | return EnsureDictionary().Remove(key); |
| | 154 | | } |
| | 155 | |
|
| | 156 | | public bool TryGetValue(TKey key, out TValue value) |
| | 157 | | { |
| 0 | 158 | | if (IsUndefined) |
| | 159 | | { |
| 0 | 160 | | value = default!; |
| 0 | 161 | | return false; |
| | 162 | | } |
| 0 | 163 | | return EnsureDictionary().TryGetValue(key, out value); |
| | 164 | | } |
| | 165 | |
|
| | 166 | | public TValue this[TKey key] |
| | 167 | | { |
| | 168 | | get |
| | 169 | | { |
| 0 | 170 | | if (IsUndefined) |
| | 171 | | { |
| 0 | 172 | | throw new KeyNotFoundException(nameof(key)); |
| | 173 | | } |
| | 174 | |
|
| 0 | 175 | | return EnsureDictionary()[key]; |
| | 176 | | } |
| 0 | 177 | | set => EnsureDictionary()[key] = value; |
| | 178 | | } |
| | 179 | |
|
| 0 | 180 | | IEnumerable<TKey> IReadOnlyDictionary<TKey, TValue>.Keys => Keys; |
| | 181 | |
|
| 0 | 182 | | IEnumerable<TValue> IReadOnlyDictionary<TKey, TValue>.Values => Values; |
| | 183 | |
|
| | 184 | | public ICollection<TKey> Keys |
| | 185 | | { |
| | 186 | | get |
| | 187 | | { |
| 0 | 188 | | if (IsUndefined) |
| | 189 | | { |
| 0 | 190 | | return Array.Empty<TKey>(); |
| | 191 | | } |
| | 192 | |
|
| 0 | 193 | | return EnsureDictionary().Keys; |
| | 194 | | } |
| | 195 | | } |
| | 196 | |
|
| | 197 | | public ICollection<TValue> Values |
| | 198 | | { |
| | 199 | | get |
| | 200 | | { |
| 0 | 201 | | if (IsUndefined) |
| | 202 | | { |
| 0 | 203 | | return Array.Empty<TValue>(); |
| | 204 | | } |
| | 205 | |
|
| 0 | 206 | | return EnsureDictionary().Values; |
| | 207 | | } |
| | 208 | | } |
| | 209 | |
|
| | 210 | | private IDictionary<TKey, TValue> EnsureDictionary() |
| | 211 | | { |
| 0 | 212 | | return _innerDictionary ??= new Dictionary<TKey, TValue>(); |
| | 213 | | } |
| | 214 | | } |
| | 215 | | } |