David K. Bainbridge
Committed by Gerrit Code Review

added equality functions that do a deep content check

Change-Id: Ibc64373587622fa6911d18e21d1695577cc36301
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
15 */ 15 */
16 package org.onosproject.cluster; 16 package org.onosproject.cluster;
17 17
18 +import java.util.Arrays;
18 import java.util.Collection; 19 import java.util.Collection;
19 import java.util.Set; 20 import java.util.Set;
20 import java.util.stream.Collectors; 21 import java.util.stream.Collectors;
...@@ -26,6 +27,7 @@ import static com.google.common.base.Verify.verify; ...@@ -26,6 +27,7 @@ import static com.google.common.base.Verify.verify;
26 import com.google.common.base.MoreObjects; 27 import com.google.common.base.MoreObjects;
27 import com.google.common.collect.Collections2; 28 import com.google.common.collect.Collections2;
28 import com.google.common.collect.ImmutableSet; 29 import com.google.common.collect.ImmutableSet;
30 +import com.google.common.collect.Sets;
29 31
30 /** 32 /**
31 * Cluster metadata. 33 * Cluster metadata.
...@@ -82,6 +84,33 @@ public final class ClusterMetadata { ...@@ -82,6 +84,33 @@ public final class ClusterMetadata {
82 .toString(); 84 .toString();
83 } 85 }
84 86
87 + @Override
88 + public int hashCode() {
89 + return Arrays.deepHashCode(new Object[] {name, nodes, partitions});
90 + }
91 +
92 + /*
93 + * Provide a deep quality check of the meta data (non-Javadoc)
94 + *
95 + * @see java.lang.Object#equals(java.lang.Object)
96 + */
97 + @Override
98 + public boolean equals(Object object) {
99 +
100 + if (!ClusterMetadata.class.isInstance(object)) {
101 + return false;
102 + }
103 + ClusterMetadata that = (ClusterMetadata) object;
104 +
105 + if (!this.name.equals(that.name) || this.nodes.size() != that.nodes.size()
106 + || this.partitions.size() != that.partitions.size()) {
107 + return false;
108 + }
109 +
110 + return Sets.symmetricDifference(this.nodes, that.nodes).isEmpty()
111 + && Sets.symmetricDifference(this.partitions, that.partitions).isEmpty();
112 + }
113 +
85 /** 114 /**
86 * Builder for a {@link ClusterMetadata} instance. 115 * Builder for a {@link ClusterMetadata} instance.
87 */ 116 */
...@@ -146,10 +175,10 @@ public final class ClusterMetadata { ...@@ -146,10 +175,10 @@ public final class ClusterMetadata {
146 175
147 // verify that partitions are constituted from valid cluster nodes. 176 // verify that partitions are constituted from valid cluster nodes.
148 boolean validPartitions = Collections2.transform(metadata.getNodes(), ControllerNode::id) 177 boolean validPartitions = Collections2.transform(metadata.getNodes(), ControllerNode::id)
149 - .containsAll(metadata.getPartitions() 178 + .containsAll(metadata.getPartitions()
150 - .stream() 179 + .stream()
151 - .flatMap(r -> r.getMembers().stream()) 180 + .flatMap(r -> r.getMembers().stream())
152 - .collect(Collectors.toSet())); 181 + .collect(Collectors.toSet()));
153 verify(validPartitions, "Partition locations must be valid cluster nodes"); 182 verify(validPartitions, "Partition locations must be valid cluster nodes");
154 } 183 }
155 } 184 }
......
...@@ -20,7 +20,7 @@ import java.util.Objects; ...@@ -20,7 +20,7 @@ import java.util.Objects;
20 /** 20 /**
21 * Controller cluster identity. 21 * Controller cluster identity.
22 */ 22 */
23 -public class NodeId { 23 +public class NodeId implements Comparable<NodeId> {
24 24
25 private final String id; 25 private final String id;
26 26
...@@ -55,4 +55,9 @@ public class NodeId { ...@@ -55,4 +55,9 @@ public class NodeId {
55 return id; 55 return id;
56 } 56 }
57 57
58 + @Override
59 + public int compareTo(NodeId that) {
60 + return this.id.compareTo(that.id);
61 + }
62 +
58 } 63 }
......
...@@ -15,10 +15,12 @@ ...@@ -15,10 +15,12 @@
15 */ 15 */
16 package org.onosproject.cluster; 16 package org.onosproject.cluster;
17 17
18 +import java.util.Arrays;
18 import java.util.Collection; 19 import java.util.Collection;
19 import java.util.Set; 20 import java.util.Set;
20 21
21 import com.google.common.collect.ImmutableSet; 22 import com.google.common.collect.ImmutableSet;
23 +import com.google.common.collect.Sets;
22 24
23 import static com.google.common.base.Preconditions.checkNotNull; 25 import static com.google.common.base.Preconditions.checkNotNull;
24 26
...@@ -61,4 +63,29 @@ public class Partition { ...@@ -61,4 +63,29 @@ public class Partition {
61 public Collection<NodeId> getMembers() { 63 public Collection<NodeId> getMembers() {
62 return this.members; 64 return this.members;
63 } 65 }
66 +
67 + @Override
68 + public int hashCode() {
69 + return Arrays.deepHashCode(new Object[] {name, members});
70 + }
71 +
72 + @Override
73 + public boolean equals(Object other) {
74 + if (this == other) {
75 + return true;
76 + }
77 +
78 + if (other == null || !Partition.class.isInstance(other)) {
79 + return false;
80 + }
81 +
82 + Partition that = (Partition) other;
83 +
84 + if (!this.name.equals(that.name) || (this.members == null && that.members != null)
85 + || (this.members != null && that.members == null) || this.members.size() != that.members.size()) {
86 + return false;
87 + }
88 +
89 + return Sets.symmetricDifference(this.members, that.members).isEmpty();
90 + }
64 } 91 }
...\ No newline at end of file ...\ No newline at end of file
......