|
@@ -7,6 +7,9 @@ using System.Threading.Tasks;
|
|
|
|
|
|
namespace Masuit.Tools;
|
|
|
|
|
|
+/// <summary>
|
|
|
+/// 字典扩展
|
|
|
+/// </summary>
|
|
|
public static class IDictionaryExtensions
|
|
|
{
|
|
|
/// <summary>
|
|
@@ -52,53 +55,69 @@ public static class IDictionaryExtensions
|
|
|
/// 添加或更新键值对
|
|
|
/// </summary>
|
|
|
/// <param name="this"></param>
|
|
|
- /// <param name="that">另一个字典集</param>
|
|
|
- public static void AddOrUpdateTo<TKey, TValue>(this IDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that)
|
|
|
+ /// <param name="key">键</param>
|
|
|
+ /// <param name="addValue">添加时的值</param>
|
|
|
+ /// <param name="updateValueFactory">更新时的操作</param>
|
|
|
+ public static TValue AddOrUpdate<TKey, TValue>(this IDictionary<TKey, TValue> @this, TKey key, TValue addValue, Func<TKey, TValue, TValue> updateValueFactory)
|
|
|
{
|
|
|
- foreach (var item in @this)
|
|
|
+ if ([email protected](key, addValue))
|
|
|
{
|
|
|
- that[item.Key] = item.Value;
|
|
|
+ @this[key] = updateValueFactory(key, @this[key]);
|
|
|
}
|
|
|
+
|
|
|
+ return @this[key];
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 添加或更新键值对
|
|
|
/// </summary>
|
|
|
/// <param name="this"></param>
|
|
|
- /// <param name="that">另一个字典集</param>
|
|
|
- public static void AddOrUpdateTo<TKey, TValue>(this NullableDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that)
|
|
|
+ /// <param name="key">键</param>
|
|
|
+ /// <param name="addValue">添加时的值</param>
|
|
|
+ /// <param name="updateValueFactory">更新时的操作</param>
|
|
|
+ public static TValue AddOrUpdate<TKey, TValue>(this NullableDictionary<TKey, TValue> @this, TKey key, TValue addValue, Func<TKey, TValue, TValue> updateValueFactory)
|
|
|
{
|
|
|
- foreach (var item in @this)
|
|
|
+ if ([email protected](key, addValue))
|
|
|
{
|
|
|
- that[item.Key] = item.Value;
|
|
|
+ @this[key] = updateValueFactory(key, @this[key]);
|
|
|
}
|
|
|
+
|
|
|
+ return @this[key];
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 添加或更新键值对
|
|
|
/// </summary>
|
|
|
+ /// <typeparam name="TKey"></typeparam>
|
|
|
+ /// <typeparam name="TValue"></typeparam>
|
|
|
/// <param name="this"></param>
|
|
|
- /// <param name="that">另一个字典集</param>
|
|
|
- public static void AddOrUpdateTo<TKey, TValue>(this NullableConcurrentDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that)
|
|
|
+ /// <param name="key">键</param>
|
|
|
+ /// <param name="addValue">添加时的值</param>
|
|
|
+ /// <param name="updateValueFactory">更新时的操作</param>
|
|
|
+ public static TValue AddOrUpdate<TKey, TValue>(this NullableConcurrentDictionary<TKey, TValue> @this, TKey key, TValue addValue, Func<TKey, TValue, TValue> updateValueFactory)
|
|
|
{
|
|
|
- foreach (var item in @this)
|
|
|
+ if ([email protected](key, addValue))
|
|
|
{
|
|
|
- that[item.Key] = item.Value;
|
|
|
+ @this[key] = updateValueFactory(key, @this[key]);
|
|
|
}
|
|
|
- }
|
|
|
|
|
|
+ return @this[key];
|
|
|
+ }
|
|
|
+
|
|
|
/// <summary>
|
|
|
/// 添加或更新键值对
|
|
|
/// </summary>
|
|
|
+ /// <typeparam name="TKey"></typeparam>
|
|
|
+ /// <typeparam name="TValue"></typeparam>
|
|
|
/// <param name="this"></param>
|
|
|
/// <param name="key">键</param>
|
|
|
/// <param name="addValue">添加时的值</param>
|
|
|
- /// <param name="updateValueFactory">更新时的操作</param>
|
|
|
- public static TValue AddOrUpdate<TKey, TValue>(this IDictionary<TKey, TValue> @this, TKey key, TValue addValue, Func<TKey, TValue, TValue> updateValueFactory)
|
|
|
+ /// <param name="updateValue">更新时的值</param>
|
|
|
+ public static TValue AddOrUpdate<TKey, TValue>(this IDictionary<TKey, TValue> @this, TKey key, TValue addValue, TValue updateValue)
|
|
|
{
|
|
|
if ([email protected](key, addValue))
|
|
|
{
|
|
|
- @this[key] = updateValueFactory(key, @this[key]);
|
|
|
+ @this[key] = updateValue;
|
|
|
}
|
|
|
|
|
|
return @this[key];
|
|
@@ -110,12 +129,12 @@ public static class IDictionaryExtensions
|
|
|
/// <param name="this"></param>
|
|
|
/// <param name="key">键</param>
|
|
|
/// <param name="addValue">添加时的值</param>
|
|
|
- /// <param name="updateValueFactory">更新时的操作</param>
|
|
|
- public static TValue AddOrUpdate<TKey, TValue>(this NullableDictionary<TKey, TValue> @this, TKey key, TValue addValue, Func<TKey, TValue, TValue> updateValueFactory)
|
|
|
+ /// <param name="updateValue">更新时的值</param>
|
|
|
+ public static TValue AddOrUpdate<TKey, TValue>(this NullableDictionary<TKey, TValue> @this, TKey key, TValue addValue, TValue updateValue)
|
|
|
{
|
|
|
if ([email protected](key, addValue))
|
|
|
{
|
|
|
- @this[key] = updateValueFactory(key, @this[key]);
|
|
|
+ @this[key] = updateValue;
|
|
|
}
|
|
|
|
|
|
return @this[key];
|
|
@@ -129,12 +148,12 @@ public static class IDictionaryExtensions
|
|
|
/// <param name="this"></param>
|
|
|
/// <param name="key">键</param>
|
|
|
/// <param name="addValue">添加时的值</param>
|
|
|
- /// <param name="updateValueFactory">更新时的操作</param>
|
|
|
- public static TValue AddOrUpdate<TKey, TValue>(this NullableConcurrentDictionary<TKey, TValue> @this, TKey key, TValue addValue, Func<TKey, TValue, TValue> updateValueFactory)
|
|
|
+ /// <param name="updateValue">更新时的值</param>
|
|
|
+ public static TValue AddOrUpdate<TKey, TValue>(this NullableConcurrentDictionary<TKey, TValue> @this, TKey key, TValue addValue, TValue updateValue)
|
|
|
{
|
|
|
if ([email protected](key, addValue))
|
|
|
{
|
|
|
- @this[key] = updateValueFactory(key, @this[key]);
|
|
|
+ @this[key] = updateValue;
|
|
|
}
|
|
|
|
|
|
return @this[key];
|
|
@@ -146,17 +165,14 @@ public static class IDictionaryExtensions
|
|
|
/// <typeparam name="TKey"></typeparam>
|
|
|
/// <typeparam name="TValue"></typeparam>
|
|
|
/// <param name="this"></param>
|
|
|
- /// <param name="key">键</param>
|
|
|
- /// <param name="addValue">添加时的值</param>
|
|
|
+ /// <param name="that">另一个字典集</param>
|
|
|
/// <param name="updateValueFactory">更新时的操作</param>
|
|
|
- public static async Task<TValue> AddOrUpdateAsync<TKey, TValue>(this IDictionary<TKey, TValue> @this, TKey key, TValue addValue, Func<TKey, TValue, Task<TValue>> updateValueFactory)
|
|
|
+ public static void AddOrUpdate<TKey, TValue>(this IDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that, Func<TKey, TValue, TValue> updateValueFactory)
|
|
|
{
|
|
|
- if ([email protected](key, addValue))
|
|
|
+ foreach (var item in that)
|
|
|
{
|
|
|
- @this[key] = await updateValueFactory(key, @this[key]);
|
|
|
+ AddOrUpdate(@this, item.Key, item.Value, updateValueFactory);
|
|
|
}
|
|
|
-
|
|
|
- return @this[key];
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
@@ -165,17 +181,14 @@ public static class IDictionaryExtensions
|
|
|
/// <typeparam name="TKey"></typeparam>
|
|
|
/// <typeparam name="TValue"></typeparam>
|
|
|
/// <param name="this"></param>
|
|
|
- /// <param name="key">键</param>
|
|
|
- /// <param name="addValue">添加时的值</param>
|
|
|
+ /// <param name="that">另一个字典集</param>
|
|
|
/// <param name="updateValueFactory">更新时的操作</param>
|
|
|
- public static async Task<TValue> AddOrUpdateAsync<TKey, TValue>(this NullableDictionary<TKey, TValue> @this, TKey key, TValue addValue, Func<TKey, TValue, Task<TValue>> updateValueFactory)
|
|
|
+ public static void AddOrUpdate<TKey, TValue>(this NullableDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that, Func<TKey, TValue, TValue> updateValueFactory)
|
|
|
{
|
|
|
- if ([email protected](key, addValue))
|
|
|
+ foreach (var item in that)
|
|
|
{
|
|
|
- @this[key] = await updateValueFactory(key, @this[key]);
|
|
|
+ AddOrUpdate(@this, item.Key, item.Value, updateValueFactory);
|
|
|
}
|
|
|
-
|
|
|
- return @this[key];
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
@@ -184,17 +197,14 @@ public static class IDictionaryExtensions
|
|
|
/// <typeparam name="TKey"></typeparam>
|
|
|
/// <typeparam name="TValue"></typeparam>
|
|
|
/// <param name="this"></param>
|
|
|
- /// <param name="key">键</param>
|
|
|
- /// <param name="addValue">添加时的值</param>
|
|
|
+ /// <param name="that">另一个字典集</param>
|
|
|
/// <param name="updateValueFactory">更新时的操作</param>
|
|
|
- public static async Task<TValue> AddOrUpdateAsync<TKey, TValue>(this NullableConcurrentDictionary<TKey, TValue> @this, TKey key, TValue addValue, Func<TKey, TValue, Task<TValue>> updateValueFactory)
|
|
|
+ public static void AddOrUpdate<TKey, TValue>(this NullableConcurrentDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that, Func<TKey, TValue, TValue> updateValueFactory)
|
|
|
{
|
|
|
- if ([email protected](key, addValue))
|
|
|
+ foreach (var item in that)
|
|
|
{
|
|
|
- @this[key] = await updateValueFactory(key, @this[key]);
|
|
|
+ AddOrUpdate(@this, item.Key, item.Value, updateValueFactory);
|
|
|
}
|
|
|
-
|
|
|
- return @this[key];
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
@@ -204,13 +214,13 @@ public static class IDictionaryExtensions
|
|
|
/// <typeparam name="TValue"></typeparam>
|
|
|
/// <param name="this"></param>
|
|
|
/// <param name="key">键</param>
|
|
|
- /// <param name="addValue">添加时的值</param>
|
|
|
- /// <param name="updateValue">更新时的值</param>
|
|
|
- public static TValue AddOrUpdate<TKey, TValue>(this IDictionary<TKey, TValue> @this, TKey key, TValue addValue, TValue updateValue)
|
|
|
+ /// <param name="addValueFactory">添加时的操作</param>
|
|
|
+ /// <param name="updateValueFactory">更新时的操作</param>
|
|
|
+ public static TValue AddOrUpdate<TKey, TValue>(this IDictionary<TKey, TValue> @this, TKey key, Func<TKey, TValue> addValueFactory, Func<TKey, TValue, TValue> updateValueFactory)
|
|
|
{
|
|
|
- if ([email protected](key, addValue))
|
|
|
+ if ([email protected](key, addValueFactory(key)))
|
|
|
{
|
|
|
- @this[key] = updateValue;
|
|
|
+ @this[key] = updateValueFactory(key, @this[key]);
|
|
|
}
|
|
|
|
|
|
return @this[key];
|
|
@@ -219,15 +229,17 @@ public static class IDictionaryExtensions
|
|
|
/// <summary>
|
|
|
/// 添加或更新键值对
|
|
|
/// </summary>
|
|
|
+ /// <typeparam name="TKey"></typeparam>
|
|
|
+ /// <typeparam name="TValue"></typeparam>
|
|
|
/// <param name="this"></param>
|
|
|
/// <param name="key">键</param>
|
|
|
- /// <param name="addValue">添加时的值</param>
|
|
|
- /// <param name="updateValue">更新时的值</param>
|
|
|
- public static TValue AddOrUpdate<TKey, TValue>(this NullableDictionary<TKey, TValue> @this, TKey key, TValue addValue, TValue updateValue)
|
|
|
+ /// <param name="addValueFactory">添加时的操作</param>
|
|
|
+ /// <param name="updateValueFactory">更新时的操作</param>
|
|
|
+ public static TValue AddOrUpdate<TKey, TValue>(this NullableDictionary<TKey, TValue> @this, TKey key, Func<TKey, TValue> addValueFactory, Func<TKey, TValue, TValue> updateValueFactory)
|
|
|
{
|
|
|
- if ([email protected](key, addValue))
|
|
|
+ if ([email protected](key, addValueFactory(key)))
|
|
|
{
|
|
|
- @this[key] = updateValue;
|
|
|
+ @this[key] = updateValueFactory(key, @this[key]);
|
|
|
}
|
|
|
|
|
|
return @this[key];
|
|
@@ -240,13 +252,13 @@ public static class IDictionaryExtensions
|
|
|
/// <typeparam name="TValue"></typeparam>
|
|
|
/// <param name="this"></param>
|
|
|
/// <param name="key">键</param>
|
|
|
- /// <param name="addValue">添加时的值</param>
|
|
|
- /// <param name="updateValue">更新时的值</param>
|
|
|
- public static TValue AddOrUpdate<TKey, TValue>(this NullableConcurrentDictionary<TKey, TValue> @this, TKey key, TValue addValue, TValue updateValue)
|
|
|
+ /// <param name="addValueFactory">添加时的操作</param>
|
|
|
+ /// <param name="updateValueFactory">更新时的操作</param>
|
|
|
+ public static TValue AddOrUpdate<TKey, TValue>(this NullableConcurrentDictionary<TKey, TValue> @this, TKey key, Func<TKey, TValue> addValueFactory, Func<TKey, TValue, TValue> updateValueFactory)
|
|
|
{
|
|
|
- if ([email protected](key, addValue))
|
|
|
+ if ([email protected](key, addValueFactory(key)))
|
|
|
{
|
|
|
- @this[key] = updateValue;
|
|
|
+ @this[key] = updateValueFactory(key, @this[key]);
|
|
|
}
|
|
|
|
|
|
return @this[key];
|
|
@@ -258,14 +270,17 @@ public static class IDictionaryExtensions
|
|
|
/// <typeparam name="TKey"></typeparam>
|
|
|
/// <typeparam name="TValue"></typeparam>
|
|
|
/// <param name="this"></param>
|
|
|
- /// <param name="that">另一个字典集</param>
|
|
|
+ /// <param name="key">键</param>
|
|
|
+ /// <param name="addValue">添加时的值</param>
|
|
|
/// <param name="updateValueFactory">更新时的操作</param>
|
|
|
- public static void AddOrUpdate<TKey, TValue>(this IDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that, Func<TKey, TValue, TValue> updateValueFactory)
|
|
|
+ public static async Task<TValue> AddOrUpdateAsync<TKey, TValue>(this IDictionary<TKey, TValue> @this, TKey key, TValue addValue, Func<TKey, TValue, Task<TValue>> updateValueFactory)
|
|
|
{
|
|
|
- foreach (var item in that)
|
|
|
+ if ([email protected](key, addValue))
|
|
|
{
|
|
|
- AddOrUpdate(@this, item.Key, item.Value, updateValueFactory);
|
|
|
+ @this[key] = await updateValueFactory(key, @this[key]);
|
|
|
}
|
|
|
+
|
|
|
+ return @this[key];
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
@@ -274,14 +289,17 @@ public static class IDictionaryExtensions
|
|
|
/// <typeparam name="TKey"></typeparam>
|
|
|
/// <typeparam name="TValue"></typeparam>
|
|
|
/// <param name="this"></param>
|
|
|
- /// <param name="that">另一个字典集</param>
|
|
|
+ /// <param name="key">键</param>
|
|
|
+ /// <param name="addValue">添加时的值</param>
|
|
|
/// <param name="updateValueFactory">更新时的操作</param>
|
|
|
- public static void AddOrUpdate<TKey, TValue>(this NullableDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that, Func<TKey, TValue, TValue> updateValueFactory)
|
|
|
+ public static async Task<TValue> AddOrUpdateAsync<TKey, TValue>(this NullableDictionary<TKey, TValue> @this, TKey key, TValue addValue, Func<TKey, TValue, Task<TValue>> updateValueFactory)
|
|
|
{
|
|
|
- foreach (var item in that)
|
|
|
+ if ([email protected](key, addValue))
|
|
|
{
|
|
|
- AddOrUpdate(@this, item.Key, item.Value, updateValueFactory);
|
|
|
+ @this[key] = await updateValueFactory(key, @this[key]);
|
|
|
}
|
|
|
+
|
|
|
+ return @this[key];
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
@@ -290,14 +308,17 @@ public static class IDictionaryExtensions
|
|
|
/// <typeparam name="TKey"></typeparam>
|
|
|
/// <typeparam name="TValue"></typeparam>
|
|
|
/// <param name="this"></param>
|
|
|
- /// <param name="that">另一个字典集</param>
|
|
|
+ /// <param name="key">键</param>
|
|
|
+ /// <param name="addValue">添加时的值</param>
|
|
|
/// <param name="updateValueFactory">更新时的操作</param>
|
|
|
- public static void AddOrUpdate<TKey, TValue>(this NullableConcurrentDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that, Func<TKey, TValue, TValue> updateValueFactory)
|
|
|
+ public static async Task<TValue> AddOrUpdateAsync<TKey, TValue>(this NullableConcurrentDictionary<TKey, TValue> @this, TKey key, TValue addValue, Func<TKey, TValue, Task<TValue>> updateValueFactory)
|
|
|
{
|
|
|
- foreach (var item in that)
|
|
|
+ if ([email protected](key, addValue))
|
|
|
{
|
|
|
- AddOrUpdate(@this, item.Key, item.Value, updateValueFactory);
|
|
|
+ @this[key] = await updateValueFactory(key, @this[key]);
|
|
|
}
|
|
|
+
|
|
|
+ return @this[key];
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
@@ -345,14 +366,17 @@ public static class IDictionaryExtensions
|
|
|
/// <typeparam name="TKey"></typeparam>
|
|
|
/// <typeparam name="TValue"></typeparam>
|
|
|
/// <param name="this"></param>
|
|
|
- /// <param name="that">另一个字典集</param>
|
|
|
+ /// <param name="key">键</param>
|
|
|
+ /// <param name="addValueFactory">添加时的操作</param>
|
|
|
/// <param name="updateValueFactory">更新时的操作</param>
|
|
|
- public static void AddOrUpdateTo<TKey, TValue>(this IDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that, Func<TKey, TValue, TValue> updateValueFactory)
|
|
|
+ public static async Task<TValue> AddOrUpdateAsync<TKey, TValue>(this IDictionary<TKey, TValue> @this, TKey key, Func<TKey, Task<TValue>> addValueFactory, Func<TKey, TValue, Task<TValue>> updateValueFactory)
|
|
|
{
|
|
|
- foreach (var item in @this)
|
|
|
+ if ([email protected](key, await addValueFactory(key)))
|
|
|
{
|
|
|
- AddOrUpdate(that, item.Key, item.Value, updateValueFactory);
|
|
|
+ @this[key] = await updateValueFactory(key, @this[key]);
|
|
|
}
|
|
|
+
|
|
|
+ return @this[key];
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
@@ -361,14 +385,17 @@ public static class IDictionaryExtensions
|
|
|
/// <typeparam name="TKey"></typeparam>
|
|
|
/// <typeparam name="TValue"></typeparam>
|
|
|
/// <param name="this"></param>
|
|
|
- /// <param name="that">另一个字典集</param>
|
|
|
+ /// <param name="key">键</param>
|
|
|
+ /// <param name="addValueFactory">添加时的操作</param>
|
|
|
/// <param name="updateValueFactory">更新时的操作</param>
|
|
|
- public static void AddOrUpdateTo<TKey, TValue>(this IDictionary<TKey, TValue> @this, NullableDictionary<TKey, TValue> that, Func<TKey, TValue, TValue> updateValueFactory)
|
|
|
+ public static async Task<TValue> AddOrUpdateAsync<TKey, TValue>(this NullableDictionary<TKey, TValue> @this, TKey key, Func<TKey, Task<TValue>> addValueFactory, Func<TKey, TValue, Task<TValue>> updateValueFactory)
|
|
|
{
|
|
|
- foreach (var item in @this)
|
|
|
+ if ([email protected](key, await addValueFactory(key)))
|
|
|
{
|
|
|
- AddOrUpdate(that, item.Key, item.Value, updateValueFactory);
|
|
|
+ @this[key] = await updateValueFactory(key, @this[key]);
|
|
|
}
|
|
|
+
|
|
|
+ return @this[key];
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
@@ -377,53 +404,56 @@ public static class IDictionaryExtensions
|
|
|
/// <typeparam name="TKey"></typeparam>
|
|
|
/// <typeparam name="TValue"></typeparam>
|
|
|
/// <param name="this"></param>
|
|
|
- /// <param name="that">另一个字典集</param>
|
|
|
+ /// <param name="key">键</param>
|
|
|
+ /// <param name="addValueFactory">添加时的操作</param>
|
|
|
/// <param name="updateValueFactory">更新时的操作</param>
|
|
|
- public static void AddOrUpdateTo<TKey, TValue>(this IDictionary<TKey, TValue> @this, NullableConcurrentDictionary<TKey, TValue> that, Func<TKey, TValue, TValue> updateValueFactory)
|
|
|
+ public static async Task<TValue> AddOrUpdateAsync<TKey, TValue>(this NullableConcurrentDictionary<TKey, TValue> @this, TKey key, Func<TKey, Task<TValue>> addValueFactory, Func<TKey, TValue, Task<TValue>> updateValueFactory)
|
|
|
{
|
|
|
- foreach (var item in @this)
|
|
|
+ if ([email protected](key, await addValueFactory(key)))
|
|
|
{
|
|
|
- AddOrUpdate(that, item.Key, item.Value, updateValueFactory);
|
|
|
+ @this[key] = await updateValueFactory(key, @this[key]);
|
|
|
}
|
|
|
+
|
|
|
+ return @this[key];
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 添加或更新键值对
|
|
|
/// </summary>
|
|
|
- /// <typeparam name="TKey"></typeparam>
|
|
|
- /// <typeparam name="TValue"></typeparam>
|
|
|
/// <param name="this"></param>
|
|
|
/// <param name="that">另一个字典集</param>
|
|
|
- /// <param name="updateValueFactory">更新时的操作</param>
|
|
|
- public static Task AddOrUpdateAsyncTo<TKey, TValue>(this IDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that, Func<TKey, TValue, Task<TValue>> updateValueFactory)
|
|
|
+ public static void AddOrUpdateTo<TKey, TValue>(this IDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that)
|
|
|
{
|
|
|
- return @this.ForeachAsync(item => AddOrUpdateAsync(that, item.Key, item.Value, updateValueFactory));
|
|
|
+ foreach (var item in @this)
|
|
|
+ {
|
|
|
+ that[item.Key] = item.Value;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 添加或更新键值对
|
|
|
/// </summary>
|
|
|
- /// <typeparam name="TKey"></typeparam>
|
|
|
- /// <typeparam name="TValue"></typeparam>
|
|
|
/// <param name="this"></param>
|
|
|
/// <param name="that">另一个字典集</param>
|
|
|
- /// <param name="updateValueFactory">更新时的操作</param>
|
|
|
- public static Task AddOrUpdateAsyncTo<TKey, TValue>(this IDictionary<TKey, TValue> @this, NullableDictionary<TKey, TValue> that, Func<TKey, TValue, Task<TValue>> updateValueFactory)
|
|
|
+ public static void AddOrUpdateTo<TKey, TValue>(this NullableDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that)
|
|
|
{
|
|
|
- return @this.ForeachAsync(item => AddOrUpdateAsync(that, item.Key, item.Value, updateValueFactory));
|
|
|
+ foreach (var item in @this)
|
|
|
+ {
|
|
|
+ that[item.Key] = item.Value;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 添加或更新键值对
|
|
|
/// </summary>
|
|
|
- /// <typeparam name="TKey"></typeparam>
|
|
|
- /// <typeparam name="TValue"></typeparam>
|
|
|
/// <param name="this"></param>
|
|
|
/// <param name="that">另一个字典集</param>
|
|
|
- /// <param name="updateValueFactory">更新时的操作</param>
|
|
|
- public static Task AddOrUpdateAsyncTo<TKey, TValue>(this IDictionary<TKey, TValue> @this, NullableConcurrentDictionary<TKey, TValue> that, Func<TKey, TValue, Task<TValue>> updateValueFactory)
|
|
|
+ public static void AddOrUpdateTo<TKey, TValue>(this NullableConcurrentDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that)
|
|
|
{
|
|
|
- return @this.ForeachAsync(item => AddOrUpdateAsync(that, item.Key, item.Value, updateValueFactory));
|
|
|
+ foreach (var item in @this)
|
|
|
+ {
|
|
|
+ that[item.Key] = item.Value;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
@@ -432,17 +462,14 @@ public static class IDictionaryExtensions
|
|
|
/// <typeparam name="TKey"></typeparam>
|
|
|
/// <typeparam name="TValue"></typeparam>
|
|
|
/// <param name="this"></param>
|
|
|
- /// <param name="key">键</param>
|
|
|
- /// <param name="addValueFactory">添加时的操作</param>
|
|
|
+ /// <param name="that">另一个字典集</param>
|
|
|
/// <param name="updateValueFactory">更新时的操作</param>
|
|
|
- public static TValue AddOrUpdate<TKey, TValue>(this IDictionary<TKey, TValue> @this, TKey key, Func<TKey, TValue> addValueFactory, Func<TKey, TValue, TValue> updateValueFactory)
|
|
|
+ public static void AddOrUpdateTo<TKey, TValue>(this IDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that, Func<TKey, TValue, TValue> updateValueFactory)
|
|
|
{
|
|
|
- if ([email protected](key, addValueFactory(key)))
|
|
|
+ foreach (var item in @this)
|
|
|
{
|
|
|
- @this[key] = updateValueFactory(key, @this[key]);
|
|
|
+ AddOrUpdate(that, item.Key, item.Value, updateValueFactory);
|
|
|
}
|
|
|
-
|
|
|
- return @this[key];
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
@@ -451,17 +478,14 @@ public static class IDictionaryExtensions
|
|
|
/// <typeparam name="TKey"></typeparam>
|
|
|
/// <typeparam name="TValue"></typeparam>
|
|
|
/// <param name="this"></param>
|
|
|
- /// <param name="key">键</param>
|
|
|
- /// <param name="addValueFactory">添加时的操作</param>
|
|
|
+ /// <param name="that">另一个字典集</param>
|
|
|
/// <param name="updateValueFactory">更新时的操作</param>
|
|
|
- public static TValue AddOrUpdate<TKey, TValue>(this NullableDictionary<TKey, TValue> @this, TKey key, Func<TKey, TValue> addValueFactory, Func<TKey, TValue, TValue> updateValueFactory)
|
|
|
+ public static void AddOrUpdateTo<TKey, TValue>(this IDictionary<TKey, TValue> @this, NullableDictionary<TKey, TValue> that, Func<TKey, TValue, TValue> updateValueFactory)
|
|
|
{
|
|
|
- if ([email protected](key, addValueFactory(key)))
|
|
|
+ foreach (var item in @this)
|
|
|
{
|
|
|
- @this[key] = updateValueFactory(key, @this[key]);
|
|
|
+ AddOrUpdate(that, item.Key, item.Value, updateValueFactory);
|
|
|
}
|
|
|
-
|
|
|
- return @this[key];
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
@@ -470,17 +494,14 @@ public static class IDictionaryExtensions
|
|
|
/// <typeparam name="TKey"></typeparam>
|
|
|
/// <typeparam name="TValue"></typeparam>
|
|
|
/// <param name="this"></param>
|
|
|
- /// <param name="key">键</param>
|
|
|
- /// <param name="addValueFactory">添加时的操作</param>
|
|
|
+ /// <param name="that">另一个字典集</param>
|
|
|
/// <param name="updateValueFactory">更新时的操作</param>
|
|
|
- public static TValue AddOrUpdate<TKey, TValue>(this NullableConcurrentDictionary<TKey, TValue> @this, TKey key, Func<TKey, TValue> addValueFactory, Func<TKey, TValue, TValue> updateValueFactory)
|
|
|
+ public static void AddOrUpdateTo<TKey, TValue>(this IDictionary<TKey, TValue> @this, NullableConcurrentDictionary<TKey, TValue> that, Func<TKey, TValue, TValue> updateValueFactory)
|
|
|
{
|
|
|
- if ([email protected](key, addValueFactory(key)))
|
|
|
+ foreach (var item in @this)
|
|
|
{
|
|
|
- @this[key] = updateValueFactory(key, @this[key]);
|
|
|
+ AddOrUpdate(that, item.Key, item.Value, updateValueFactory);
|
|
|
}
|
|
|
-
|
|
|
- return @this[key];
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
@@ -489,17 +510,11 @@ public static class IDictionaryExtensions
|
|
|
/// <typeparam name="TKey"></typeparam>
|
|
|
/// <typeparam name="TValue"></typeparam>
|
|
|
/// <param name="this"></param>
|
|
|
- /// <param name="key">键</param>
|
|
|
- /// <param name="addValueFactory">添加时的操作</param>
|
|
|
+ /// <param name="that">另一个字典集</param>
|
|
|
/// <param name="updateValueFactory">更新时的操作</param>
|
|
|
- public static async Task<TValue> AddOrUpdateAsync<TKey, TValue>(this IDictionary<TKey, TValue> @this, TKey key, Func<TKey, Task<TValue>> addValueFactory, Func<TKey, TValue, Task<TValue>> updateValueFactory)
|
|
|
+ public static Task AddOrUpdateToAsync<TKey, TValue>(this IDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that, Func<TKey, TValue, Task<TValue>> updateValueFactory)
|
|
|
{
|
|
|
- if ([email protected](key, await addValueFactory(key)))
|
|
|
- {
|
|
|
- @this[key] = await updateValueFactory(key, @this[key]);
|
|
|
- }
|
|
|
-
|
|
|
- return @this[key];
|
|
|
+ return @this.ForeachAsync(item => AddOrUpdateAsync(that, item.Key, item.Value, updateValueFactory));
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
@@ -508,17 +523,11 @@ public static class IDictionaryExtensions
|
|
|
/// <typeparam name="TKey"></typeparam>
|
|
|
/// <typeparam name="TValue"></typeparam>
|
|
|
/// <param name="this"></param>
|
|
|
- /// <param name="key">键</param>
|
|
|
- /// <param name="addValueFactory">添加时的操作</param>
|
|
|
+ /// <param name="that">另一个字典集</param>
|
|
|
/// <param name="updateValueFactory">更新时的操作</param>
|
|
|
- public static async Task<TValue> AddOrUpdateAsync<TKey, TValue>(this NullableDictionary<TKey, TValue> @this, TKey key, Func<TKey, Task<TValue>> addValueFactory, Func<TKey, TValue, Task<TValue>> updateValueFactory)
|
|
|
+ public static Task AddOrUpdateToAsync<TKey, TValue>(this IDictionary<TKey, TValue> @this, NullableDictionary<TKey, TValue> that, Func<TKey, TValue, Task<TValue>> updateValueFactory)
|
|
|
{
|
|
|
- if ([email protected](key, await addValueFactory(key)))
|
|
|
- {
|
|
|
- @this[key] = await updateValueFactory(key, @this[key]);
|
|
|
- }
|
|
|
-
|
|
|
- return @this[key];
|
|
|
+ return @this.ForeachAsync(item => AddOrUpdateAsync(that, item.Key, item.Value, updateValueFactory));
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
@@ -527,17 +536,11 @@ public static class IDictionaryExtensions
|
|
|
/// <typeparam name="TKey"></typeparam>
|
|
|
/// <typeparam name="TValue"></typeparam>
|
|
|
/// <param name="this"></param>
|
|
|
- /// <param name="key">键</param>
|
|
|
- /// <param name="addValueFactory">添加时的操作</param>
|
|
|
+ /// <param name="that">另一个字典集</param>
|
|
|
/// <param name="updateValueFactory">更新时的操作</param>
|
|
|
- public static async Task<TValue> AddOrUpdateAsync<TKey, TValue>(this NullableConcurrentDictionary<TKey, TValue> @this, TKey key, Func<TKey, Task<TValue>> addValueFactory, Func<TKey, TValue, Task<TValue>> updateValueFactory)
|
|
|
+ public static Task AddOrUpdateToAsync<TKey, TValue>(this IDictionary<TKey, TValue> @this, NullableConcurrentDictionary<TKey, TValue> that, Func<TKey, TValue, Task<TValue>> updateValueFactory)
|
|
|
{
|
|
|
- if ([email protected](key, await addValueFactory(key)))
|
|
|
- {
|
|
|
- @this[key] = await updateValueFactory(key, @this[key]);
|
|
|
- }
|
|
|
-
|
|
|
- return @this[key];
|
|
|
+ return @this.ForeachAsync(item => AddOrUpdateAsync(that, item.Key, item.Value, updateValueFactory));
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
@@ -565,15 +568,10 @@ public static class IDictionaryExtensions
|
|
|
/// <typeparam name="TValue"></typeparam>
|
|
|
/// <param name="this"></param>
|
|
|
/// <param name="key"></param>
|
|
|
- /// <param name="addValueFactory"></param>
|
|
|
- public static async Task<TValue> GetOrAddAsync<TKey, TValue>(this IDictionary<TKey, TValue> @this, TKey key, Func<Task<TValue>> addValueFactory)
|
|
|
+ /// <param name="addValue"></param>
|
|
|
+ public static TValue GetOrAdd<TKey, TValue>(this Dictionary<TKey, TValue> @this, TKey key, TValue addValue)
|
|
|
{
|
|
|
- if ([email protected](key))
|
|
|
- {
|
|
|
- @this[key] = await addValueFactory();
|
|
|
- }
|
|
|
-
|
|
|
- return @this[key];
|
|
|
+ return @this.TryAdd(key, addValue) ? addValue : @this[key];
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
@@ -583,15 +581,18 @@ public static class IDictionaryExtensions
|
|
|
/// <typeparam name="TValue"></typeparam>
|
|
|
/// <param name="this"></param>
|
|
|
/// <param name="key"></param>
|
|
|
- /// <param name="addValue"></param>
|
|
|
- public static TValue GetOrAdd<TKey, TValue>(this Dictionary<TKey, TValue> @this, TKey key, TValue addValue)
|
|
|
+ /// <param name="addValueFactory"></param>
|
|
|
+ public static async Task<TValue> GetOrAddAsync<TKey, TValue>(this IDictionary<TKey, TValue> @this, TKey key, Func<Task<TValue>> addValueFactory)
|
|
|
{
|
|
|
- return @this.TryAdd(key, addValue) ? addValue : @this[key];
|
|
|
- }
|
|
|
+ if ([email protected](key))
|
|
|
+ {
|
|
|
+ @this[key] = await addValueFactory();
|
|
|
+ }
|
|
|
|
|
|
-#if !NETSTANDARD2_1_OR_GREATER
|
|
|
+ return @this[key];
|
|
|
+ }
|
|
|
|
|
|
- public static bool TryAdd<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue value) where TKey : notnull
|
|
|
+ private static bool TryAdd<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue value) where TKey : notnull
|
|
|
{
|
|
|
if (dictionary == null)
|
|
|
throw new ArgumentNullException(nameof(dictionary));
|
|
@@ -601,8 +602,6 @@ public static class IDictionaryExtensions
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
-#endif
|
|
|
-
|
|
|
/// <summary>
|
|
|
/// 遍历IEnumerable
|
|
|
/// </summary>
|