| | 1 | | namespace NLightning.Domain.Models; |
| | 2 | |
|
| | 3 | | /// <summary> |
| | 4 | | /// A collection of routing information |
| | 5 | | /// </summary> |
| | 6 | | public sealed class RoutingInfoCollection : List<RoutingInfo> |
| | 7 | | { |
| | 8 | | /// <summary> |
| | 9 | | /// Te maximum amount of routing information that can be stored in a single routing information list |
| | 10 | | /// </summary> |
| | 11 | | /// <remarks> |
| | 12 | | /// The maximum length of a tagged field is 1023 * 5 bits. The routing information is 408 bits long. |
| | 13 | | /// 1023 * 5 bits = 5115 bits / 408 bits = 12.5 => round down to 12 |
| | 14 | | /// </remarks> |
| | 15 | | private const int MAX_CAPACITY = 12; |
| | 16 | |
|
| | 17 | | public event EventHandler? Changed; |
| | 18 | |
|
| | 19 | | /// <summary> |
| | 20 | | /// Adds a routing information to the collection |
| | 21 | | /// </summary> |
| | 22 | | /// <param name="routingInfo">The routing information to add</param> |
| | 23 | | /// <exception cref="InvalidOperationException">Thrown when the maximum capacity has been reached</exception> |
| | 24 | | /// <remarks> |
| | 25 | | /// The maximum capacity of the collection is 12 |
| | 26 | | /// </remarks> |
| | 27 | | public new void Add(RoutingInfo routingInfo) |
| | 28 | | { |
| 192 | 29 | | if (Count >= MAX_CAPACITY) |
| | 30 | | { |
| 4 | 31 | | throw new InvalidOperationException($"The maximum capacity of {MAX_CAPACITY} has been reached"); |
| | 32 | | } |
| | 33 | |
|
| 188 | 34 | | base.Add(routingInfo); |
| | 35 | |
|
| 188 | 36 | | OnChanged(); |
| 188 | 37 | | } |
| | 38 | |
|
| | 39 | | public new void AddRange(IEnumerable<RoutingInfo> routingInfos) |
| | 40 | | { |
| 8 | 41 | | var iEnumerable = routingInfos.ToList(); |
| 8 | 42 | | if (Count + iEnumerable.Count > MAX_CAPACITY) |
| | 43 | | { |
| 4 | 44 | | throw new InvalidOperationException($"The maximum capacity of {MAX_CAPACITY} has been reached"); |
| | 45 | | } |
| | 46 | |
|
| 4 | 47 | | base.AddRange(iEnumerable); |
| 4 | 48 | | } |
| | 49 | |
|
| | 50 | | public new bool Remove(RoutingInfo item) |
| | 51 | | { |
| 8 | 52 | | if (!base.Remove(item)) |
| | 53 | | { |
| 4 | 54 | | return false; |
| | 55 | | } |
| | 56 | |
|
| 4 | 57 | | OnChanged(); |
| 4 | 58 | | return true; |
| | 59 | | } |
| | 60 | |
|
| | 61 | | internal new void RemoveAt(int index) |
| | 62 | | { |
| 0 | 63 | | base.RemoveAt(index); |
| 0 | 64 | | OnChanged(); |
| 0 | 65 | | } |
| | 66 | |
|
| | 67 | | public new int RemoveAll(Predicate<RoutingInfo> match) |
| | 68 | | { |
| 4 | 69 | | var removed = base.RemoveAll(match); |
| 4 | 70 | | if (removed > 0) |
| | 71 | | { |
| 4 | 72 | | OnChanged(); |
| | 73 | | } |
| | 74 | |
|
| 4 | 75 | | return removed; |
| | 76 | | } |
| | 77 | |
|
| | 78 | | internal new void RemoveRange(int index, int count) |
| | 79 | | { |
| 0 | 80 | | base.RemoveRange(index, count); |
| 0 | 81 | | OnChanged(); |
| 0 | 82 | | } |
| | 83 | |
|
| | 84 | | private void OnChanged() |
| | 85 | | { |
| 196 | 86 | | Changed?.Invoke(this, EventArgs.Empty); |
| 60 | 87 | | } |
| | 88 | | } |