-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
299 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
using System.ComponentModel; | ||
|
||
namespace Masuit.Tools.Systems; | ||
|
||
/// <summary> | ||
/// 可空对象 | ||
/// </summary> | ||
/// <typeparam name="T"></typeparam> | ||
public readonly struct NullObject<T> | ||
{ | ||
[DefaultValue(true)] | ||
private readonly bool _isnull; | ||
|
||
private NullObject(T item, bool isnull) : this() | ||
{ | ||
_isnull = isnull; | ||
Item = item; | ||
} | ||
|
||
public NullObject(T item) : this(item, item == null) | ||
{ | ||
} | ||
|
||
public static NullObject<T> Null() | ||
{ | ||
return new NullObject<T>(); | ||
} | ||
|
||
public T Item { get; } | ||
|
||
/// <summary> | ||
/// 是否是null | ||
/// </summary> | ||
/// <returns></returns> | ||
public bool IsNull() | ||
{ | ||
return _isnull; | ||
} | ||
|
||
/// <summary> | ||
/// 隐式转换 | ||
/// </summary> | ||
/// <param name="nullObject"></param> | ||
public static implicit operator T(NullObject<T> nullObject) | ||
{ | ||
return nullObject.Item; | ||
} | ||
|
||
/// <summary> | ||
/// 隐式转换 | ||
/// </summary> | ||
/// <param name="item"></param> | ||
public static implicit operator NullObject<T>(T item) | ||
{ | ||
return new NullObject<T>(item); | ||
} | ||
|
||
public override string ToString() | ||
{ | ||
return (Item != null) ? Item.ToString() : "NULL"; | ||
} | ||
|
||
public override bool Equals(object obj) | ||
{ | ||
if (obj == null) | ||
{ | ||
return IsNull(); | ||
} | ||
|
||
if (obj is not NullObject<T> nullObject) | ||
{ | ||
return false; | ||
} | ||
|
||
if (IsNull()) | ||
{ | ||
return nullObject.IsNull(); | ||
} | ||
|
||
if (nullObject.IsNull()) | ||
{ | ||
return false; | ||
} | ||
|
||
return Item.Equals(nullObject.Item); | ||
} | ||
|
||
public override int GetHashCode() | ||
{ | ||
if (_isnull) | ||
{ | ||
return 0; | ||
} | ||
|
||
var result = Item.GetHashCode(); | ||
|
||
if (result >= 0) | ||
{ | ||
result++; | ||
} | ||
|
||
return result; | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
Masuit.Tools.Abstractions/Systems/NullableConcurrentDictionary.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
using System.Collections.Concurrent; | ||
using System.Collections.Generic; | ||
|
||
namespace Masuit.Tools.Systems; | ||
|
||
/// <summary> | ||
/// 支持null-key和value的字典类型 | ||
/// </summary> | ||
/// <typeparam name="TKey"></typeparam> | ||
/// <typeparam name="TValue"></typeparam> | ||
public class NullableConcurrentDictionary<TKey, TValue> : ConcurrentDictionary<NullObject<TKey>, TValue> | ||
{ | ||
public NullableConcurrentDictionary() : base() | ||
{ | ||
} | ||
|
||
public NullableConcurrentDictionary(int concurrencyLevel, int capacity) : base(concurrencyLevel, capacity) | ||
{ | ||
} | ||
|
||
public NullableConcurrentDictionary(IEqualityComparer<NullObject<TKey>> comparer) : base(comparer) | ||
{ | ||
} | ||
|
||
public new TValue this[NullObject<TKey> key] | ||
{ | ||
get => TryGetValue(key, out var value) ? value : default; | ||
set => base[key] = value; | ||
} | ||
|
||
/// <summary> | ||
/// 隐式转换 | ||
/// </summary> | ||
/// <param name="dic"></param> | ||
public static implicit operator NullableConcurrentDictionary<TKey, TValue>(Dictionary<TKey, TValue> dic) | ||
{ | ||
var nullableDictionary = new NullableConcurrentDictionary<TKey, TValue>(); | ||
foreach (var p in dic) | ||
{ | ||
nullableDictionary[p.Key] = p.Value; | ||
} | ||
return nullableDictionary; | ||
} | ||
|
||
/// <summary> | ||
/// 隐式转换 | ||
/// </summary> | ||
/// <param name="dic"></param> | ||
public static implicit operator NullableConcurrentDictionary<TKey, TValue>(ConcurrentDictionary<TKey, TValue> dic) | ||
{ | ||
var nullableDictionary = new NullableConcurrentDictionary<TKey, TValue>(); | ||
foreach (var p in dic) | ||
{ | ||
nullableDictionary[p.Key] = p.Value; | ||
} | ||
return nullableDictionary; | ||
} | ||
|
||
/// <summary> | ||
/// 隐式转换 | ||
/// </summary> | ||
/// <param name="dic"></param> | ||
public static implicit operator ConcurrentDictionary<TKey, TValue>(NullableConcurrentDictionary<TKey, TValue> dic) | ||
{ | ||
var newdic = new ConcurrentDictionary<TKey, TValue>(); | ||
foreach (var p in dic) | ||
{ | ||
newdic[p.Key] = p.Value; | ||
} | ||
return newdic; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
using System.Collections.Concurrent; | ||
using System.Collections.Generic; | ||
|
||
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() : base() | ||
{ | ||
} | ||
|
||
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) | ||
{ | ||
} | ||
|
||
public new TValue this[NullObject<TKey> key] | ||
{ | ||
get => TryGetValue(key, out var value) ? value : default; | ||
set => base[key] = 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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.