David K. Bainbridge
Committed by Gerrit Code Review

added equality functions that do a deep content check

Change-Id: Ibc64373587622fa6911d18e21d1695577cc36301
......@@ -15,6 +15,7 @@
*/
package org.onosproject.cluster;
import java.util.Arrays;
import java.util.Collection;
import java.util.Set;
import java.util.stream.Collectors;
......@@ -26,6 +27,7 @@ import static com.google.common.base.Verify.verify;
import com.google.common.base.MoreObjects;
import com.google.common.collect.Collections2;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;
/**
* Cluster metadata.
......@@ -82,6 +84,33 @@ public final class ClusterMetadata {
.toString();
}
@Override
public int hashCode() {
return Arrays.deepHashCode(new Object[] {name, nodes, partitions});
}
/*
* Provide a deep quality check of the meta data (non-Javadoc)
*
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object object) {
if (!ClusterMetadata.class.isInstance(object)) {
return false;
}
ClusterMetadata that = (ClusterMetadata) object;
if (!this.name.equals(that.name) || this.nodes.size() != that.nodes.size()
|| this.partitions.size() != that.partitions.size()) {
return false;
}
return Sets.symmetricDifference(this.nodes, that.nodes).isEmpty()
&& Sets.symmetricDifference(this.partitions, that.partitions).isEmpty();
}
/**
* Builder for a {@link ClusterMetadata} instance.
*/
......@@ -146,10 +175,10 @@ public final class ClusterMetadata {
// verify that partitions are constituted from valid cluster nodes.
boolean validPartitions = Collections2.transform(metadata.getNodes(), ControllerNode::id)
.containsAll(metadata.getPartitions()
.stream()
.flatMap(r -> r.getMembers().stream())
.collect(Collectors.toSet()));
.containsAll(metadata.getPartitions()
.stream()
.flatMap(r -> r.getMembers().stream())
.collect(Collectors.toSet()));
verify(validPartitions, "Partition locations must be valid cluster nodes");
}
}
......
......@@ -20,7 +20,7 @@ import java.util.Objects;
/**
* Controller cluster identity.
*/
public class NodeId {
public class NodeId implements Comparable<NodeId> {
private final String id;
......@@ -55,4 +55,9 @@ public class NodeId {
return id;
}
@Override
public int compareTo(NodeId that) {
return this.id.compareTo(that.id);
}
}
......
......@@ -15,10 +15,12 @@
*/
package org.onosproject.cluster;
import java.util.Arrays;
import java.util.Collection;
import java.util.Set;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;
import static com.google.common.base.Preconditions.checkNotNull;
......@@ -61,4 +63,29 @@ public class Partition {
public Collection<NodeId> getMembers() {
return this.members;
}
@Override
public int hashCode() {
return Arrays.deepHashCode(new Object[] {name, members});
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (other == null || !Partition.class.isInstance(other)) {
return false;
}
Partition that = (Partition) other;
if (!this.name.equals(that.name) || (this.members == null && that.members != null)
|| (this.members != null && that.members == null) || this.members.size() != that.members.size()) {
return false;
}
return Sets.symmetricDifference(this.members, that.members).isEmpty();
}
}
\ No newline at end of file
......