From 04de4c5aaa1f2235a5209df33f0beb00a2be0078 Mon Sep 17 00:00:00 2001 From: Parth Brahmbhatt Date: Mon, 29 Dec 2014 15:02:39 -0800 Subject: [PATCH 1/3] KAFKA-1805: adding equals and hashcode methods to ProducerRecord. --- .../kafka/clients/producer/ProducerRecord.java | 25 ++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/clients/src/main/java/org/apache/kafka/clients/producer/ProducerRecord.java b/clients/src/main/java/org/apache/kafka/clients/producer/ProducerRecord.java index 065d4e6..e47d12a 100644 --- a/clients/src/main/java/org/apache/kafka/clients/producer/ProducerRecord.java +++ b/clients/src/main/java/org/apache/kafka/clients/producer/ProducerRecord.java @@ -73,6 +73,7 @@ public final class ProducerRecord { } /** + * The key (or null if no key is specified) */ public K key() { @@ -99,4 +100,28 @@ public final class ProducerRecord { String value = this.value == null ? "null" : this.value.toString(); return "ProducerRecord(topic=" + topic + ", partition=" + partition + ", key=" + key + ", value=" + value; } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof ProducerRecord)) return false; + + ProducerRecord that = (ProducerRecord) o; + + if (!key.equals(that.key)) return false; + if (!partition.equals(that.partition)) return false; + if (!topic.equals(that.topic)) return false; + if (!value.equals(that.value)) return false; + + return true; + } + + @Override + public int hashCode() { + int result = topic.hashCode(); + result = 31 * result + partition.hashCode(); + result = 31 * result + key.hashCode(); + result = 31 * result + value.hashCode(); + return result; + } } -- 1.9.3 (Apple Git-50) From b606b575d597b734b995fc0b05f1941981021b0a Mon Sep 17 00:00:00 2001 From: Parth Brahmbhatt Date: Wed, 11 Feb 2015 14:23:39 -0800 Subject: [PATCH 2/3] Handling the case where al the fields in ProducerRecord can be null. --- .../kafka/clients/producer/ProducerRecord.java | 24 ++++++++-------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/clients/src/main/java/org/apache/kafka/clients/producer/ProducerRecord.java b/clients/src/main/java/org/apache/kafka/clients/producer/ProducerRecord.java index e47d12a..c30ad4b 100644 --- a/clients/src/main/java/org/apache/kafka/clients/producer/ProducerRecord.java +++ b/clients/src/main/java/org/apache/kafka/clients/producer/ProducerRecord.java @@ -73,7 +73,6 @@ public final class ProducerRecord { } /** - * The key (or null if no key is specified) */ public K key() { @@ -95,33 +94,26 @@ public final class ProducerRecord { } @Override - public String toString() { - String key = this.key == null ? "null" : this.key.toString(); - String value = this.value == null ? "null" : this.value.toString(); - return "ProducerRecord(topic=" + topic + ", partition=" + partition + ", key=" + key + ", value=" + value; - } - - @Override public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof ProducerRecord)) return false; ProducerRecord that = (ProducerRecord) o; - if (!key.equals(that.key)) return false; - if (!partition.equals(that.partition)) return false; - if (!topic.equals(that.topic)) return false; - if (!value.equals(that.value)) return false; + if (key != null ? !key.equals(that.key) : that.key != null) return false; + if (partition != null ? !partition.equals(that.partition) : that.partition != null) return false; + if (topic != null ? !topic.equals(that.topic) : that.topic != null) return false; + if (value != null ? !value.equals(that.value) : that.value != null) return false; return true; } @Override public int hashCode() { - int result = topic.hashCode(); - result = 31 * result + partition.hashCode(); - result = 31 * result + key.hashCode(); - result = 31 * result + value.hashCode(); + int result = topic != null ? topic.hashCode() : 0; + result = 31 * result + (partition != null ? partition.hashCode() : 0); + result = 31 * result + (key != null ? key.hashCode() : 0); + result = 31 * result + (value != null ? value.hashCode() : 0); return result; } } -- 1.9.3 (Apple Git-50) From 499b73d29ca81f593841a72d7e01ae2d89299082 Mon Sep 17 00:00:00 2001 From: Parth Brahmbhatt Date: Wed, 11 Feb 2015 14:34:25 -0800 Subject: [PATCH 3/3] Adding toString back. --- .../java/org/apache/kafka/clients/producer/ProducerRecord.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/clients/src/main/java/org/apache/kafka/clients/producer/ProducerRecord.java b/clients/src/main/java/org/apache/kafka/clients/producer/ProducerRecord.java index c30ad4b..85413c7 100644 --- a/clients/src/main/java/org/apache/kafka/clients/producer/ProducerRecord.java +++ b/clients/src/main/java/org/apache/kafka/clients/producer/ProducerRecord.java @@ -93,6 +93,14 @@ public final class ProducerRecord { return partition; } + + @Override + public String toString() { + String key = this.key == null ? "null" : this.key.toString(); + String value = this.value == null ? "null" : this.value.toString(); + return "ProducerRecord(topic=" + topic + ", partition=" + partition + ", key=" + key + ", value=" + value; + } + @Override public boolean equals(Object o) { if (this == o) return true; -- 1.9.3 (Apple Git-50)