Files
DP44/Common/DTS.Common.DAS.Concepts/.svn/pristine/0b/0b5ba0510b827b4a7158e28fd8aee91008f2b61f.svn-base
2026-04-17 14:55:32 -04:00

172 lines
5.3 KiB
Plaintext

/*
* 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
{
/// <summary>
/// Representation of the DTS.Common.DAS.Concepts.DAS.Id type.
/// why does this class even exist?
/// </summary>
public sealed class Id : Exceptional, IComparable<Id>, IEquatable<Id>
{
/// <summary>
/// The Thing In Itself.
/// </summary>
private readonly string value;
/// <summary>
/// Initialize an instance of the DTS.Common.DAS.Concepts.DAS.Id class.
/// </summary>
public Id(string value)
{
this.value = value;
}
/// <summary>
/// Allow DTS.Common.DAS.Concepts.DAS.Ids to be implicitly converted to strings.
/// </summary>
///
/// <param name="id">
/// The <see cref="Id"/> id to be stringified.
/// </param>
///
/// <returns>
/// The <see cref="string"/> value of this DTS.Common.DAS.Concepts.DAS.Id.
/// </returns>
///
public static implicit operator string(Id id)
{
return id.ToString();
}
/// <summary>
/// Allow strings to be implicitly converted to DTS.Slice.Control.DAS.Ids.
/// </summary>
///
/// <param name="id">
/// The <see cref="string"/> value to be DTS.Slice.Control.DAS.Id-ified.
/// </param>
///
/// <returns>
/// The <see cref="Id"/> equivalent to the specified string.
/// </returns>
///
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;
}
/// <summary>
/// Determine whether this object instance and the specified object instance are equal.
/// </summary>
///
/// <param name="that">
/// The <see cref="Id"/> to be compared to this object instance.
/// </param>
///
/// <returns>
/// <see cref="bool"/> true if this object is equal to the specified object; false otherwise.
/// </returns>
///
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);
}
/// <summary>
/// Generate a comparison value between this object and another of it's type.
/// </summary>
///
/// <param name="that">
/// The <see cref="Id"/> to which this one is to be compared.
/// </param>
///
/// <returns>
/// An <see cref="int"/> value expressing the "similarity" between this object
/// and the one specified.
/// </returns>
///
public int CompareTo(Id that)
{
return Compare(this, that);
}
/// <summary>
/// Generate a string representation of this class.
/// </summary>
///
/// <returns>
/// The <see cref="string"/> representation of this object.
/// </returns>
///
public override string ToString()
{
return value;
}
/// <summary>
/// Generate a hash code for this instance.
/// </summary>
///
/// <returns>
/// The <see cref="int"/> hash code for this instance.
/// </returns>
///
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);
}
}
}