123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- using System;
- using System.Collections.Concurrent;
- using System.Collections.Generic;
- using System.Linq;
- namespace Masuit.Tools.Systems;
- /// <summary>
- /// 支持null-key和value的字典类型
- /// </summary>
- /// <typeparam name="TKey"></typeparam>
- /// <typeparam name="TValue"></typeparam>
- public class NullableDictionary<TKey, TValue> : Dictionary<NullObject<TKey>, TValue>
- {
- public NullableDictionary()
- {
- }
- public NullableDictionary(TValue fallbackValue)
- {
- FallbackValue = fallbackValue;
- }
- public NullableDictionary(int capacity) : base(capacity)
- {
- }
- public NullableDictionary(IEqualityComparer<NullObject<TKey>> comparer) : base(comparer)
- {
- }
- public NullableDictionary(int capacity, IEqualityComparer<NullObject<TKey>> comparer) : base(capacity, comparer)
- {
- }
- public NullableDictionary(IDictionary<NullObject<TKey>, TValue> dictionary) : base(dictionary)
- {
- }
- public NullableDictionary(IDictionary<NullObject<TKey>, TValue> dictionary, IEqualityComparer<NullObject<TKey>> comparer) : base(dictionary, comparer)
- {
- }
- internal TValue FallbackValue { get; set; }
- /// <summary>
- ///
- /// </summary>
- /// <param name="key"></param>
- public new TValue this[NullObject<TKey> key]
- {
- get => TryGetValue(key, out var value) ? value : FallbackValue;
- set => base[key] = value;
- }
- public virtual TValue this[Func<KeyValuePair<TKey, TValue>, bool> condition]
- {
- get
- {
- foreach (var pair in this.Where(pair => condition(new KeyValuePair<TKey, TValue>(pair.Key.Item, pair.Value))))
- {
- return pair.Value;
- }
- return FallbackValue;
- }
- set
- {
- foreach (var pair in this.Where(pair => condition(new KeyValuePair<TKey, TValue>(pair.Key.Item, pair.Value))))
- {
- this[pair.Key] = value;
- }
- }
- }
- public virtual TValue this[Func<TKey, TValue, bool> condition]
- {
- get
- {
- foreach (var pair in this.Where(pair => condition(pair.Key.Item, pair.Value)))
- {
- return pair.Value;
- }
- return FallbackValue;
- }
- set
- {
- foreach (var pair in this.Where(pair => condition(pair.Key.Item, pair.Value)))
- {
- this[pair.Key] = value;
- }
- }
- }
- public virtual TValue this[Func<TKey, bool> condition]
- {
- get
- {
- foreach (var pair in this.Where(pair => condition(pair.Key.Item)))
- {
- return pair.Value;
- }
- return FallbackValue;
- }
- set
- {
- foreach (var pair in this.Where(pair => condition(pair.Key.Item)))
- {
- this[pair.Key] = value;
- }
- }
- }
- public virtual TValue this[Func<TValue, bool> condition]
- {
- get
- {
- foreach (var pair in this.Where(pair => condition(pair.Value)))
- {
- return pair.Value;
- }
- return FallbackValue;
- }
- set
- {
- foreach (var pair in this.Where(pair => condition(pair.Value)))
- {
- this[pair.Key] = value;
- }
- }
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="key"></param>
- public virtual TValue this[TKey key]
- {
- get => TryGetValue(new NullObject<TKey>(key), out var value) ? value : FallbackValue;
- set => base[new NullObject<TKey>(key)] = value;
- }
- public bool ContainsKey(TKey key)
- {
- return base.ContainsKey(new NullObject<TKey>(key));
- }
- public void Add(TKey key, TValue value)
- {
- base.Add(new NullObject<TKey>(key), value);
- }
- public bool Remove(TKey key)
- {
- return base.Remove(new NullObject<TKey>(key));
- }
- public bool TryGetValue(TKey key, out TValue value)
- {
- return base.TryGetValue(new NullObject<TKey>(key), out value);
- }
- /// <summary>
- /// 隐式转换
- /// </summary>
- /// <param name="dic"></param>
- public static implicit operator NullableDictionary<TKey, TValue>(Dictionary<TKey, TValue> dic)
- {
- var nullableDictionary = new NullableDictionary<TKey, TValue>();
- foreach (var p in dic)
- {
- nullableDictionary[p.Key] = p.Value;
- }
- return nullableDictionary;
- }
- /// <summary>
- /// 隐式转换
- /// </summary>
- /// <param name="dic"></param>
- public static implicit operator NullableDictionary<TKey, TValue>(ConcurrentDictionary<TKey, TValue> dic)
- {
- var nullableDictionary = new NullableDictionary<TKey, TValue>();
- foreach (var p in dic)
- {
- nullableDictionary[p.Key] = p.Value;
- }
- return nullableDictionary;
- }
- /// <summary>
- /// 隐式转换
- /// </summary>
- /// <param name="dic"></param>
- public static implicit operator Dictionary<TKey, TValue>(NullableDictionary<TKey, TValue> dic)
- {
- var newdic = new Dictionary<TKey, TValue>();
- foreach (var p in dic)
- {
- newdic[p.Key] = p.Value;
- }
- return newdic;
- }
- }
|