162 lines
6.1 KiB
C#
Executable File
162 lines
6.1 KiB
C#
Executable File
using Opsoft.NetCore.O3App.Shared;
|
|
using System;
|
|
using System.Linq;
|
|
|
|
namespace Opsoft.NetCore.O3App.Domain.Models
|
|
{
|
|
public struct TransactionId : IEquatable<TransactionId>
|
|
{
|
|
#region Private fields
|
|
private const char _delimiter = '^';
|
|
private readonly int _hashCode;
|
|
private string _description;
|
|
private readonly byte[] _bytes;
|
|
#endregion
|
|
|
|
#region Public properties
|
|
/// <summary>
|
|
/// Gets the current runing agent's identifier.
|
|
/// </summary>
|
|
/// <value>
|
|
/// The current runing agent's identifier.
|
|
/// </value>
|
|
public string AgentId { get; }
|
|
|
|
/// <summary>
|
|
/// Gets the agent start timestamp.
|
|
/// </summary>
|
|
/// <value>
|
|
/// The agent start timestamp.
|
|
/// </value>
|
|
public long AgentStartTimestamp { get; }
|
|
|
|
/// <summary>
|
|
/// Gets the sequence.
|
|
/// </summary>
|
|
/// <value>
|
|
/// The sequence.
|
|
/// </value>
|
|
public long Sequence { get; }
|
|
#endregion
|
|
|
|
#region Constructors
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="TransactionId"/> struct.
|
|
/// </summary>
|
|
/// <param name="agentId">The agent identifier.</param>
|
|
/// <param name="agentStartTimestamp">The agent start time.</param>
|
|
/// <param name="sequence">The sequence.</param>
|
|
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
|
|
/// <summary>
|
|
/// Tries to parse the specified string and generate a <see cref="TransactionId"/>.
|
|
/// </summary>
|
|
/// <param name="payload">The transaction ID payload to parse.</param>
|
|
/// <param name="transactionId">The generated <see cref="TransactionId"/></param>
|
|
/// <returns>A <see cref="bool"/> value indicating whether to successfully parse the payload.</returns>
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Indicates whether the current object is equal to another object of the same type.
|
|
/// </summary>
|
|
/// <param name="other">An object to compare with this object.</param>
|
|
/// <returns>
|
|
/// true if the current object is equal to the <paramref name="other">other</paramref> parameter; otherwise, false.
|
|
/// </returns>
|
|
public bool Equals(TransactionId other) => AgentId == other.AgentId && AgentStartTimestamp == other.AgentStartTimestamp && Sequence == other.Sequence;
|
|
|
|
/// <summary>
|
|
/// Determines whether the specified <see cref="System.Object" />, is equal to this instance.
|
|
/// </summary>
|
|
/// <param name="obj">The <see cref="System.Object" /> to compare with this instance.</param>
|
|
/// <returns>
|
|
/// <c>true</c> if the specified <see cref="System.Object" /> is equal to this instance; otherwise, <c>false</c>.
|
|
/// </returns>
|
|
public override bool Equals(object obj)
|
|
{
|
|
if (obj is TransactionId)
|
|
{
|
|
return Equals((TransactionId)obj);
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns a hash code for this instance.
|
|
/// </summary>
|
|
/// <returns>
|
|
/// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
|
|
/// </returns>
|
|
public override int GetHashCode() => _hashCode;
|
|
|
|
/// <summary>
|
|
/// Returns a <see cref="string" /> that represents this instance.
|
|
/// </summary>
|
|
/// <returns>
|
|
/// A <see cref="string" /> that represents this instance.
|
|
/// </returns>
|
|
public override string ToString() => _description;
|
|
|
|
/// <summary>
|
|
/// Implements the operator ==.
|
|
/// </summary>
|
|
/// <param name="transactionId1">The transaction id1.</param>
|
|
/// <param name="transactionId2">The transaction id2.</param>
|
|
/// <returns>
|
|
/// The result of the operator.
|
|
/// </returns>
|
|
public static bool operator ==(TransactionId transactionId1, TransactionId transactionId2) => transactionId1.Equals(transactionId2);
|
|
|
|
/// <summary>
|
|
/// Implements the operator !=.
|
|
/// </summary>
|
|
/// <param name="transactionId1">The transaction id1.</param>
|
|
/// <param name="transactionId2">The transaction id2.</param>
|
|
/// <returns>
|
|
/// The result of the operator.
|
|
/// </returns>
|
|
public static bool operator !=(TransactionId transactionId1, TransactionId transactionId2) => !transactionId1.Equals(transactionId2);
|
|
|
|
/// <summary>
|
|
/// To the bytes.
|
|
/// </summary>
|
|
/// <returns>Serialize to byte array.</returns>
|
|
public byte[] ToBytes() => _bytes;
|
|
#endregion
|
|
}
|
|
}
|