Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion src/Confluent.Kafka/Offset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace Confluent.Kafka
/// its purpose is to add some syntactical sugar
/// related to special values.
/// </remarks>
public struct Offset : IEquatable<Offset>
public struct Offset : IEquatable<Offset>, IComparable<Offset>
{
private const long RD_KAFKA_OFFSET_BEGINNING = -2;
private const long RD_KAFKA_OFFSET_END = -1;
Expand Down Expand Up @@ -287,5 +287,31 @@ public override string ToString()
return Value.ToString();
}
}

/// <summary>Compares the current instance with another object of the same type and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other object.</summary>
/// <param name="other">An object to compare with this instance.</param>
/// <returns>
/// <para>A value that indicates the relative order of the objects being compared. The return value has these meanings:</para>
/// <list type="table">
/// <listheader>
/// <term>Value</term>
/// <description>Meaning</description>
/// </listheader>
/// <item>
/// <term>Less than zero</term>
/// <description>This instance precedes <paramref name="other" /> in the sort order.</description>
/// </item>
/// <item>
/// <term>Zero</term>
/// <description>This instance occurs in the same position in the sort order as <paramref name="other" />.</description>
/// </item>
/// <item>
/// <term>Greater than zero</term>
/// <description>This instance follows <paramref name="other" /> in the sort order.</description>
/// </item>
/// </list>
/// </returns>
public int CompareTo(Offset other)
=> Value.CompareTo(other.Value);
}
}
28 changes: 27 additions & 1 deletion src/Confluent.Kafka/Partition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace Confluent.Kafka
/// its purpose is to add some syntactical sugar
/// related to special values.
/// </remarks>
public struct Partition : IEquatable<Partition>
public struct Partition : IEquatable<Partition>, IComparable<Partition>
{
private const int RD_KAFKA_PARTITION_UA = -1;

Expand Down Expand Up @@ -224,5 +224,31 @@ public override string ToString()
return $"[{Value}]";
}
}

/// <summary>Compares the current instance with another object of the same type and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other object.</summary>
/// <param name="other">An object to compare with this instance.</param>
/// <returns>
/// <para>A value that indicates the relative order of the objects being compared. The return value has these meanings:</para>
/// <list type="table">
/// <listheader>
/// <term>Value</term>
/// <description>Meaning</description>
/// </listheader>
/// <item>
/// <term>Less than zero</term>
/// <description>This instance precedes <paramref name="other" /> in the sort order.</description>
/// </item>
/// <item>
/// <term>Zero</term>
/// <description>This instance occurs in the same position in the sort order as <paramref name="other" />.</description>
/// </item>
/// <item>
/// <term>Greater than zero</term>
/// <description>This instance follows <paramref name="other" /> in the sort order.</description>
/// </item>
/// </list>
/// </returns>
public int CompareTo(Partition other)
=> Value.CompareTo(other.Value);
}
}
11 changes: 11 additions & 0 deletions test/Confluent.Kafka.UnitTests/Offset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,17 @@ public void Inequality()
Assert.True(a >= a2);
}

[Fact]
public void CompareTo()
{
Offset a = new Offset(42);
Offset a2 = new Offset(42);
Offset b = new Offset(37);
Assert.True(a.CompareTo(b) > 0);
Assert.True(b.CompareTo(a) < 0);
Assert.True(a.CompareTo(a2) == 0);
}

[Fact]
public void Addition_Int()
{
Expand Down
11 changes: 11 additions & 0 deletions test/Confluent.Kafka.UnitTests/Partition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,17 @@ public void Inequality()
Assert.True(a >= a2);
}

[Fact]
public void CompareTo()
{
Partition a = new Partition(42);
Partition a2 = new Partition(42);
Partition b = new Partition(37);
Assert.True(a.CompareTo(b) > 0);
Assert.True(b.CompareTo(a) < 0);
Assert.True(a.CompareTo(a2) == 0);
}

[Fact]
public void Hash()
{
Expand Down