Skip to content

Commit 8373368

Browse files
committed
ID is now a record
1 parent daab2f1 commit 8373368

File tree

2 files changed

+14
-41
lines changed

2 files changed

+14
-41
lines changed

IdGen/ID.cs

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace IdGen;
55
/// <summary>
66
/// Holds information about a decoded id.
77
/// </summary>
8-
public struct Id : IEquatable<Id>
8+
public record struct Id
99
{
1010
/// <summary>
1111
/// Gets the sequence number of the id.
@@ -35,43 +35,4 @@ internal Id(int sequenceNumber, int generatorId, DateTimeOffset dateTimeOffset)
3535
GeneratorId = generatorId;
3636
DateTimeOffset = dateTimeOffset;
3737
}
38-
39-
/// <summary>
40-
/// Returns a value that indicates whether this instance is equal to a specified object.
41-
/// </summary>
42-
/// <param name="obj">The object to compare with this instance.</param>
43-
/// <returns>true if <paramref name="obj"/> is a <see cref="Id"/> that has the same value as this instance; otherwise, false.</returns>
44-
public override bool Equals(object obj)
45-
=> obj is Id id && Equals(id);
46-
47-
/// <summary>
48-
/// Returns the hash code for this instance.
49-
/// </summary>
50-
/// <returns>The hash code for this instance.</returns>
51-
public override int GetHashCode() => Tuple.Create(DateTimeOffset, GeneratorId, SequenceNumber).GetHashCode();
52-
53-
/// <summary>
54-
/// Indicates whether the values of two specified <see cref="Id"/> objects are equal.
55-
/// </summary>
56-
/// <param name="left">The first object to compare.</param>
57-
/// <param name="right">The second object to compare.</param>
58-
/// <returns>true if left and right are equal; otherwise, false.</returns>
59-
public static bool operator ==(Id left, Id right) => left.Equals(right);
60-
61-
/// <summary>
62-
/// Indicates whether the values of two specified <see cref="Id"/> objects are not equal.
63-
/// </summary>
64-
/// <param name="left">The first object to compare.</param>
65-
/// <param name="right">The second object to compare.</param>
66-
/// <returns>true if left and right are not equal; otherwise, false.</returns>
67-
public static bool operator !=(Id left, Id right) => !(left == right);
68-
69-
/// <summary>
70-
/// Returns a value indicating whether this instance and a specified <see cref="Id"/> object represent the same value.
71-
/// </summary>
72-
/// <param name="other">An <see cref="Id"/> to compare to this instance.</param>
73-
/// <returns>true if <paramref name="other"/> is equal to this instance; otherwise, false.</returns>
74-
public bool Equals(Id other) => GeneratorId == other.GeneratorId
75-
&& DateTimeOffset == other.DateTimeOffset
76-
&& SequenceNumber == other.SequenceNumber;
7738
}

IdGenTests/IDTests.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,19 @@ public void ID_Equals_OtherId()
3131
}
3232

3333
[TestMethod]
34-
public void X()
34+
public void ID_FromZeroInt_HasCorrectValue()
35+
{
36+
var g = new IdGenerator(0);
37+
var i = g.FromId(0);
38+
39+
Assert.AreEqual(0, i.SequenceNumber);
40+
Assert.AreEqual(0, i.GeneratorId);
41+
Assert.AreEqual(g.Options.TimeSource.Epoch, i.DateTimeOffset);
42+
}
43+
44+
45+
[TestMethod]
46+
public void ID_FromOneInt_HasCorrectValue()
3547
{
3648
var g = new IdGenerator(0);
3749
var i = g.FromId(1);

0 commit comments

Comments
 (0)