forked from ZeraGmbH/Blockly.Net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Entities.cs
69 lines (58 loc) · 2.22 KB
/
Entities.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
using System.Collections;
namespace BlocklyNet.Core.Model;
/// <summary>
/// Similiar to a keyed collection but with the special
/// notion of the first item for any given key.
/// </summary>
/// <typeparam name="T">Type of items managed.</typeparam>
public abstract class Entities<T> : IEnumerable<T>
{
/// <summary>
/// Retrieve the key of an item.
/// </summary>
/// <param name="item">Some item.</param>
/// <returns>The key to use for this item.</returns>
protected abstract string GetKey(T item);
private readonly List<T> _itemList = [];
private readonly Dictionary<string, T> _firstItemMap = [];
/// <summary>
/// Add a brand new item.
/// </summary>
/// <param name="item">The item to add.</param>
public void Add(T item)
{
/* Always add tp the list. */
_itemList.Add(item);
/* Remember the first item for any given key for fast access. */
_firstItemMap.TryAdd(GetKey(item), item);
}
/// <summary>
/// Retrieve the first item for a given key.
/// </summary>
/// <param name="key">Key of the item.</param>
/// <returns>The item.</returns>
/// <exception cref="KeyNotFoundException">There is no such item.</exception>
public T Get(string key) => _firstItemMap[key];
/// <summary>
/// Retrieve the first item for a given key.
/// </summary>
/// <param name="key">Key of the item.</param>
/// <returns>The item or null if the item is not known.</returns>
public T TryGet(string key) => _firstItemMap.TryGetValue(key, out var value) ? value : default!;
/// <summary>
/// Check if an item exists.
/// </summary>
/// <param name="key">The key of the item.</param>
/// <returns>Set if the item is known.</returns>
public bool Has(string key) => _firstItemMap.ContainsKey(key);
/// <summary>
/// Create an enumerator on all items.
/// </summary>
/// <returns>All items added to this manager.</returns>
public IEnumerator<T> GetEnumerator() => _itemList.GetEnumerator();
/// <summary>
/// Create an enumerator on all items.
/// </summary>
/// <returns>All items added to this manager.</returns>
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}