|
| 1 | +// Copyright (c) MASA Stack All rights reserved. |
| 2 | +// Licensed under the MIT License. See LICENSE.txt in the project root for license information. |
| 3 | + |
| 4 | +namespace System.Collections.Concurrent; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// Extension methods for Dictionary. |
| 8 | +/// </summary> |
| 9 | +public static class DictionaryExtensions |
| 10 | +{ |
| 11 | + /// <summary> |
| 12 | + /// This method is used to try to get a value in a dictionary if it does exists. |
| 13 | + /// </summary> |
| 14 | + /// <typeparam name="T">Type of the value</typeparam> |
| 15 | + /// <param name="dictionary">The collection object</param> |
| 16 | + /// <param name="key">Key</param> |
| 17 | + /// <param name="value">Value of the key (or default value if key not exists)</param> |
| 18 | + /// <returns>True if key does exists in the dictionary</returns> |
| 19 | + internal static bool TryGetValue<T>(this IDictionary<string, object> dictionary, string key, out T value) |
| 20 | + { |
| 21 | + object valueObj; |
| 22 | + if (dictionary.TryGetValue(key, out valueObj) && valueObj is T) |
| 23 | + { |
| 24 | + value = (T)valueObj; |
| 25 | + return true; |
| 26 | + } |
| 27 | + |
| 28 | + value = default; |
| 29 | + return false; |
| 30 | + } |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// Gets a value from the dictionary with given key. Returns default value if can not find. |
| 34 | + /// </summary> |
| 35 | + /// <param name="dictionary">Dictionary to check and get</param> |
| 36 | + /// <param name="key">Key to find the value</param> |
| 37 | + /// <typeparam name="TKey">Type of the key</typeparam> |
| 38 | + /// <typeparam name="TValue">Type of the value</typeparam> |
| 39 | + /// <returns>Value if found, default if can not found.</returns> |
| 40 | + public static TValue? GetOrDefault<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key) |
| 41 | + { |
| 42 | + return dictionary.TryGetValue(key, out var obj) ? obj : default; |
| 43 | + } |
| 44 | + |
| 45 | + /// <summary> |
| 46 | + /// Gets a value from the dictionary with given key. Returns default value if can not find. |
| 47 | + /// </summary> |
| 48 | + /// <param name="dictionary">Dictionary to check and get</param> |
| 49 | + /// <param name="key">Key to find the value</param> |
| 50 | + /// <typeparam name="TKey">Type of the key</typeparam> |
| 51 | + /// <typeparam name="TValue">Type of the value</typeparam> |
| 52 | + /// <returns>Value if found, default if can not found.</returns> |
| 53 | + public static TValue? GetOrDefault<TKey, TValue>(this IReadOnlyDictionary<TKey, TValue> dictionary, TKey key) |
| 54 | + { |
| 55 | + return dictionary.TryGetValue(key, out var obj) ? obj : default; |
| 56 | + } |
| 57 | + |
| 58 | + /// <summary> |
| 59 | + /// Gets a value from the dictionary with given key. Returns default value if can not find. |
| 60 | + /// </summary> |
| 61 | + /// <param name="dictionary">Dictionary to check and get</param> |
| 62 | + /// <param name="key">Key to find the value</param> |
| 63 | + /// <typeparam name="TKey">Type of the key</typeparam> |
| 64 | + /// <typeparam name="TValue">Type of the value</typeparam> |
| 65 | + /// <returns>Value if found, default if can not found.</returns> |
| 66 | + public static TValue? GetOrDefault<TKey, TValue>(this ConcurrentDictionary<TKey, TValue> dictionary, TKey key) |
| 67 | + { |
| 68 | + return dictionary.TryGetValue(key, out var obj) ? obj : default; |
| 69 | + } |
| 70 | + |
| 71 | + /// <summary> |
| 72 | + /// Gets a value from the dictionary with given key. Returns default value if can not find. |
| 73 | + /// </summary> |
| 74 | + /// <param name="dictionary">Dictionary to check and get</param> |
| 75 | + /// <param name="key">Key to find the value</param> |
| 76 | + /// <param name="factory">A factory method used to create the value if not found in the dictionary</param> |
| 77 | + /// <typeparam name="TKey">Type of the key</typeparam> |
| 78 | + /// <typeparam name="TValue">Type of the value</typeparam> |
| 79 | + /// <returns>Value if found, default if can not found.</returns> |
| 80 | + public static TValue GetOrAdd<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, [NotNull] TKey key, Func<TKey, TValue> factory) |
| 81 | + { |
| 82 | + TValue obj; |
| 83 | + if (dictionary.TryGetValue(key, out obj)) |
| 84 | + { |
| 85 | + return obj; |
| 86 | + } |
| 87 | + |
| 88 | + return dictionary[key] = factory(key); |
| 89 | + } |
| 90 | + |
| 91 | + /// <summary> |
| 92 | + /// Gets a value from the dictionary with given key. Returns default value if can not find. |
| 93 | + /// </summary> |
| 94 | + /// <param name="dictionary">Dictionary to check and get</param> |
| 95 | + /// <param name="key">Key to find the value</param> |
| 96 | + /// <param name="factory">A factory method used to create the value if not found in the dictionary</param> |
| 97 | + /// <typeparam name="TKey">Type of the key</typeparam> |
| 98 | + /// <typeparam name="TValue">Type of the value</typeparam> |
| 99 | + /// <returns>Value if found, default if can not found.</returns> |
| 100 | + public static TValue GetOrAdd<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, [NotNull] TKey key, Func<TValue> factory) |
| 101 | + { |
| 102 | + return dictionary.GetOrAdd(key, k => factory()); |
| 103 | + } |
| 104 | + |
| 105 | + /// <summary> |
| 106 | + /// Gets a value from the concurrent dictionary with given key. Returns default value if can not find. |
| 107 | + /// </summary> |
| 108 | + /// <param name="dictionary">Concurrent dictionary to check and get</param> |
| 109 | + /// <param name="key">Key to find the value</param> |
| 110 | + /// <param name="factory">A factory method used to create the value if not found in the dictionary</param> |
| 111 | + /// <typeparam name="TKey">Type of the key</typeparam> |
| 112 | + /// <typeparam name="TValue">Type of the value</typeparam> |
| 113 | + /// <returns>Value if found, default if can not found.</returns> |
| 114 | + public static TValue GetOrAdd<TKey, TValue>(this ConcurrentDictionary<TKey, TValue> dictionary, [NotNull] TKey key, Func<TValue> factory) |
| 115 | + { |
| 116 | + return dictionary.GetOrAdd(key, k => factory()); |
| 117 | + } |
| 118 | +} |
0 commit comments