From a268182116716dd8354941c75f1ffddbcd32315a Mon Sep 17 00:00:00 2001 From: Aman Bhardwaj Date: Mon, 26 Aug 2019 09:16:24 -0700 Subject: [PATCH] Making ActorId string only as Actions only support strigns as Id. --- src/Microsoft.Actions.Actors/ActorId.cs | 215 ++------------------ src/Microsoft.Actions.Actors/ActorIdKind.cs | 28 --- 2 files changed, 16 insertions(+), 227 deletions(-) delete mode 100644 src/Microsoft.Actions.Actors/ActorIdKind.cs diff --git a/src/Microsoft.Actions.Actors/ActorId.cs b/src/Microsoft.Actions.Actors/ActorId.cs index 88352f4e..df022e06 100644 --- a/src/Microsoft.Actions.Actors/ActorId.cs +++ b/src/Microsoft.Actions.Actors/ActorId.cs @@ -17,31 +17,7 @@ namespace Microsoft.Actions.Actors { private static readonly Random Rand = new Random(); private static readonly object RandLock = new object(); - private readonly long longId; - private readonly Guid guidId; private readonly string stringId; - private volatile string stringRepresentation; - private volatile string traceId; - - /// - /// Initializes a new instance of the class with Id value of type . - /// - /// Value for actor id. - public ActorId(long id) - { - this.Kind = ActorIdKind.Long; - this.longId = id; - } - - /// - /// Initializes a new instance of the class with Id value of type . - /// - /// Value for actor id. - public ActorId(Guid id) - { - this.Kind = ActorIdKind.Guid; - this.guidId = id; - } /// /// Initializes a new instance of the class with Id value of type . @@ -49,22 +25,15 @@ namespace Microsoft.Actions.Actors /// Value for actor id. public ActorId(string id) { - this.Kind = ActorIdKind.String; this.stringId = id ?? throw new ArgumentNullException("id"); } /// - /// Gets the for the ActorId. - /// - /// for the ActorId. - public ActorIdKind Kind { get; } - - /// - /// Determines whether two specified actorIds have the same id and . + /// Determines whether two specified actorIds have the same id. /// /// The first actorId to compare, or null. /// The second actorId to compare, or null. - /// true if the id and is same for both objects; otherwise, false. + /// true if the id is same for both objects; otherwise, false. public static bool operator ==(ActorId x, ActorId y) { if (ReferenceEquals(x, null) && ReferenceEquals(y, null)) @@ -82,18 +51,18 @@ namespace Microsoft.Actions.Actors } /// - /// Determines whether two specified actorIds have different values for id and . + /// Determines whether two specified actorIds have different values for id./>. /// /// The first actorId to compare, or null. /// The second actorId to compare, or null. - /// true if the id or is different for both objects; otherwise, true. + /// true if the id is different for both objects; otherwise, true. public static bool operator !=(ActorId x, ActorId y) { return !(x == y); } /// - /// Create a new instance of the of kind + /// Create a new instance of the ./> /// with a random id value. /// /// A new ActorId object. @@ -106,52 +75,16 @@ namespace Microsoft.Actions.Actors Rand.NextBytes(buffer); } - return new ActorId(BitConverter.ToInt64(buffer, 0)); + return new ActorId(BitConverter.ToString(buffer, 0)); } /// - /// Gets id for ActorId whose is . - /// - /// The id value for ActorId. - /// The is not . - public long GetLongId() - { - if (this.Kind == ActorIdKind.Long) - { - return this.longId; - } - - throw new InvalidOperationException($"Invalid actor kind {this.Kind.ToString()}."); - } - - /// - /// Gets id for ActorId whose is . - /// - /// The id value for ActorId. - /// The is not . - public Guid GetGuidId() - { - if (this.Kind == ActorIdKind.Guid) - { - return this.guidId; - } - - throw new InvalidOperationException($"Invalid actor kind {this.Kind.ToString()}."); - } - - /// - /// Gets id for ActorId whose is . + /// Gets id./>. /// /// The id value for ActorId. - /// The is not . - public string GetStringId() + public string GetId() { - if (this.Kind == ActorIdKind.String) - { - return this.stringId; - } - - throw new InvalidOperationException($"Invalid actor kind {this.Kind.ToString()}."); + return this.stringId; } /// @@ -160,33 +93,7 @@ namespace Microsoft.Actions.Actors /// Returns a string that represents the current object. public override string ToString() { - if (this.stringRepresentation != null) - { - return this.stringRepresentation; - } - - var actorIdAsString = string.Empty; - switch (this.Kind) - { - case ActorIdKind.Long: - actorIdAsString = this.longId.ToString(CultureInfo.InvariantCulture); - break; - - case ActorIdKind.Guid: - actorIdAsString = this.guidId.ToString(); - break; - - case ActorIdKind.String: - actorIdAsString = this.stringId; - break; - - default: - Environment.FailFast($"The ActorIdKind value {this.Kind} is invalid"); - break; - } - - this.stringRepresentation = actorIdAsString; - return actorIdAsString; + return this.stringId; } /// @@ -195,21 +102,7 @@ namespace Microsoft.Actions.Actors /// Hash code for the current object. public override int GetHashCode() { - switch (this.Kind) - { - case ActorIdKind.Long: - return this.longId.GetHashCode(); - - case ActorIdKind.Guid: - return this.guidId.GetHashCode(); - - case ActorIdKind.String: - return this.stringId.GetHashCode(); - - default: - Environment.FailFast($"The ActorIdKind value {this.Kind} is invalid"); - return 0; // this fails the process, so unreachable code - } + return this.stringId.GetHashCode(); } /// @@ -239,8 +132,8 @@ namespace Microsoft.Actions.Actors /// Determines whether this instance and another specified object have the same value. /// /// The actorId to compare to this instance. - /// true if the and id of the other parameter is the same as the - /// and id of this instance; otherwise, false. + /// true if the id of the other parameter is the same as the + /// id of this instance; otherwise, false. /// If other is null, the method returns false. public bool Equals(ActorId other) { @@ -261,96 +154,20 @@ namespace Microsoft.Actions.Actors /// The actorId to compare with this instance. /// A 32-bit signed integer that indicates whether this instance precedes, follows, or appears /// in the same position in the sort order as the other parameter. - /// The comparison is done based on the id if both the instances have same . - /// If is different, then comparison is done based on string representation of the actor id. + /// The comparison is done based on the id if both the instances. public int CompareTo(ActorId other) { return ReferenceEquals(other, null) ? 1 : CompareContents(this, other); } - internal string GetTraceId() - { - if (this.traceId == null) - { - // Needs InvariantCulture for key. - string key; - switch (this.Kind) - { - case ActorIdKind.Long: - key = string.Format(CultureInfo.InvariantCulture, "{0}_{1}", this.Kind.ToString(), this.longId); - break; - - case ActorIdKind.Guid: - key = string.Format(CultureInfo.InvariantCulture, "{0}_{1}", this.Kind.ToString(), this.guidId); - break; - - case ActorIdKind.String: - key = string.Format( - CultureInfo.InvariantCulture, - "{0}_{1}", - this.Kind.ToString(), - this.stringId); - break; - - default: - Environment.FailFast($"The ActorIdKind value {this.Kind} is invalid"); - key = null; // unreachable - break; - } - - this.traceId = key; - } - - return this.traceId; - } - private static bool EqualsContents(ActorId x, ActorId y) { - if (x.Kind != y.Kind) - { - return false; - } - - switch (x.Kind) - { - case ActorIdKind.Long: - return (x.longId == y.longId); - - case ActorIdKind.Guid: - return (x.guidId == y.guidId); - - case ActorIdKind.String: - return string.Equals(x.stringId, y.stringId, StringComparison.OrdinalIgnoreCase); - - default: - return false; - } + return string.Equals(x.stringId, y.stringId, StringComparison.OrdinalIgnoreCase); } private static int CompareContents(ActorId x, ActorId y) { - if (x.Kind == y.Kind) - { - switch (x.Kind) - { - case ActorIdKind.Long: - return (x.longId.CompareTo(y.longId)); - - case ActorIdKind.Guid: - return (x.guidId.CompareTo(y.guidId)); - - case ActorIdKind.String: - return string.Compare(x.stringId, y.stringId, StringComparison.OrdinalIgnoreCase); - - default: - Environment.FailFast($"The ActorIdKind value {x.Kind} is invalid"); - return 0; // unreachable code - } - } - else - { - return string.Compare(x.GetTraceId(), y.GetTraceId(), StringComparison.OrdinalIgnoreCase); - } + return string.Compare(x.stringId, y.stringId, StringComparison.OrdinalIgnoreCase); } } } diff --git a/src/Microsoft.Actions.Actors/ActorIdKind.cs b/src/Microsoft.Actions.Actors/ActorIdKind.cs deleted file mode 100644 index 25313a59..00000000 --- a/src/Microsoft.Actions.Actors/ActorIdKind.cs +++ /dev/null @@ -1,28 +0,0 @@ -// ------------------------------------------------------------ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License (MIT). See License.txt in the repo root for license information. -// ------------------------------------------------------------ - -namespace Microsoft.Actions.Actors -{ - /// - /// Specifies the type of the ID value for an . - /// - public enum ActorIdKind - { - /// - /// Represents ID value of type . - /// - Long = 0, - - /// - /// Represents ID value of type . - /// - Guid = 1, - - /// - /// Represents ID value of type . - /// - String = 2, - } -}