using Masuit.Tools.Systems;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Masuit.Tools
{
public static class IDictionaryExtensions
{
///
/// 添加或更新键值对
///
///
///
///
/// 另一个字典集
///
public static void AddOrUpdate(this IDictionary @this, IDictionary that)
{
foreach (var item in that)
{
@this[item.Key] = item.Value;
}
}
///
/// 添加或更新键值对
///
///
///
///
/// 另一个字典集
///
public static void AddOrUpdate(this NullableDictionary @this, IDictionary that)
{
foreach (var item in that)
{
@this[item.Key] = item.Value;
}
}
///
/// 添加或更新键值对
///
///
///
///
/// 另一个字典集
///
public static void AddOrUpdate(this NullableConcurrentDictionary @this, IDictionary that)
{
foreach (var item in that)
{
@this[item.Key] = item.Value;
}
}
///
/// 添加或更新键值对
///
///
///
///
/// 另一个字典集
///
public static void AddOrUpdateTo(this IDictionary @this, IDictionary that)
{
foreach (var item in @this)
{
that[item.Key] = item.Value;
}
}
///
/// 添加或更新键值对
///
///
///
///
/// 另一个字典集
///
public static void AddOrUpdateTo(this NullableDictionary @this, IDictionary that)
{
foreach (var item in @this)
{
that[item.Key] = item.Value;
}
}
///
/// 添加或更新键值对
///
///
///
///
/// 另一个字典集
///
public static void AddOrUpdateTo(this NullableConcurrentDictionary @this, IDictionary that)
{
foreach (var item in @this)
{
that[item.Key] = item.Value;
}
}
///
/// 添加或更新键值对
///
///
///
///
/// 键
/// 添加时的值
/// 更新时的操作
///
public static TValue AddOrUpdate(this IDictionary @this, TKey key, TValue addValue, Func updateValueFactory)
{
if (!@this.ContainsKey(key))
{
@this.Add(key, addValue);
}
else
{
@this[key] = updateValueFactory(key, @this[key]);
}
return @this[key];
}
///
/// 添加或更新键值对
///
///
///
///
/// 键
/// 添加时的值
/// 更新时的操作
///
public static TValue AddOrUpdate(this NullableDictionary @this, TKey key, TValue addValue, Func updateValueFactory)
{
if (!@this.ContainsKey(key))
{
@this.Add(key, addValue);
}
else
{
@this[key] = updateValueFactory(key, @this[key]);
}
return @this[key];
}
///
/// 添加或更新键值对
///
///
///
///
/// 键
/// 添加时的值
/// 更新时的操作
///
public static TValue AddOrUpdate(this NullableConcurrentDictionary @this, TKey key, TValue addValue, Func updateValueFactory)
{
if (!@this.ContainsKey(key))
{
@this.TryAdd(key, addValue);
}
else
{
@this[key] = updateValueFactory(key, @this[key]);
}
return @this[key];
}
///
/// 添加或更新键值对
///
///
///
///
/// 键
/// 添加时的值
/// 更新时的操作
///
public static async Task AddOrUpdateAsync(this IDictionary @this, TKey key, TValue addValue, Func> updateValueFactory)
{
if (!@this.ContainsKey(key))
{
@this.Add(key, addValue);
}
else
{
@this[key] = await updateValueFactory(key, @this[key]);
}
return @this[key];
}
///
/// 添加或更新键值对
///
///
///
///
/// 键
/// 添加时的值
/// 更新时的操作
///
public static async Task AddOrUpdateAsync(this NullableDictionary @this, TKey key, TValue addValue, Func> updateValueFactory)
{
if (!@this.ContainsKey(key))
{
@this.Add(key, addValue);
}
else
{
@this[key] = await updateValueFactory(key, @this[key]);
}
return @this[key];
}
///
/// 添加或更新键值对
///
///
///
///
/// 键
/// 添加时的值
/// 更新时的操作
///
public static async Task AddOrUpdateAsync(this NullableConcurrentDictionary @this, TKey key, TValue addValue, Func> updateValueFactory)
{
if (!@this.ContainsKey(key))
{
@this.TryAdd(key, addValue);
}
else
{
@this[key] = await updateValueFactory(key, @this[key]);
}
return @this[key];
}
///
/// 添加或更新键值对
///
///
///
///
/// 键
/// 添加时的值
/// 更新时的值
///
public static TValue AddOrUpdate(this IDictionary @this, TKey key, TValue addValue, TValue updateValue)
{
if (!@this.ContainsKey(key))
{
@this.Add(key, addValue);
}
else
{
@this[key] = updateValue;
}
return @this[key];
}
///
/// 添加或更新键值对
///
///
///
///
/// 键
/// 添加时的值
/// 更新时的值
///
public static TValue AddOrUpdate(this NullableDictionary @this, TKey key, TValue addValue, TValue updateValue)
{
if (!@this.ContainsKey(key))
{
@this.Add(key, addValue);
}
else
{
@this[key] = updateValue;
}
return @this[key];
}
///
/// 添加或更新键值对
///
///
///
///
/// 键
/// 添加时的值
/// 更新时的值
///
public static TValue AddOrUpdate(this NullableConcurrentDictionary @this, TKey key, TValue addValue, TValue updateValue)
{
if (!@this.ContainsKey(key))
{
@this.TryAdd(key, addValue);
}
else
{
@this[key] = updateValue;
}
return @this[key];
}
///
/// 添加或更新键值对
///
///
///
///
/// 另一个字典集
/// 更新时的操作
///
public static void AddOrUpdate(this IDictionary @this, IDictionary that, Func updateValueFactory)
{
foreach (var item in that)
{
AddOrUpdate(@this, item.Key, item.Value, updateValueFactory);
}
}
///
/// 添加或更新键值对
///
///
///
///
/// 另一个字典集
/// 更新时的操作
///
public static void AddOrUpdate(this NullableDictionary @this, IDictionary that, Func updateValueFactory)
{
foreach (var item in that)
{
AddOrUpdate(@this, item.Key, item.Value, updateValueFactory);
}
}
///
/// 添加或更新键值对
///
///
///
///
/// 另一个字典集
/// 更新时的操作
///
public static void AddOrUpdate(this NullableConcurrentDictionary @this, IDictionary that, Func updateValueFactory)
{
foreach (var item in that)
{
AddOrUpdate(@this, item.Key, item.Value, updateValueFactory);
}
}
///
/// 添加或更新键值对
///
///
///
///
/// 另一个字典集
/// 更新时的操作
///
public static Task AddOrUpdateAsync(this IDictionary @this, IDictionary that, Func> updateValueFactory)
{
return that.ForeachAsync(item => AddOrUpdateAsync(@this, item.Key, item.Value, updateValueFactory));
}
///
/// 添加或更新键值对
///
///
///
///
/// 另一个字典集
/// 更新时的操作
///
public static Task AddOrUpdateAsync(this NullableDictionary @this, IDictionary that, Func> updateValueFactory)
{
return that.ForeachAsync(item => AddOrUpdateAsync(@this, item.Key, item.Value, updateValueFactory));
}
///
/// 添加或更新键值对
///
///
///
///
/// 另一个字典集
/// 更新时的操作
///
public static Task AddOrUpdateAsync(this NullableConcurrentDictionary @this, IDictionary that, Func> updateValueFactory)
{
return that.ForeachAsync(item => AddOrUpdateAsync(@this, item.Key, item.Value, updateValueFactory));
}
///
/// 添加或更新键值对
///
///
///
///
/// 另一个字典集
/// 更新时的操作
///
public static void AddOrUpdateTo(this IDictionary @this, IDictionary that, Func updateValueFactory)
{
foreach (var item in @this)
{
AddOrUpdate(that, item.Key, item.Value, updateValueFactory);
}
}
///
/// 添加或更新键值对
///
///
///
///
/// 另一个字典集
/// 更新时的操作
///
public static void AddOrUpdateTo(this IDictionary @this, NullableDictionary that, Func updateValueFactory)
{
foreach (var item in @this)
{
AddOrUpdate(that, item.Key, item.Value, updateValueFactory);
}
}
///
/// 添加或更新键值对
///
///
///
///
/// 另一个字典集
/// 更新时的操作
///
public static void AddOrUpdateTo(this IDictionary @this, NullableConcurrentDictionary that, Func updateValueFactory)
{
foreach (var item in @this)
{
AddOrUpdate(that, item.Key, item.Value, updateValueFactory);
}
}
///
/// 添加或更新键值对
///
///
///
///
/// 另一个字典集
/// 更新时的操作
///
public static Task AddOrUpdateAsyncTo(this IDictionary @this, IDictionary that, Func> updateValueFactory)
{
return @this.ForeachAsync(item => AddOrUpdateAsync(that, item.Key, item.Value, updateValueFactory));
}
///
/// 添加或更新键值对
///
///
///
///
/// 另一个字典集
/// 更新时的操作
///
public static Task AddOrUpdateAsyncTo(this IDictionary @this, NullableDictionary that, Func> updateValueFactory)
{
return @this.ForeachAsync(item => AddOrUpdateAsync(that, item.Key, item.Value, updateValueFactory));
}
///
/// 添加或更新键值对
///
///
///
///
/// 另一个字典集
/// 更新时的操作
///
public static Task AddOrUpdateAsyncTo(this IDictionary @this, NullableConcurrentDictionary that, Func> updateValueFactory)
{
return @this.ForeachAsync(item => AddOrUpdateAsync(that, item.Key, item.Value, updateValueFactory));
}
///
/// 添加或更新键值对
///
///
///
///
/// 键
/// 添加时的操作
/// 更新时的操作
///
public static TValue AddOrUpdate(this IDictionary @this, TKey key, Func addValueFactory, Func updateValueFactory)
{
if (!@this.ContainsKey(key))
{
@this.Add(key, addValueFactory(key));
}
else
{
@this[key] = updateValueFactory(key, @this[key]);
}
return @this[key];
}
///
/// 添加或更新键值对
///
///
///
///
/// 键
/// 添加时的操作
/// 更新时的操作
///
public static TValue AddOrUpdate(this NullableDictionary @this, TKey key, Func addValueFactory, Func updateValueFactory)
{
if (!@this.ContainsKey(key))
{
@this.Add(key, addValueFactory(key));
}
else
{
@this[key] = updateValueFactory(key, @this[key]);
}
return @this[key];
}
///
/// 添加或更新键值对
///
///
///
///
/// 键
/// 添加时的操作
/// 更新时的操作
///
public static TValue AddOrUpdate(this NullableConcurrentDictionary @this, TKey key, Func addValueFactory, Func updateValueFactory)
{
if (!@this.ContainsKey(key))
{
@this.TryAdd(key, addValueFactory(key));
}
else
{
@this[key] = updateValueFactory(key, @this[key]);
}
return @this[key];
}
///
/// 添加或更新键值对
///
///
///
///
/// 键
/// 添加时的操作
/// 更新时的操作
///
public static async Task AddOrUpdateAsync(this IDictionary @this, TKey key, Func> addValueFactory, Func> updateValueFactory)
{
if (!@this.ContainsKey(key))
{
@this.Add(key, await addValueFactory(key));
}
else
{
@this[key] = await updateValueFactory(key, @this[key]);
}
return @this[key];
}
///
/// 添加或更新键值对
///
///
///
///
/// 键
/// 添加时的操作
/// 更新时的操作
///
public static async Task AddOrUpdateAsync(this NullableDictionary @this, TKey key, Func> addValueFactory, Func> updateValueFactory)
{
if (!@this.ContainsKey(key))
{
@this.Add(key, await addValueFactory(key));
}
else
{
@this[key] = await updateValueFactory(key, @this[key]);
}
return @this[key];
}
///
/// 添加或更新键值对
///
///
///
///
/// 键
/// 添加时的操作
/// 更新时的操作
///
public static async Task AddOrUpdateAsync(this NullableConcurrentDictionary @this, TKey key, Func> addValueFactory, Func> updateValueFactory)
{
if (!@this.ContainsKey(key))
{
@this.TryAdd(key, await addValueFactory(key));
}
else
{
@this[key] = await updateValueFactory(key, @this[key]);
}
return @this[key];
}
///
/// 获取或添加
///
///
///
///
///
///
///
public static TValue GetOrAdd(this IDictionary @this, TKey key, Func addValueFactory)
{
if (!@this.ContainsKey(key))
{
@this[key] = addValueFactory();
}
return @this[key];
}
///
/// 获取或添加
///
///
///
///
///
///
///
public static async Task GetOrAddAsync(this IDictionary @this, TKey key, Func> addValueFactory)
{
if (!@this.ContainsKey(key))
{
@this[key] = await addValueFactory();
}
return @this[key];
}
///
/// 获取或添加
///
///
///
///
///
///
///
public static TValue GetOrAdd(this Dictionary @this, TKey key, TValue addValue)
{
if (!@this.ContainsKey(key))
{
@this[key] = addValue;
return addValue;
}
return @this[key];
}
///
/// 遍历IEnumerable
///
///
/// 回调方法
public static void ForEach(this IDictionary dic, Action action)
{
foreach (var item in dic)
{
action(item.Key, item.Value);
}
}
///
/// 遍历IDictionary
///
///
/// 回调方法
public static Task ForEachAsync(this IDictionary dic, Func action)
{
return dic.ForeachAsync(x => action(x.Key, x.Value));
}
///
/// 安全的转换成字典集
///
///
///
///
/// 键选择器
///
public static NullableDictionary ToDictionarySafety(this IEnumerable source, Func keySelector)
{
var dic = new NullableDictionary(source.Count());
foreach (var item in source)
{
dic[keySelector(item)] = item;
}
return dic;
}
///
/// 安全的转换成字典集
///
///
///
///
/// 键选择器
/// 键未找到时的默认值
///
public static NullableDictionary ToDictionarySafety(this IEnumerable source, Func keySelector, TSource defaultValue)
{
var dic = new NullableDictionary(source.Count()) { FallbackValue = defaultValue };
foreach (var item in source)
{
dic[keySelector(item)] = item;
}
return dic;
}
///
/// 安全的转换成字典集
///
///
///
///
///
/// 键选择器
/// 值选择器
///
public static NullableDictionary ToDictionarySafety(this IEnumerable source, Func keySelector, Func elementSelector)
{
var dic = new NullableDictionary(source.Count());
foreach (var item in source)
{
dic[keySelector(item)] = elementSelector(item);
}
return dic;
}
///
/// 安全的转换成字典集
///
///
///
///
///
/// 键选择器
/// 值选择器
/// 键未找到时的默认值
///
public static NullableDictionary ToDictionarySafety(this IEnumerable source, Func keySelector, Func elementSelector, TElement defaultValue)
{
var dic = new NullableDictionary(source.Count()) { FallbackValue = defaultValue };
foreach (var item in source)
{
dic[keySelector(item)] = elementSelector(item);
}
return dic;
}
///
/// 安全的转换成字典集
///
///
///
///
///
/// 键选择器
/// 值选择器
///
public static async Task> ToDictionarySafetyAsync(this IEnumerable source, Func keySelector, Func> elementSelector)
{
var dic = new NullableDictionary(source.Count());
await source.ForeachAsync(async item => dic[keySelector(item)] = await elementSelector(item));
return dic;
}
///
/// 安全的转换成字典集
///
///
///
///
///
/// 键选择器
/// 值选择器
/// 键未找到时的默认值
///
public static async Task> ToDictionarySafetyAsync(this IEnumerable source, Func keySelector, Func> elementSelector, TElement defaultValue)
{
var dic = new NullableDictionary(source.Count()) { FallbackValue = defaultValue };
await source.ForeachAsync(async item => dic[keySelector(item)] = await elementSelector(item));
return dic;
}
///
/// 安全的转换成字典集
///
///
///
///
/// 键选择器
///
public static DisposableDictionary ToDisposableDictionarySafety(this IEnumerable source, Func keySelector) where TSource : IDisposable
{
var dic = new DisposableDictionary(source.Count());
foreach (var item in source)
{
dic[keySelector(item)] = item;
}
return dic;
}
///
/// 安全的转换成字典集
///
///
///
///
/// 键选择器
/// 键未找到时的默认值
///
public static DisposableDictionary ToDisposableDictionarySafety(this IEnumerable source, Func keySelector, TSource defaultValue) where TSource : IDisposable
{
var dic = new DisposableDictionary(source.Count()) { FallbackValue = defaultValue };
foreach (var item in source)
{
dic[keySelector(item)] = item;
}
return dic;
}
///
/// 安全的转换成字典集
///
///
///
///
///
/// 键选择器
/// 值选择器
///
public static DisposableDictionary ToDisposableDictionarySafety(this IEnumerable source, Func keySelector, Func elementSelector) where TElement : IDisposable
{
var dic = new DisposableDictionary(source.Count());
foreach (var item in source)
{
dic[keySelector(item)] = elementSelector(item);
}
return dic;
}
///
/// 安全的转换成字典集
///
///
///
///
///
/// 键选择器
/// 值选择器
/// 键未找到时的默认值
///
public static DisposableDictionary ToDisposableDictionarySafety(this IEnumerable source, Func keySelector, Func elementSelector, TElement defaultValue) where TElement : IDisposable
{
var dic = new DisposableDictionary(source.Count()) { FallbackValue = defaultValue };
foreach (var item in source)
{
dic[keySelector(item)] = elementSelector(item);
}
return dic;
}
///
/// 安全的转换成字典集
///
///
///
///
///
/// 键选择器
/// 值选择器
///
public static async Task> ToDisposableDictionarySafetyAsync(this IEnumerable source, Func keySelector, Func> elementSelector) where TElement : IDisposable
{
var dic = new DisposableDictionary(source.Count());
await source.ForeachAsync(async item => dic[keySelector(item)] = await elementSelector(item));
return dic;
}
///
/// 安全的转换成字典集
///
///
///
///
///
/// 键选择器
/// 值选择器
/// 键未找到时的默认值
///
public static async Task> ToDisposableDictionarySafetyAsync(this IEnumerable source, Func keySelector, Func> elementSelector, TElement defaultValue) where TElement : IDisposable
{
var dic = new DisposableDictionary(source.Count()) { FallbackValue = defaultValue };
await source.ForeachAsync(async item => dic[keySelector(item)] = await elementSelector(item));
return dic;
}
///
/// 安全的转换成字典集
///
///
///
///
/// 键选择器
///
public static NullableConcurrentDictionary ToConcurrentDictionary(this IEnumerable source, Func keySelector)
{
var dic = new NullableConcurrentDictionary();
foreach (var item in source)
{
dic[keySelector(item)] = item;
}
return dic;
}
///
/// 安全的转换成字典集
///
///
///
///
/// 键选择器
/// 键未找到时的默认值
///
public static NullableConcurrentDictionary ToConcurrentDictionary(this IEnumerable source, Func keySelector, TSource defaultValue)
{
var dic = new NullableConcurrentDictionary() { FallbackValue = defaultValue };
foreach (var item in source)
{
dic[keySelector(item)] = item;
}
return dic;
}
///
/// 安全的转换成字典集
///
///
///
///
///
/// 键选择器
/// 值选择器
///
public static NullableConcurrentDictionary ToConcurrentDictionary(this IEnumerable source, Func keySelector, Func elementSelector)
{
var dic = new NullableConcurrentDictionary();
foreach (var item in source)
{
dic[keySelector(item)] = elementSelector(item);
}
return dic;
}
///
/// 安全的转换成字典集
///
///
///
///
///
/// 键选择器
/// 值选择器
///
public static NullableConcurrentDictionary ToConcurrentDictionary(this IEnumerable source, Func keySelector, Func elementSelector, TElement defaultValue)
{
var dic = new NullableConcurrentDictionary() { FallbackValue = defaultValue };
foreach (var item in source)
{
dic[keySelector(item)] = elementSelector(item);
}
return dic;
}
///
/// 安全的转换成字典集
///
///
///
///
///
/// 键选择器
/// 值选择器
///
public static async Task> ToConcurrentDictionaryAsync(this IEnumerable source, Func keySelector, Func> elementSelector)
{
var dic = new ConcurrentDictionary();
await source.ForeachAsync(async item => dic[keySelector(item)] = await elementSelector(item));
return dic;
}
///
/// 安全的转换成字典集
///
///
///
///
///
/// 键选择器
/// 值选择器
/// 键未找到时的默认值
///
public static async Task> ToConcurrentDictionaryAsync(this IEnumerable source, Func keySelector, Func> elementSelector, TElement defaultValue)
{
var dic = new NullableConcurrentDictionary() { FallbackValue = defaultValue };
await source.ForeachAsync(async item => dic[keySelector(item)] = await elementSelector(item));
return dic;
}
///
/// 安全的转换成字典集
///
///
///
///
/// 键选择器
///
public static DisposableConcurrentDictionary ToDisposableConcurrentDictionary(this IEnumerable source, Func keySelector) where TSource : IDisposable
{
var dic = new DisposableConcurrentDictionary();
foreach (var item in source)
{
dic[keySelector(item)] = item;
}
return dic;
}
///
/// 安全的转换成字典集
///
///
///
///
/// 键选择器
/// 键未找到时的默认值
///
public static DisposableConcurrentDictionary ToDisposableConcurrentDictionary(this IEnumerable source, Func keySelector, TSource defaultValue) where TSource : IDisposable
{
var dic = new DisposableConcurrentDictionary() { FallbackValue = defaultValue };
foreach (var item in source)
{
dic[keySelector(item)] = item;
}
return dic;
}
///
/// 安全的转换成字典集
///
///
///
///
///
/// 键选择器
/// 值选择器
///
public static DisposableConcurrentDictionary ToDisposableConcurrentDictionary(this IEnumerable source, Func keySelector, Func elementSelector) where TElement : IDisposable
{
var dic = new DisposableConcurrentDictionary();
foreach (var item in source)
{
dic[keySelector(item)] = elementSelector(item);
}
return dic;
}
///
/// 安全的转换成字典集
///
///
///
///
///
/// 键选择器
/// 值选择器
///
public static DisposableConcurrentDictionary ToDisposableConcurrentDictionary