| | 1 | | namespace NLightning.Domain.ValueObjects; |
| | 2 | |
|
| | 3 | | using Interfaces; |
| | 4 | |
|
| | 5 | | /// <summary> |
| | 6 | | /// Represents a channel id. |
| | 7 | | /// </summary> |
| | 8 | | /// <remarks> |
| | 9 | | /// The channel id is a unique identifier for a channel. |
| | 10 | | /// </remarks> |
| | 11 | | public readonly struct ChannelId : IValueObject, IEquatable<ChannelId> |
| | 12 | | { |
| | 13 | | public const int LENGTH = 32; |
| | 14 | |
|
| | 15 | | private readonly byte[] _value; |
| | 16 | |
|
| 320 | 17 | | public static ChannelId Zero => new(new byte[LENGTH]); |
| | 18 | |
|
| | 19 | | public ChannelId(ReadOnlySpan<byte> value) |
| | 20 | | { |
| 532 | 21 | | if (value.Length != LENGTH) |
| | 22 | | { |
| 4 | 23 | | throw new ArgumentException($"ChannelId must be {LENGTH} bytes", nameof(value)); |
| | 24 | | } |
| | 25 | |
|
| 528 | 26 | | _value = value.ToArray(); |
| 528 | 27 | | } |
| | 28 | |
|
| | 29 | | #region Overrides |
| | 30 | | public override bool Equals(object? obj) |
| | 31 | | { |
| 0 | 32 | | if (obj is ChannelId other) |
| | 33 | | { |
| 0 | 34 | | return Equals(other); |
| | 35 | | } |
| | 36 | |
|
| 0 | 37 | | return false; |
| | 38 | | } |
| | 39 | |
|
| 184 | 40 | | public bool Equals(ChannelId other) => _value.SequenceEqual(other._value); |
| | 41 | |
|
| | 42 | | public override int GetHashCode() |
| | 43 | | { |
| 0 | 44 | | return BitConverter.ToInt32(_value, 0); |
| | 45 | | } |
| | 46 | | #endregion |
| | 47 | |
|
| | 48 | | #region Operators |
| 36 | 49 | | public static implicit operator byte[](ChannelId c) => c._value; |
| 140 | 50 | | public static implicit operator ReadOnlyMemory<byte>(ChannelId c) => c._value; |
| 8 | 51 | | public static implicit operator ChannelId(byte[] value) => new(value); |
| 0 | 52 | | public static implicit operator ChannelId(Span<byte> value) => new(value); |
| | 53 | |
|
| | 54 | | public static bool operator ==(ChannelId left, ChannelId right) |
| | 55 | | { |
| 24 | 56 | | return left.Equals(right); |
| | 57 | | } |
| | 58 | |
|
| | 59 | | public static bool operator !=(ChannelId left, ChannelId right) |
| | 60 | | { |
| 12 | 61 | | return !(left == right); |
| | 62 | | } |
| | 63 | | #endregion |
| | 64 | | } |