|
8 | 8 | using System; |
9 | 9 | using System.Collections.Concurrent; |
10 | 10 | using System.Collections.Generic; |
| 11 | +using System.ComponentModel; |
11 | 12 |
|
12 | | -namespace Microsoft.Azure.Functions.Worker |
| 13 | +internal static class IDictionaryExtensions |
13 | 14 | { |
14 | | - internal static class IDictionaryExtensions |
| 15 | + internal static bool TryAdd<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue value) |
15 | 16 | { |
16 | | - internal static bool TryAdd<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue value) |
| 17 | + if (key == null) |
17 | 18 | { |
18 | | - if (key == null) |
19 | | - { |
20 | | - throw new ArgumentNullException(nameof(key)); |
21 | | - } |
22 | | - |
23 | | - if (dictionary.ContainsKey(key)) |
24 | | - { |
25 | | - return false; |
26 | | - } |
27 | | - |
28 | | - dictionary.Add(key, value); |
29 | | - return true; |
| 19 | + throw new ArgumentNullException(nameof(key)); |
30 | 20 | } |
| 21 | + |
| 22 | + if (dictionary.ContainsKey(key)) |
| 23 | + { |
| 24 | + return false; |
| 25 | + } |
| 26 | + |
| 27 | + dictionary.Add(key, value); |
| 28 | + return true; |
31 | 29 | } |
| 30 | +} |
32 | 31 |
|
33 | | - internal static class ConcurrentDictionaryExtensions |
| 32 | +internal static class ConcurrentDictionaryExtensions |
| 33 | +{ |
| 34 | + public static TValue GetOrAdd<TKey, TValue, TArg>(this ConcurrentDictionary<TKey, TValue> dictionary, TKey key, Func<TKey, TArg, TValue> valueFactory, TArg factoryArgument) |
34 | 35 | { |
35 | | - public static TValue GetOrAdd<TKey, TValue, TArg>(this ConcurrentDictionary<TKey, TValue> dictionary, TKey key, Func<TKey, TArg, TValue> valueFactory, TArg factoryArgument) |
| 36 | + if (dictionary == null) |
36 | 37 | { |
37 | | - if (dictionary == null) |
38 | | - { |
39 | | - throw new ArgumentNullException(nameof(dictionary)); |
40 | | - } |
41 | | - |
42 | | - if (key == null) |
43 | | - { |
44 | | - throw new ArgumentNullException(nameof(key)); |
45 | | - } |
46 | | - |
47 | | - if (valueFactory == null) |
48 | | - { |
49 | | - throw new ArgumentNullException(nameof(valueFactory)); |
50 | | - } |
51 | | - |
52 | | - return dictionary.GetOrAdd(key, k => valueFactory(k, factoryArgument)); |
| 38 | + throw new ArgumentNullException(nameof(dictionary)); |
53 | 39 | } |
| 40 | + |
| 41 | + if (key == null) |
| 42 | + { |
| 43 | + throw new ArgumentNullException(nameof(key)); |
| 44 | + } |
| 45 | + |
| 46 | + if (valueFactory == null) |
| 47 | + { |
| 48 | + throw new ArgumentNullException(nameof(valueFactory)); |
| 49 | + } |
| 50 | + |
| 51 | + return dictionary.GetOrAdd(key, k => valueFactory(k, factoryArgument)); |
54 | 52 | } |
55 | 53 | } |
| 54 | + |
| 55 | +internal static class KeyValuePairExtensions |
| 56 | +{ |
| 57 | + // Based on https://source.dot.net/#System.Private.CoreLib/KeyValuePair.cs,aa57b8e336bf7f59 |
| 58 | + [EditorBrowsable(EditorBrowsableState.Never)] |
| 59 | + public static void Deconstruct<TKey, TValue>(this KeyValuePair<TKey, TValue> pair, out TKey key, out TValue value) |
| 60 | + { |
| 61 | + key = pair.Key; |
| 62 | + value = pair.Value; |
| 63 | + } |
| 64 | +} |
| 65 | + |
56 | 66 | #endif |
0 commit comments