Skip to content

Commit 7f36800

Browse files
author
Олександр Ляхевич
committed
Implement IComparable for Offset and Partition
1 parent 4d60910 commit 7f36800

File tree

4 files changed

+76
-2
lines changed

4 files changed

+76
-2
lines changed

src/Confluent.Kafka/Offset.cs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ namespace Confluent.Kafka
2828
/// its purpose is to add some syntactical sugar
2929
/// related to special values.
3030
/// </remarks>
31-
public struct Offset : IEquatable<Offset>
31+
public struct Offset : IEquatable<Offset>, IComparable<Offset>
3232
{
3333
private const long RD_KAFKA_OFFSET_BEGINNING = -2;
3434
private const long RD_KAFKA_OFFSET_END = -1;
@@ -287,5 +287,31 @@ public override string ToString()
287287
return Value.ToString();
288288
}
289289
}
290+
291+
/// <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>
292+
/// <param name="other">An object to compare with this instance.</param>
293+
/// <returns>
294+
/// <para>A value that indicates the relative order of the objects being compared. The return value has these meanings:</para>
295+
/// <list type="table">
296+
/// <listheader>
297+
/// <term>Value</term>
298+
/// <description>Meaning</description>
299+
/// </listheader>
300+
/// <item>
301+
/// <term>Less than zero</term>
302+
/// <description>This instance precedes <paramref name="other" /> in the sort order.</description>
303+
/// </item>
304+
/// <item>
305+
/// <term>Zero</term>
306+
/// <description>This instance occurs in the same position in the sort order as <paramref name="other" />.</description>
307+
/// </item>
308+
/// <item>
309+
/// <term>Greater than zero</term>
310+
/// <description>This instance follows <paramref name="other" /> in the sort order.</description>
311+
/// </item>
312+
/// </list>
313+
/// </returns>
314+
public int CompareTo(Offset other)
315+
=> Value.CompareTo(other.Value);
290316
}
291317
}

src/Confluent.Kafka/Partition.cs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ namespace Confluent.Kafka
2828
/// its purpose is to add some syntactical sugar
2929
/// related to special values.
3030
/// </remarks>
31-
public struct Partition : IEquatable<Partition>
31+
public struct Partition : IEquatable<Partition>, IComparable<Partition>
3232
{
3333
private const int RD_KAFKA_PARTITION_UA = -1;
3434

@@ -224,5 +224,31 @@ public override string ToString()
224224
return $"[{Value}]";
225225
}
226226
}
227+
228+
/// <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>
229+
/// <param name="other">An object to compare with this instance.</param>
230+
/// <returns>
231+
/// <para>A value that indicates the relative order of the objects being compared. The return value has these meanings:</para>
232+
/// <list type="table">
233+
/// <listheader>
234+
/// <term>Value</term>
235+
/// <description>Meaning</description>
236+
/// </listheader>
237+
/// <item>
238+
/// <term>Less than zero</term>
239+
/// <description>This instance precedes <paramref name="other" /> in the sort order.</description>
240+
/// </item>
241+
/// <item>
242+
/// <term>Zero</term>
243+
/// <description>This instance occurs in the same position in the sort order as <paramref name="other" />.</description>
244+
/// </item>
245+
/// <item>
246+
/// <term>Greater than zero</term>
247+
/// <description>This instance follows <paramref name="other" /> in the sort order.</description>
248+
/// </item>
249+
/// </list>
250+
/// </returns>
251+
public int CompareTo(Partition other)
252+
=> Value.CompareTo(other.Value);
227253
}
228254
}

test/Confluent.Kafka.UnitTests/Offset.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,17 @@ public void Inequality()
6969
Assert.True(a >= a2);
7070
}
7171

72+
[Fact]
73+
public void CompareTo()
74+
{
75+
Offset a = new Offset(42);
76+
Offset a2 = new Offset(42);
77+
Offset b = new Offset(37);
78+
Assert.True(a.CompareTo(b) > 0);
79+
Assert.True(b.CompareTo(a) < 0);
80+
Assert.True(a.CompareTo(a2) == 0);
81+
}
82+
7283
[Fact]
7384
public void Addition_Int()
7485
{

test/Confluent.Kafka.UnitTests/Partition.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,17 @@ public void Inequality()
6666
Assert.True(a >= a2);
6767
}
6868

69+
[Fact]
70+
public void CompareTo()
71+
{
72+
Partition a = new Partition(42);
73+
Partition a2 = new Partition(42);
74+
Partition b = new Partition(37);
75+
Assert.True(a.CompareTo(b) > 0);
76+
Assert.True(b.CompareTo(a) < 0);
77+
Assert.True(a.CompareTo(a2) == 0);
78+
}
79+
6980
[Fact]
7081
public void Hash()
7182
{

0 commit comments

Comments
 (0)