Skip to content

Commit 7d047c4

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

File tree

4 files changed

+56
-2
lines changed

4 files changed

+56
-2
lines changed

src/Confluent.Kafka/Offset.cs

Lines changed: 17 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,21 @@ public override string ToString()
287287
return Value.ToString();
288288
}
289289
}
290+
291+
/// <summary>
292+
/// Compares the current instance with another object of the same type and returns
293+
/// an integer that indicates whether the current instance precedes, follows, or
294+
/// occurs in the same position in the sort order as the other object.
295+
/// </summary>
296+
/// <param name="other">An object to compare with this instance.</param>
297+
/// <returns>
298+
/// A value that indicates the relative order of the objects being compared. The
299+
/// return value has these meanings:
300+
/// Value – Meaning
301+
/// Less than zero – This instance precedes other in the sort order.
302+
/// Zero – This instance occurs in the same position in the sort order as other.
303+
/// Greater than zero – This instance follows other in the sort order.</returns>
304+
public int CompareTo(Offset other)
305+
=> Value.CompareTo(other.Value);
290306
}
291307
}

src/Confluent.Kafka/Partition.cs

Lines changed: 17 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,21 @@ public override string ToString()
224224
return $"[{Value}]";
225225
}
226226
}
227+
228+
/// <summary>
229+
/// Compares the current instance with another object of the same type and returns
230+
/// an integer that indicates whether the current instance precedes, follows, or
231+
/// occurs in the same position in the sort order as the other object.
232+
/// </summary>
233+
/// <param name="other">An object to compare with this instance.</param>
234+
/// <returns>
235+
/// A value that indicates the relative order of the objects being compared. The
236+
/// return value has these meanings:
237+
/// Value – Meaning
238+
/// Less than zero – This instance precedes other in the sort order.
239+
/// Zero – This instance occurs in the same position in the sort order as other.
240+
/// Greater than zero – This instance follows other in the sort order.</returns>
241+
public int CompareTo(Partition other)
242+
=> Value.CompareTo(other.Value);
227243
}
228244
}

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)