diff --git a/src/Confluent.Kafka/Offset.cs b/src/Confluent.Kafka/Offset.cs index 5d6438d05..1a47b99cd 100644 --- a/src/Confluent.Kafka/Offset.cs +++ b/src/Confluent.Kafka/Offset.cs @@ -28,7 +28,7 @@ namespace Confluent.Kafka /// its purpose is to add some syntactical sugar /// related to special values. /// - public struct Offset : IEquatable + public struct Offset : IEquatable, IComparable { private const long RD_KAFKA_OFFSET_BEGINNING = -2; private const long RD_KAFKA_OFFSET_END = -1; @@ -287,5 +287,31 @@ public override string ToString() return Value.ToString(); } } + + /// 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. + /// An object to compare with this instance. + /// + /// A value that indicates the relative order of the objects being compared. The return value has these meanings: + /// + /// + /// Value + /// Meaning + /// + /// + /// Less than zero + /// This instance precedes in the sort order. + /// + /// + /// Zero + /// This instance occurs in the same position in the sort order as . + /// + /// + /// Greater than zero + /// This instance follows in the sort order. + /// + /// + /// + public int CompareTo(Offset other) + => Value.CompareTo(other.Value); } } diff --git a/src/Confluent.Kafka/Partition.cs b/src/Confluent.Kafka/Partition.cs index 2b49e1ec4..01b529ae9 100644 --- a/src/Confluent.Kafka/Partition.cs +++ b/src/Confluent.Kafka/Partition.cs @@ -28,7 +28,7 @@ namespace Confluent.Kafka /// its purpose is to add some syntactical sugar /// related to special values. /// - public struct Partition : IEquatable + public struct Partition : IEquatable, IComparable { private const int RD_KAFKA_PARTITION_UA = -1; @@ -224,5 +224,31 @@ public override string ToString() return $"[{Value}]"; } } + + /// 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. + /// An object to compare with this instance. + /// + /// A value that indicates the relative order of the objects being compared. The return value has these meanings: + /// + /// + /// Value + /// Meaning + /// + /// + /// Less than zero + /// This instance precedes in the sort order. + /// + /// + /// Zero + /// This instance occurs in the same position in the sort order as . + /// + /// + /// Greater than zero + /// This instance follows in the sort order. + /// + /// + /// + public int CompareTo(Partition other) + => Value.CompareTo(other.Value); } } diff --git a/test/Confluent.Kafka.UnitTests/Offset.cs b/test/Confluent.Kafka.UnitTests/Offset.cs index 66e551784..aacbb6148 100644 --- a/test/Confluent.Kafka.UnitTests/Offset.cs +++ b/test/Confluent.Kafka.UnitTests/Offset.cs @@ -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() { diff --git a/test/Confluent.Kafka.UnitTests/Partition.cs b/test/Confluent.Kafka.UnitTests/Partition.cs index 309f31739..770a8f9c5 100644 --- a/test/Confluent.Kafka.UnitTests/Partition.cs +++ b/test/Confluent.Kafka.UnitTests/Partition.cs @@ -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() {