|
| 1 | +using System.Collections; |
| 2 | +using System.Collections.ObjectModel; |
| 3 | +using System.Diagnostics.CodeAnalysis; |
| 4 | + |
| 5 | +namespace Sagara.Core.Collections; |
| 6 | + |
| 7 | +/// <summary> |
| 8 | +/// Provides a cached, read-only instance for the specified key and value types. |
| 9 | +/// </summary> |
| 10 | +/// <remarks> |
| 11 | +/// <para>We're implementing <see cref="IReadOnlyDictionary{TKey, TValue}"/> to get a true read-only dictionary without |
| 12 | +/// risking various extension methods leading to runtime exceptions. For example, <see cref="ReadOnlyDictionary{TKey, TValue}"/> |
| 13 | +/// implements <see cref="IDictionary{TKey, TValue}"/>, which in turn lights up the <see cref="CollectionExtensions.TryAdd{TKey, TValue}(IDictionary{TKey, TValue}, TKey, TValue)"/> |
| 14 | +/// extension method that will throw at runtime if called on a <see cref="ReadOnlyDictionary{TKey, TValue}"/>.</para> |
| 15 | +/// </remarks> |
| 16 | +/// <typeparam name="TKey">The type of keys in the read-only dictionary.</typeparam> |
| 17 | +/// <typeparam name="TValue">The type of values in the read-only dictionary.</typeparam> |
| 18 | +public sealed class EmptyDictionary<TKey, TValue> : IReadOnlyDictionary<TKey, TValue> |
| 19 | + where TKey : notnull |
| 20 | +{ |
| 21 | + /// <summary> |
| 22 | + /// The one and only instance of <see cref="EmptyDictionary{TKey, TValue}"/> for this key/value type pair. |
| 23 | + /// </summary> |
| 24 | + public static readonly IReadOnlyDictionary<TKey, TValue> Instance = ReadOnlyDictionary<TKey, TValue>.Empty; |
| 25 | + |
| 26 | + /// <summary> |
| 27 | + /// Don't let callers create their own instances. |
| 28 | + /// </summary> |
| 29 | + private EmptyDictionary() |
| 30 | + { |
| 31 | + // Intentionally blank. |
| 32 | + } |
| 33 | + |
| 34 | + /// <inheritdoc/> |
| 35 | + public TValue this[TKey key] |
| 36 | + => Instance[key]; |
| 37 | + |
| 38 | + /// <inheritdoc/> |
| 39 | + public IEnumerable<TKey> Keys |
| 40 | + => Instance.Keys; |
| 41 | + |
| 42 | + /// <inheritdoc/> |
| 43 | + public IEnumerable<TValue> Values |
| 44 | + => Instance.Values; |
| 45 | + |
| 46 | + /// <inheritdoc/> |
| 47 | + public int Count |
| 48 | + => Instance.Count; |
| 49 | + |
| 50 | + /// <inheritdoc/> |
| 51 | + public bool ContainsKey(TKey key) |
| 52 | + => Instance.ContainsKey(key); |
| 53 | + |
| 54 | + /// <inheritdoc/> |
| 55 | + public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator() |
| 56 | + => Instance.GetEnumerator(); |
| 57 | + |
| 58 | + /// <inheritdoc/> |
| 59 | + public bool TryGetValue(TKey key, [MaybeNullWhen(false)] out TValue value) |
| 60 | + => Instance.TryGetValue(key, out value); |
| 61 | + |
| 62 | + /// <inheritdoc/> |
| 63 | + IEnumerator IEnumerable.GetEnumerator() |
| 64 | + => ((IEnumerable)Instance).GetEnumerator(); |
| 65 | +} |
0 commit comments