| | | 1 | | namespace NLightning.Domain.Protocol.Models; |
| | | 2 | | |
| | | 3 | | using Tlv; |
| | | 4 | | using ValueObjects; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// A series of (possibly zero) TLVs |
| | | 8 | | /// </summary> |
| | | 9 | | public sealed class TlvStream |
| | | 10 | | { |
| | 172 | 11 | | private readonly SortedDictionary<BigSize, BaseTlv> _tlvs = []; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Add a TLV to the stream |
| | | 15 | | /// </summary> |
| | | 16 | | /// <param name="baseTlv">The TLV to add</param> |
| | | 17 | | public void Add(BaseTlv baseTlv) |
| | | 18 | | { |
| | 116 | 19 | | if (!_tlvs.TryAdd(baseTlv.Type, baseTlv)) |
| | | 20 | | { |
| | 4 | 21 | | throw new ArgumentException($"A TLV with type {baseTlv.Type} already exists."); |
| | | 22 | | } |
| | 112 | 23 | | } |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// Add a series of TLV to the stream |
| | | 27 | | /// </summary> |
| | | 28 | | /// <param name="tlvs">The TLVs to add</param> |
| | | 29 | | public void Add(params BaseTlv?[] tlvs) |
| | | 30 | | { |
| | 320 | 31 | | foreach (var tlv in tlvs) |
| | | 32 | | { |
| | 112 | 33 | | if (tlv is null) |
| | | 34 | | continue; |
| | | 35 | | |
| | 104 | 36 | | if (!_tlvs.TryAdd(tlv.Type, tlv)) |
| | | 37 | | { |
| | 0 | 38 | | throw new ArgumentException($"A TLV with type {tlv.Type} already exists."); |
| | | 39 | | } |
| | | 40 | | } |
| | 48 | 41 | | } |
| | | 42 | | |
| | | 43 | | /// <summary> |
| | | 44 | | /// Get all TLVs in the stream |
| | | 45 | | /// </summary> |
| | | 46 | | public IEnumerable<BaseTlv> GetTlvs() |
| | | 47 | | { |
| | 40 | 48 | | return _tlvs.Values; |
| | | 49 | | } |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Get a specific TLV from the stream |
| | | 53 | | /// </summary> |
| | | 54 | | /// <param name="type">The type of TLV to get</param> |
| | | 55 | | /// <param name="tlv">The TLV to get</param> |
| | | 56 | | /// <returns></returns> |
| | | 57 | | public bool TryGetTlv(BigSize type, out BaseTlv? tlv) |
| | | 58 | | { |
| | 88 | 59 | | return _tlvs.TryGetValue(type, out tlv); |
| | | 60 | | } |
| | | 61 | | |
| | | 62 | | /// <summary> |
| | | 63 | | /// Check if any TLVs are present |
| | | 64 | | /// </summary> |
| | | 65 | | public bool Any() |
| | | 66 | | { |
| | 40 | 67 | | return _tlvs.Count != 0; |
| | | 68 | | } |
| | | 69 | | } |