Repository: incubator-samza Updated Branches: refs/heads/master ffb282784 -> 610ee8492
SAMZA-331: SystemStreamPartition should implement Comparable for sorting goodness Project: http://git-wip-us.apache.org/repos/asf/incubator-samza/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-samza/commit/610ee849 Tree: http://git-wip-us.apache.org/repos/asf/incubator-samza/tree/610ee849 Diff: http://git-wip-us.apache.org/repos/asf/incubator-samza/diff/610ee849 Branch: refs/heads/master Commit: 610ee8492e0ef537d8a109262bd1ed65aeac1eb8 Parents: ffb2827 Author: Jakob Homan <[email protected]> Authored: Mon Jul 14 14:22:48 2014 -0700 Committer: Jakob Homan <[email protected]> Committed: Mon Jul 14 14:22:48 2014 -0700 ---------------------------------------------------------------------- .../main/java/org/apache/samza/Partition.java | 12 ++++++++- .../samza/system/SystemStreamPartition.java | 26 ++++++++++++++++++-- 2 files changed, 35 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-samza/blob/610ee849/samza-api/src/main/java/org/apache/samza/Partition.java ---------------------------------------------------------------------- diff --git a/samza-api/src/main/java/org/apache/samza/Partition.java b/samza-api/src/main/java/org/apache/samza/Partition.java index 66d517d..864f9bd 100644 --- a/samza-api/src/main/java/org/apache/samza/Partition.java +++ b/samza-api/src/main/java/org/apache/samza/Partition.java @@ -22,7 +22,7 @@ package org.apache.samza; /** * A numbered, ordered partition of a stream. */ -public class Partition { +public class Partition implements Comparable<Partition> { private final int partition; /** @@ -60,4 +60,14 @@ public class Partition { public String toString() { return "Partition [partition=" + partition + "]"; } + + @Override + public int compareTo(Partition that) { + if (partition < that.partition) { + return -1; + } else if (partition > that.partition) { + return 1; + } + return 0; + } } http://git-wip-us.apache.org/repos/asf/incubator-samza/blob/610ee849/samza-api/src/main/java/org/apache/samza/system/SystemStreamPartition.java ---------------------------------------------------------------------- diff --git a/samza-api/src/main/java/org/apache/samza/system/SystemStreamPartition.java b/samza-api/src/main/java/org/apache/samza/system/SystemStreamPartition.java index 4fed4dc..8dcea09 100644 --- a/samza-api/src/main/java/org/apache/samza/system/SystemStreamPartition.java +++ b/samza-api/src/main/java/org/apache/samza/system/SystemStreamPartition.java @@ -24,9 +24,9 @@ import org.apache.samza.Partition; /** * Aggregate object representing a both the {@link org.apache.samza.system.SystemStream} and {@link org.apache.samza.Partition}. */ -public class SystemStreamPartition extends SystemStream { +public class SystemStreamPartition extends SystemStream implements Comparable<SystemStreamPartition> { protected final Partition partition; - protected final int hash; + protected final int hash; // precomputed as instances are immutable and often stored in hash-addressed data structures /** * Constructs a Samza stream partition object from specified components. @@ -98,4 +98,26 @@ public class SystemStreamPartition extends SystemStream { public String toString() { return "SystemStreamPartition ["+ system + ", " + stream + ", " + partition.getPartitionId() + "]"; } + + @Override + public int compareTo(SystemStreamPartition that) { + if (this.system.compareTo(that.system) < 0) { + return -1; + } else if (this.system.compareTo(that.system) > 0) { + return 1; + } + + if (this.stream.compareTo(that.stream) < 0) { + return -1; + } else if (this.stream.compareTo(that.stream) > 0) { + return 1; + } + + if (this.partition.compareTo(that.partition) < 0) { + return -1; + } else if (this.partition.compareTo(that.partition) > 0) { + return 1; + } + return 0; + } }
