NullableConcurrentDictionary.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. namespace Masuit.Tools.Systems;
  6. /// <summary>
  7. /// 支持null-key和value的字典类型
  8. /// </summary>
  9. /// <typeparam name="TKey"></typeparam>
  10. /// <typeparam name="TValue"></typeparam>
  11. public class NullableConcurrentDictionary<TKey, TValue> : ConcurrentDictionary<NullObject<TKey>, TValue>
  12. {
  13. public NullableConcurrentDictionary() : base()
  14. {
  15. }
  16. public NullableConcurrentDictionary(TValue fallbackValue) : base()
  17. {
  18. FallbackValue = fallbackValue;
  19. }
  20. public NullableConcurrentDictionary(int concurrencyLevel, int capacity) : base(concurrencyLevel, capacity)
  21. {
  22. }
  23. public NullableConcurrentDictionary(IEqualityComparer<NullObject<TKey>> comparer) : base(comparer)
  24. {
  25. }
  26. internal TValue FallbackValue { get; set; }
  27. public new TValue this[NullObject<TKey> key]
  28. {
  29. get => TryGetValue(key, out var value) ? value : FallbackValue;
  30. set => base[key] = value;
  31. }
  32. public TValue this[Func<KeyValuePair<TKey, TValue>, bool> condition]
  33. {
  34. get
  35. {
  36. foreach (var pair in this.Where(pair => condition(new KeyValuePair<TKey, TValue>(pair.Key.Item, pair.Value))))
  37. {
  38. return pair.Value;
  39. }
  40. return FallbackValue;
  41. }
  42. set
  43. {
  44. foreach (var pair in this.Where(pair => condition(new KeyValuePair<TKey, TValue>(pair.Key.Item, pair.Value))))
  45. {
  46. this[pair.Key] = value;
  47. }
  48. }
  49. }
  50. public virtual TValue this[Func<TKey, TValue, bool> condition]
  51. {
  52. get
  53. {
  54. foreach (var pair in this.Where(pair => condition(pair.Key.Item, pair.Value)))
  55. {
  56. return pair.Value;
  57. }
  58. return FallbackValue;
  59. }
  60. set
  61. {
  62. foreach (var pair in this.Where(pair => condition(pair.Key.Item, pair.Value)))
  63. {
  64. this[pair.Key] = value;
  65. }
  66. }
  67. }
  68. public virtual TValue this[Func<TKey, bool> condition]
  69. {
  70. get
  71. {
  72. foreach (var pair in this.Where(pair => condition(pair.Key.Item)))
  73. {
  74. return pair.Value;
  75. }
  76. return FallbackValue;
  77. }
  78. set
  79. {
  80. foreach (var pair in this.Where(pair => condition(pair.Key.Item)))
  81. {
  82. this[pair.Key] = value;
  83. }
  84. }
  85. }
  86. public virtual TValue this[Func<TValue, bool> condition]
  87. {
  88. get
  89. {
  90. foreach (var pair in this.Where(pair => condition(pair.Value)))
  91. {
  92. return pair.Value;
  93. }
  94. return FallbackValue;
  95. }
  96. set
  97. {
  98. foreach (var pair in this.Where(pair => condition(pair.Value)))
  99. {
  100. this[pair.Key] = value;
  101. }
  102. }
  103. }
  104. /// <summary>
  105. ///
  106. /// </summary>
  107. /// <param name="key"></param>
  108. public virtual TValue this[TKey key]
  109. {
  110. get => base.TryGetValue(new NullObject<TKey>(key), out var value) ? value : FallbackValue;
  111. set => base[new NullObject<TKey>(key)] = value;
  112. }
  113. public bool ContainsKey(TKey key)
  114. {
  115. return base.ContainsKey(new NullObject<TKey>(key));
  116. }
  117. public bool TryAdd(TKey key, TValue value)
  118. {
  119. return base.TryAdd(new NullObject<TKey>(key), value);
  120. }
  121. public bool TryRemove(TKey key, out TValue value)
  122. {
  123. return base.TryRemove(new NullObject<TKey>(key), out value);
  124. }
  125. public bool TryUpdate(TKey key, TValue value, TValue comparisionValue)
  126. {
  127. return base.TryUpdate(new NullObject<TKey>(key), value, comparisionValue);
  128. }
  129. public bool TryGetValue(TKey key, out TValue value)
  130. {
  131. return base.TryGetValue(new NullObject<TKey>(key), out value);
  132. }
  133. /// <summary>
  134. /// 隐式转换
  135. /// </summary>
  136. /// <param name="dic"></param>
  137. public static implicit operator NullableConcurrentDictionary<TKey, TValue>(Dictionary<TKey, TValue> dic)
  138. {
  139. var nullableDictionary = new NullableConcurrentDictionary<TKey, TValue>();
  140. foreach (var p in dic)
  141. {
  142. nullableDictionary[p.Key] = p.Value;
  143. }
  144. return nullableDictionary;
  145. }
  146. /// <summary>
  147. /// 隐式转换
  148. /// </summary>
  149. /// <param name="dic"></param>
  150. public static implicit operator NullableConcurrentDictionary<TKey, TValue>(ConcurrentDictionary<TKey, TValue> dic)
  151. {
  152. var nullableDictionary = new NullableConcurrentDictionary<TKey, TValue>();
  153. foreach (var p in dic)
  154. {
  155. nullableDictionary[p.Key] = p.Value;
  156. }
  157. return nullableDictionary;
  158. }
  159. /// <summary>
  160. /// 隐式转换
  161. /// </summary>
  162. /// <param name="dic"></param>
  163. public static implicit operator ConcurrentDictionary<TKey, TValue>(NullableConcurrentDictionary<TKey, TValue> dic)
  164. {
  165. var newdic = new ConcurrentDictionary<TKey, TValue>();
  166. foreach (var p in dic)
  167. {
  168. newdic[p.Key] = p.Value;
  169. }
  170. return newdic;
  171. }
  172. }