using Opsoft.NetCore.O3App.Shared; using System; using System.Linq; namespace Opsoft.NetCore.O3App.Domain.Models { public struct TransactionId : IEquatable { #region Private fields private const char _delimiter = '^'; private readonly int _hashCode; private string _description; private readonly byte[] _bytes; #endregion #region Public properties /// /// Gets the current runing agent's identifier. /// /// /// The current runing agent's identifier. /// public string AgentId { get; } /// /// Gets the agent start timestamp. /// /// /// The agent start timestamp. /// public long AgentStartTimestamp { get; } /// /// Gets the sequence. /// /// /// The sequence. /// public long Sequence { get; } #endregion #region Constructors /// /// Initializes a new instance of the struct. /// /// The agent identifier. /// The agent start time. /// The sequence. public TransactionId(string agentId, long agentStartTimestamp, long sequence) { AgentId = agentId ?? throw new ArgumentNullException(nameof(agentId)); AgentStartTimestamp = agentStartTimestamp; Sequence = sequence; _description = $"{AgentId}{_delimiter}{agentStartTimestamp}{_delimiter}{Sequence}"; _hashCode = _description.GetHashCode(); _bytes = _description.ToArray().Select(it => (byte)it).ToArray(); } #endregion #region Public methods /// /// Tries to parse the specified string and generate a . /// /// The transaction ID payload to parse. /// The generated /// A value indicating whether to successfully parse the payload. public static bool TryParse(string payload, out TransactionId transactionId) { Guard.ArgumentNotNullOrWhiteSpace(payload, nameof(payload)); transactionId = default(TransactionId); var split = payload.Split(_delimiter); if (split.Length != 3) { return false; } var agentId = split[0]; long timestamp; long sequence; if (!long.TryParse(split[1], out timestamp)) { return false; } if (!long.TryParse(split[2], out sequence)) { return false; } transactionId = new TransactionId(agentId, timestamp, sequence); return true; } /// /// Indicates whether the current object is equal to another object of the same type. /// /// An object to compare with this object. /// /// true if the current object is equal to the other parameter; otherwise, false. /// public bool Equals(TransactionId other) => AgentId == other.AgentId && AgentStartTimestamp == other.AgentStartTimestamp && Sequence == other.Sequence; /// /// Determines whether the specified , is equal to this instance. /// /// The to compare with this instance. /// /// true if the specified is equal to this instance; otherwise, false. /// public override bool Equals(object obj) { if (obj is TransactionId) { return Equals((TransactionId)obj); } return false; } /// /// Returns a hash code for this instance. /// /// /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. /// public override int GetHashCode() => _hashCode; /// /// Returns a that represents this instance. /// /// /// A that represents this instance. /// public override string ToString() => _description; /// /// Implements the operator ==. /// /// The transaction id1. /// The transaction id2. /// /// The result of the operator. /// public static bool operator ==(TransactionId transactionId1, TransactionId transactionId2) => transactionId1.Equals(transactionId2); /// /// Implements the operator !=. /// /// The transaction id1. /// The transaction id2. /// /// The result of the operator. /// public static bool operator !=(TransactionId transactionId1, TransactionId transactionId2) => !transactionId1.Equals(transactionId2); /// /// To the bytes. /// /// Serialize to byte array. public byte[] ToBytes() => _bytes; #endregion } }