| | 1 | | using System.Collections; |
| | 2 | |
|
| | 3 | | namespace Kestrun.Utilities; |
| | 4 | |
|
| | 5 | | /// <summary> |
| | 6 | | /// Adapts a non-generic IDictionary to a read-only dictionary with string keys and nullable object values. |
| | 7 | | /// </summary> |
| | 8 | | /// <remarks> |
| | 9 | | /// Initializes a new instance of the <see cref="ReadOnlyDictionaryAdapter"/> class with the specified non-generic dicti |
| | 10 | | /// </remarks> |
| | 11 | | /// <param name="inner">The non-generic <see cref="IDictionary"/> to adapt.</param> |
| | 12 | | /// <exception cref="ArgumentNullException">Thrown when <paramref name="inner"/> is null.</exception> |
| 4 | 13 | | public sealed class ReadOnlyDictionaryAdapter(IDictionary inner) : IReadOnlyDictionary<string, object?> |
| | 14 | | { |
| 4 | 15 | | private readonly IDictionary _inner = inner ?? throw new ArgumentNullException(nameof(inner)); |
| | 16 | |
|
| | 17 | | /// <summary> |
| | 18 | | /// Gets the value associated with the specified string key, or null if the key does not exist. |
| | 19 | | /// </summary> |
| | 20 | | /// <param name="key">The string key whose value to get.</param> |
| | 21 | | /// <returns>The value associated with the specified key, or null if the key is not found.</returns> |
| | 22 | | public object? this[string key] |
| | 23 | | { |
| | 24 | | get |
| | 25 | | { |
| 6 | 26 | | ArgumentNullException.ThrowIfNull(key); |
| 6 | 27 | | return _inner.Contains(key) ? _inner[key] : null; |
| | 28 | | } |
| | 29 | | } |
| | 30 | |
|
| | 31 | | /// <summary> |
| | 32 | | /// Gets an enumerable collection of the string keys in the adapted dictionary. |
| | 33 | | /// </summary> |
| | 34 | | public IEnumerable<string> Keys => |
| 10 | 35 | | _inner.Keys.Cast<object>().Select(k => k?.ToString() ?? string.Empty); |
| | 36 | |
|
| | 37 | | /// <summary> |
| | 38 | | /// Gets an enumerable collection of the values in the adapted dictionary. |
| | 39 | | /// </summary> |
| | 40 | | public IEnumerable<object?> Values => |
| 5 | 41 | | Keys.Select(k => this[k]); |
| | 42 | |
|
| | 43 | | /// <summary> |
| | 44 | | /// Gets the number of key/value pairs contained in the adapted dictionary. |
| | 45 | | /// </summary> |
| 0 | 46 | | public int Count => _inner.Count; |
| | 47 | |
|
| | 48 | | /// <summary> |
| | 49 | | /// Determines whether the adapted dictionary contains an element with the specified string key. |
| | 50 | | /// </summary> |
| | 51 | | /// <param name="key">The string key to locate in the dictionary.</param> |
| | 52 | | /// <returns>true if the dictionary contains an element with the specified key; otherwise, false.</returns> |
| 2 | 53 | | public bool ContainsKey(string key) => key is not null && _inner.Contains(key); |
| | 54 | |
|
| | 55 | | /// <summary> |
| | 56 | | /// Attempts to get the value associated with the specified string key. |
| | 57 | | /// </summary> |
| | 58 | | /// <param name="key">The string key whose value to get.</param> |
| | 59 | | /// <param name="value">When this method returns, contains the value associated with the specified key, if the key i |
| | 60 | | /// <returns>true if the dictionary contains an element with the specified key; otherwise, false.</returns> |
| | 61 | | public bool TryGetValue(string key, out object? value) |
| | 62 | | { |
| 2 | 63 | | if (key is null) |
| | 64 | | { |
| 0 | 65 | | value = null; |
| 0 | 66 | | return false; |
| | 67 | | } |
| | 68 | |
|
| 2 | 69 | | if (_inner.Contains(key)) |
| | 70 | | { |
| 1 | 71 | | value = _inner[key]; |
| 1 | 72 | | return true; |
| | 73 | | } |
| | 74 | |
|
| 1 | 75 | | value = null; |
| 1 | 76 | | return false; |
| | 77 | | } |
| | 78 | |
|
| | 79 | | /// <summary> |
| | 80 | | /// Returns an enumerator that iterates through the key/value pairs in the adapted dictionary. |
| | 81 | | /// </summary> |
| | 82 | | /// <returns>An enumerator for the key/value pairs.</returns> |
| | 83 | | public IEnumerator<KeyValuePair<string, object?>> GetEnumerator() |
| | 84 | | { |
| 10 | 85 | | foreach (DictionaryEntry entry in _inner) |
| | 86 | | { |
| 3 | 87 | | var k = entry.Key?.ToString() |
| 3 | 88 | | ?? throw new InvalidOperationException("Underlying dictionary contains a null key."); |
| 3 | 89 | | yield return new KeyValuePair<string, object?>(k, entry.Value); |
| | 90 | | } |
| 2 | 91 | | } |
| | 92 | |
|
| 0 | 93 | | IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); |
| | 94 | | } |