|
| 1 | +/* Code generated by Azure.Iot.Operations.ProtocolCompiler; DO NOT EDIT. */ |
| 2 | + |
| 3 | +namespace PTZ |
| 4 | +{ |
| 5 | + using System; |
| 6 | + using System.Collections; |
| 7 | + using System.Collections.Generic; |
| 8 | + using System.Linq; |
| 9 | + |
| 10 | + /// <summary> |
| 11 | + /// An implementation of <c>IReadOnlyDictionary</c> that combines two <c>IReadOnlyDictionary</c> objects by prefixng their string keys. |
| 12 | + /// </summary> |
| 13 | + /// <typeparam name="TKey">The type of keys in the combined dictionary.</typeparam> |
| 14 | + /// <typeparam name="TValue">The type of values in the combined dictionary.</typeparam> |
| 15 | + public class CombinedPrefixedReadOnlyDictionary<TValue> : IReadOnlyDictionary<string, TValue> |
| 16 | + { |
| 17 | + private string prefix1; |
| 18 | + private IReadOnlyDictionary<string, TValue> dict1; |
| 19 | + private string prefix2; |
| 20 | + private IReadOnlyDictionary<string, TValue> dict2; |
| 21 | + |
| 22 | + /// <summary> |
| 23 | + /// Initializes a new instance of the <see cref="CombinedPrefixedReadOnlyDictionary{TValue}"/> class. |
| 24 | + /// </summary> |
| 25 | + /// <param name="prefix1">The prefix for keys in <paramref name="dict1"/>.</param> |
| 26 | + /// <param name="dict1">One of the <c>IReadOnlyDictionary</c> objects to combine.</param> |
| 27 | + /// <param name="prefix2">The prefix for keys in <paramref name="dict2"/>.</param> |
| 28 | + /// <param name="dict2">The other <c>IReadOnlyDictionary</c> object to combine.</param> |
| 29 | + public CombinedPrefixedReadOnlyDictionary( |
| 30 | + string prefix1, |
| 31 | + IReadOnlyDictionary<string, TValue> dict1, |
| 32 | + string prefix2, |
| 33 | + IReadOnlyDictionary<string, TValue> dict2) |
| 34 | + { |
| 35 | + ArgumentNullException.ThrowIfNull(prefix1, nameof(prefix1)); |
| 36 | + ArgumentNullException.ThrowIfNull(dict1, nameof(dict1)); |
| 37 | + ArgumentNullException.ThrowIfNull(prefix2, nameof(prefix2)); |
| 38 | + ArgumentNullException.ThrowIfNull(dict2, nameof(dict2)); |
| 39 | + |
| 40 | + this.prefix1 = prefix1; |
| 41 | + this.dict1 = dict1; |
| 42 | + this.prefix2 = prefix2; |
| 43 | + this.dict2 = dict2; |
| 44 | + } |
| 45 | + |
| 46 | + /// <inheritdoc/> |
| 47 | + IEnumerable<string> IReadOnlyDictionary<string, TValue>.Keys => this.dict1.Keys.Select(k => $"{this.prefix1}{k}").Concat(this.dict2.Keys.Select(k => $"{this.prefix2}{k}")); |
| 48 | + |
| 49 | + /// <inheritdoc/> |
| 50 | + IEnumerable<TValue> IReadOnlyDictionary<string, TValue>.Values => this.dict1.Values.Concat(this.dict2.Values); |
| 51 | + |
| 52 | + /// <inheritdoc/> |
| 53 | + int IReadOnlyCollection<KeyValuePair<string, TValue>>.Count => this.dict1.Count + this.dict2.Count; |
| 54 | + |
| 55 | + /// <inheritdoc/> |
| 56 | + TValue IReadOnlyDictionary<string, TValue>.this[string key] => |
| 57 | + key.StartsWith(this.prefix1, StringComparison.InvariantCulture) && this.dict1.TryGetValue(key.Substring(this.prefix1.Length), out TValue? value1) ? value1 : |
| 58 | + key.StartsWith(this.prefix2, StringComparison.InvariantCulture) && this.dict2.TryGetValue(key.Substring(this.prefix2.Length), out TValue? value2) ? value2 : |
| 59 | + default(TValue)!; |
| 60 | + |
| 61 | + /// <inheritdoc/> |
| 62 | + bool IReadOnlyDictionary<string, TValue>.ContainsKey(string key) |
| 63 | + { |
| 64 | + return |
| 65 | + key.StartsWith(this.prefix1, StringComparison.InvariantCulture) && this.dict1.ContainsKey(key.Substring(this.prefix1.Length)) || |
| 66 | + key.StartsWith(this.prefix2, StringComparison.InvariantCulture) && this.dict2.ContainsKey(key.Substring(this.prefix2.Length)); |
| 67 | + } |
| 68 | + |
| 69 | + /// <inheritdoc/> |
| 70 | + IEnumerator<KeyValuePair<string, TValue>> IEnumerable<KeyValuePair<string, TValue>>.GetEnumerator() |
| 71 | + { |
| 72 | + foreach (var item in this.dict1) |
| 73 | + { |
| 74 | + yield return new KeyValuePair<string, TValue>($"{this.prefix1}{item.Key}", item.Value); |
| 75 | + } |
| 76 | + |
| 77 | + foreach (var item in this.dict2) |
| 78 | + { |
| 79 | + yield return new KeyValuePair<string, TValue>($"{this.prefix2}{item.Key}", item.Value); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + /// <inheritdoc/> |
| 84 | + bool IReadOnlyDictionary<string, TValue>.TryGetValue(string key, out TValue value) |
| 85 | + { |
| 86 | + if (key.StartsWith(this.prefix1, StringComparison.InvariantCulture) && this.dict1.TryGetValue(key.Substring(this.prefix1.Length), out TValue? value1)) |
| 87 | + { |
| 88 | + value = value1; |
| 89 | + return true; |
| 90 | + } |
| 91 | + |
| 92 | + if (key.StartsWith(this.prefix2, StringComparison.InvariantCulture) && this.dict2.TryGetValue(key.Substring(this.prefix2.Length), out TValue? value2)) |
| 93 | + { |
| 94 | + value = value2; |
| 95 | + return true; |
| 96 | + } |
| 97 | + |
| 98 | + value = default(TValue)!; |
| 99 | + return false; |
| 100 | + } |
| 101 | + |
| 102 | + /// <inheritdoc/> |
| 103 | + IEnumerator IEnumerable.GetEnumerator() |
| 104 | + { |
| 105 | + return ((IEnumerable<KeyValuePair<string, TValue>>)this).GetEnumerator(); |
| 106 | + } |
| 107 | + } |
| 108 | +} |
0 commit comments