-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTreeSave.cs
119 lines (103 loc) · 4.96 KB
/
TreeSave.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
using System.Collections.Generic;
using System.Linq;
namespace Saveable;
/// <summary>
/// Store a collections of saves (<see cref="string"/> as key, <see cref="NodeSave"/> as value).
/// </summary>
public class TreeSave
{
public static readonly TreeSave Empty = new TreeSave();
public TreeSave() => Collections = new Dictionary<string, NodeSave>();
public TreeSave(IDictionary<string, NodeSave> collections) => Collections = new Dictionary<string, NodeSave>(collections);
/// <summary>
/// Collection of <see cref="NodeSave"/>
/// </summary>
public Dictionary<string, NodeSave> Collections { get; init; }
/// <summary>
/// The number of nodes in the collection
/// </summary>
public int NumOfNodes => Collections.Count;
/// <summary>
/// The number of properties in the collection
/// </summary>
public int NumOfProperties => Collections.Sum(x => x.Value.Properties.Count);
/// <summary>
/// Gets or sets the <see cref="NodeSave"/> associated with the given key
/// </summary>
/// <param name="key">The key of the <see cref="NodeSave"/></param>
/// <returns>The <see cref="NodeSave"/> or <c>null</c> if not found</returns>
/// <exception cref="ArgumentNullException">If <paramref name="value"/> is <c>null</c></exception>
public NodeSave? this[string key]
{
get => Collections[key];
set => Collections[key] = value!;
}
/// <summary>
/// Gets the <see cref="NodeSave"/> with the given key from the collection
/// </summary>
/// <param name="key">The key to search for</param>
/// <returns>The <see cref="NodeSave"/> or <c>null</c> if not found</returns>
public NodeSave? GetCollection(string key) => Collections[key];
/// <summary>
/// Tries to get the <see cref="NodeSave"/> with the given key from the collection
/// </summary>
/// <param name="key">The key to search for</param>
/// <param name="save">The <see cref="NodeSave"/> if found, otherwise <c>null</c></param>
/// <returns><c>true</c> if the key was found, otherwise <c>false</c></returns>
public bool TryGetCollection(string key, out NodeSave? save) => Collections.TryGetValue(key, out save);
/// <summary>
/// Adds a <see cref="NodeSave"/> to the collection with the given key
/// </summary>
/// <param name="key">The key to associate the <see cref="NodeSave"/> with</param>
/// <param name="value">The <see cref="NodeSave"/> to add</param>
public void AddCollection(string key, NodeSave value) => Collections.Add(key, value);
/// <summary>
/// Tries to add a <see cref="NodeSave"/> to the collection with the given key
/// </summary>
/// <param name="key">The key to associate the <see cref="NodeSave"/> with</param>
/// <param name="value">The <see cref="NodeSave"/> to add</param>
/// <returns><c>true</c> if the key was not already in the collection, otherwise <c>false</c></returns>
public bool TryAddCollection(string key, NodeSave value) => Collections.TryAdd(key, value);
/// <summary>
/// Sets the <see cref="NodeSave"/> with the given key in the collection
/// </summary>
/// <param name="key">The key to search for</param>
/// <param name="value">The <see cref="NodeSave"/> to set</param>
public void SetCollection(string key, NodeSave value) => Collections[key] = value;
/// <summary>
/// Sets the <see cref="NodeSave"/> with the given key in the collection if the key already exists,
/// otherwise adds the <see cref="NodeSave"/> to the collection with the given key
/// </summary>
/// <param name="key">The key to search for</param>
/// <param name="value">The <see cref="NodeSave"/> to set or add</param>
public void SetOrAddCollection(string key, NodeSave value)
{
if (Collections.ContainsKey(key))
Collections[key] = value;
else
Collections.Add(key, value);
}
/// <summary>
/// Tries to set the <see cref="NodeSave"/> with the given key in the collection if the key already exists,
/// otherwise tries to add the <see cref="NodeSave"/> to the collection with the given key
/// </summary>
/// <param name="key">The key to search for</param>
/// <param name="value">The <see cref="NodeSave"/> to set or add</param>
/// <returns><c>true</c> if the key was found or added, otherwise <c>false</c></returns>
public bool TrySetOrAddCollection(string key, NodeSave value)
{
if (Collections.ContainsKey(key))
{
Collections[key] = value;
return true;
}
else
return Collections.TryAdd(key, value);
}
/// <summary>
/// Removes the <see cref="NodeSave"/> with the given key from the collection
/// </summary>
/// <param name="key">The key of the <see cref="NodeSave"/> to remove</param>
/// <returns><c>true</c> if the key was found and removed, otherwise <c>false</c></returns>
public bool RemoveCollection(string key) => Collections.Remove(key);
}