-
Notifications
You must be signed in to change notification settings - Fork 0
/
TypeDictionary.cs
145 lines (112 loc) · 5.54 KB
/
TypeDictionary.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// —————————————————————————————————————————————
//?
//!? 📜 TypeDictionary.cs
//!? 🖋️ Galacticai 📅 2022 - 2023
//! ⚖️ GPL-3.0-or-later
//? 🔗 Dependencies: No special dependencies
//?
// —————————————————————————————————————————————
namespace GalacticLib;
/// <summary> Dictionary of <typeparamref name="AbstractTValue" /> with a <see cref=" Type" /> as the key </summary>
public class TypeDictionary<AbstractTValue> {
#region this
private Dictionary<string, AbstractTValue> _Dictionary { get; }
public TypeDictionary(params (Type Type, AbstractTValue Value)[] items) : this() {
Set(items);
}
public TypeDictionary() {
_Dictionary = new();
}
#endregion
#region Shortcuts
public int Count => _Dictionary.Count;
public List<Type> Keys => ToTypeList();
public List<AbstractTValue> Values => ToList();
public AbstractTValue this[Type type] => _Dictionary[type.ToString()];
#endregion
#region Methods
public void TrimExcess() => _Dictionary.TrimExcess();
public virtual void Clear() => _Dictionary.Clear();
public virtual bool Remove(Type type) => _Dictionary.Remove(type.ToString());
public virtual bool Remove<ITValue>() => _Dictionary.Remove(typeof(ITValue).ToString());
#nullable enable
/// <summary> Try get a value using the full name of the type </summary>
/// <param name="typeFullName"> Example: "System.Drawing.Common.Bitmap" </param>
/// <returns></returns>
public virtual AbstractTValue? Get(string typeFullName)
=> _Dictionary.TryGetValue(typeFullName, out AbstractTValue? value)
? value
: default;
public virtual AbstractTValue? Get(Type type)
=> _Dictionary.TryGetValue(type.ToString(), out AbstractTValue? value)
? value
: default;
public virtual AbstractTValue? Get<T>()
=> _Dictionary.TryGetValue(typeof(T).ToString(), out AbstractTValue? value)
? value
: default;
#nullable restore
public virtual bool Set(params (Type Type, AbstractTValue Value)[] items)
=> items.Aggregate(true, (current, item) => current && Set(item.Type, item.Value));
public virtual bool Set<T>(AbstractTValue value, bool force = false) => Set(typeof(T), value, force);
public virtual bool Set(Type type, AbstractTValue value, bool force = false) {
if (value == null) return false;
if (ContainsKey(type) && !force) return false;
_Dictionary[type.ToString()] = value;
return true;
}
/// <summary> Check if <paramref name="type" /> exists as a key </summary>
public bool ContainsKey(Type type) => _Dictionary.ContainsKey(type.ToString());
/// <summary> Check if <typeparamref name="T" /> type exists as a key </summary>
public bool ContainsKey<T>() => _Dictionary.ContainsKey(typeof(T).ToString());
/// <summary> Check if <paramref name="value" /> exists as a value </summary>
public bool Contains(AbstractTValue value) => _Dictionary.ContainsValue(value);
#endregion
#region Conversion
public virtual Dictionary<Type, AbstractTValue>.Enumerator GetEnumerator() => ToDictionary().GetEnumerator();
/// <summary> Keys list </summary>
public virtual List<string> ToRawTypeList() => _Dictionary.Keys.ToList();
/// <summary> Keys list but the parsed into <see cref="Type" />s </summary>
public virtual List<Type> ToTypeList()
=> _Dictionary.Keys.Aggregate(
new List<Type>(),
(list, typeString) => {
Type? type = Type.GetType(typeString);
if (type != null) list.Add(type);
return list;
}
);
/// <summary>
/// <see cref="ToValueList" />
/// </summary>
public virtual List<AbstractTValue> ToList() => ToValueList();
/// <summary> Values list </summary>
public virtual List<AbstractTValue> ToValueList() => _Dictionary.Values.ToList();
/// <summary> A copy of the original dictionary </summary>
public virtual Dictionary<string, AbstractTValue> ToRawDictionary() => new(_Dictionary);
/// <summary>
/// A transformed copy of the original dictionary with the <see cref="Type" />s parsed from the
/// <see cref="string" /> keys
/// </summary>
public virtual Dictionary<Type, AbstractTValue> ToDictionary()
=> _Dictionary.Aggregate(
new Dictionary<Type, AbstractTValue>(),
(dictionary, item) => {
Type? type = Type.GetType(item.Key);
if (type != null)
dictionary.TryAdd(type, item.Value);
return dictionary;
}
);
public static implicit operator List<string>(TypeDictionary<AbstractTValue> typeDictionary)
=> typeDictionary.ToRawTypeList();
public static implicit operator List<Type>(TypeDictionary<AbstractTValue> typeDictionary)
=> typeDictionary.ToTypeList();
public static implicit operator List<AbstractTValue>(TypeDictionary<AbstractTValue> typeDictionary)
=> typeDictionary.ToValueList();
public static implicit operator Dictionary<string, AbstractTValue>(TypeDictionary<AbstractTValue> typeDictionary)
=> typeDictionary.ToRawDictionary();
public static implicit operator Dictionary<Type, AbstractTValue>(TypeDictionary<AbstractTValue> typeDictionary)
=> typeDictionary.ToDictionary();
#endregion
}