Created
October 9, 2017 17:19
-
-
Save onyxmaster/9c9ee7dcab78205de441189e45b01134 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Represents a 64-bit entity identifier. | |
/// </summary> | |
/// | |
/// <typeparam name="TEntity">The entity type.</typeparam> | |
/// | |
/// <threadsafety static="true" instance="true" immutable="true"/> | |
[System.ComponentModel.TypeConverter(typeof(EntityIdConverter))] | |
public struct EntityId<TEntity> : IEquatable<EntityId<TEntity>>, IComparable<EntityId<TEntity>> | |
where TEntity : class | |
{ | |
/// <summary> | |
/// The minimum value. | |
/// </summary> | |
// THREADSAFE: immutable value | |
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1000:DoNotDeclareStaticMembersOnGenericTypes", Justification = "Required for interface completeness.")] | |
public static readonly EntityId<TEntity> MinValue = new EntityId<TEntity>(long.MinValue); | |
/// <summary> | |
/// The maximum value. | |
/// </summary> | |
// THREADSAFE: immutable value | |
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1000:DoNotDeclareStaticMembersOnGenericTypes", Justification = "Required for interface completeness.")] | |
public static readonly EntityId<TEntity> MaxValue = new EntityId<TEntity>(long.MaxValue); | |
// THREADSAFE: immutable value | |
private readonly long _value; | |
/// <summary> | |
/// Initializes an instance of <see cref="EntityId{TEntity}"/> from a 64-bit integer. | |
/// </summary> | |
/// | |
/// <param name="value">The 64-bit integer.</param> | |
public EntityId(long value) | |
{ | |
_value = value; | |
} | |
/// <summary> | |
/// Initializes an instance of <see cref="EntityId{TEntity}"/> from a string. | |
/// </summary> | |
/// | |
/// <param name="s">String.</param> | |
/// | |
/// <exception cref="ArgumentOutOfRangeException">Invalid data size.</exception> | |
/// <exception cref="FormatException">Invalid string format.</exception> | |
public EntityId(string s) | |
: this(EntityId.FromString(s)) | |
{ | |
} | |
/// <summary> | |
/// Converts the entity identifier to a foreign one. | |
/// </summary> | |
/// | |
/// <typeparam name="TForeignEntity">The foreign entity type.</typeparam> | |
/// | |
/// <returns>A new foreign entity identifier.</returns> | |
public EntityId<TForeignEntity> ToForeign<TForeignEntity>() | |
where TForeignEntity : class | |
=> new EntityId<TForeignEntity>(_value); | |
/// <summary> | |
/// Represents the entity identifier as a 64-bit integer. | |
/// </summary> | |
/// | |
/// <returns>A 64-bit integer representation of an entity identifier.</returns> | |
public long ToInt64() => _value; | |
/// <summary> | |
/// Returns the "next" value (the one with the smallest difference, still greater than 1), see <c>Remarks</c>. | |
/// </summary> | |
/// | |
/// <returns>The next value.</returns> | |
/// | |
/// <remarks>The overflow is not handled specifically, the returned value is a <c>default</c>(<see cref="EntityId{TEntity}"/>).</remarks> | |
public EntityId<TEntity> NextValue() => new EntityId<TEntity>(unchecked(_value + 1)); | |
/// <summary> | |
/// Overrides <see cref="object.ToString"/> | |
/// </summary> | |
public override string ToString() => EntityId.ToString(_value); | |
/// <summary> | |
/// Overrides <see cref="object.Equals(object)"/>. | |
/// </summary> | |
public override bool Equals(object obj) => obj is EntityId<TEntity> && Equals((EntityId<TEntity>)obj); | |
/// <summary> | |
/// Overrides <see cref="object.GetHashCode"/>. | |
/// </summary> | |
public override int GetHashCode() => _value.GetHashCode(); | |
/// <summary> | |
/// Overrides <see cref="IEquatable{T}.Equals"/> where <typeparamref name="TEntity"/> is <see cref="EntityId{TEntity}"/>. | |
/// </summary> | |
public bool Equals(EntityId<TEntity> other) => _value == other._value; | |
/// <summary> | |
/// Provides an equality testing operator. | |
/// </summary> | |
public static bool operator ==(EntityId<TEntity> x, EntityId<TEntity> y) => x.Equals(y); | |
/// <summary> | |
/// Provides an inequality testing operator. | |
/// </summary> | |
public static bool operator !=(EntityId<TEntity> x, EntityId<TEntity> y) => !x.Equals(y); | |
/// <summary> | |
/// Overrides <see cref="IComparable{T}.CompareTo(T)"/> where <typeparamref name="TEntity"/> is <see cref="EntityId{TEntity}"/>. | |
/// </summary> | |
public int CompareTo(EntityId<TEntity> other) => _value.CompareTo(other._value); | |
/// <summary> | |
/// Provides a "less than" comparison operator. | |
/// </summary> | |
public static bool operator <(EntityId<TEntity> x, EntityId<TEntity> y) => x._value < y._value; | |
/// <summary> | |
/// Provides a "greater than" comparison operator. | |
/// </summary> | |
public static bool operator >(EntityId<TEntity> x, EntityId<TEntity> y) => x._value > y._value; | |
/// <summary> | |
/// Provides a "less than or equal to" comparison operator. | |
/// </summary> | |
public static bool operator <=(EntityId<TEntity> x, EntityId<TEntity> y) => x._value <= y._value; | |
/// <summary> | |
/// Provides a "greater than or equal to" comparison operator. | |
/// </summary> | |
public static bool operator >=(EntityId<TEntity> x, EntityId<TEntity> y) => x._value >= y._value; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment