/* * DAS.Id.cs * DTM - why does this class exist? it's only encapsulating a string * Copyright © 2009 * Diversified Technical Systems, Inc. * All Rights Reserved */ using System; using DTS.Common.Utilities; namespace DTS.Common.DAS.Concepts.DAS { /// /// Representation of the DTS.Common.DAS.Concepts.DAS.Id type. /// why does this class even exist? /// public sealed class Id : Exceptional, IComparable, IEquatable { /// /// The Thing In Itself. /// private readonly string value; /// /// Initialize an instance of the DTS.Common.DAS.Concepts.DAS.Id class. /// public Id(string value) { this.value = value; } /// /// Allow DTS.Common.DAS.Concepts.DAS.Ids to be implicitly converted to strings. /// /// /// /// The id to be stringified. /// /// /// /// The value of this DTS.Common.DAS.Concepts.DAS.Id. /// /// public static implicit operator string(Id id) { return id.ToString(); } /// /// Allow strings to be implicitly converted to DTS.Slice.Control.DAS.Ids. /// /// /// /// The value to be DTS.Slice.Control.DAS.Id-ified. /// /// /// /// The equivalent to the specified string. /// /// public static implicit operator Id(string id) { return new Id(id); } public override bool Equals(object obj) { if (obj is Id that) { return Equals(that); } return string.Compare(ToString(), obj.ToString(), StringComparison.OrdinalIgnoreCase) == 0; } /// /// Determine whether this object instance and the specified object instance are equal. /// /// /// /// The to be compared to this object instance. /// /// /// /// true if this object is equal to the specified object; false otherwise. /// /// public bool Equals(Id that) { if (value == that.value) { return true; } if (null == value) { return false; } if (null == that.value) { return false; } return value.Equals(that.value, StringComparison.OrdinalIgnoreCase); } /// /// Generate a comparison value between this object and another of it's type. /// /// /// /// The to which this one is to be compared. /// /// /// /// An value expressing the "similarity" between this object /// and the one specified. /// /// public int CompareTo(Id that) { return Compare(this, that); } /// /// Generate a string representation of this class. /// /// /// /// The representation of this object. /// /// public override string ToString() { return value; } /// /// Generate a hash code for this instance. /// /// /// /// The hash code for this instance. /// /// public override int GetHashCode() { return value?.GetHashCode() ?? 0; } public static bool operator ==(Id left, Id right) { if (ReferenceEquals(left, null)) { return ReferenceEquals(right, null); } return left.Equals(right); } public static int Compare(Id left, Id right) { if (left == right) { return 0; } if (null == right) { return 1; } if (null == left) { return -1; } return string.Compare(left.ToString(), right.ToString(), StringComparison.OrdinalIgnoreCase); } public static bool operator >= (Id left, Id right) { return Compare(left, right) >= 0; } public static bool operator >(Id left, Id right) { return Compare(left, right) > 0; } public static bool operator <= (Id left, Id right) { return Compare(left, right) <= 0; } public static bool operator <(Id left, Id right) { return Compare(left, right) < 0; } public static bool operator !=(Id left, Id right) { return !(left == right); } } }