blob: 58326084d7ad0beeb363a7db00aff84d3d488306 [file] [log] [blame]
Madan Jampaniab7e7cd2016-01-14 14:02:32 -08001/*
2 * Copyright 2015-2016 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Madan Jampani8474fdd2016-01-19 09:56:28 -080016package org.onosproject.cluster;
Madan Jampaniab7e7cd2016-01-14 14:02:32 -080017
18import static com.google.common.base.Preconditions.checkNotNull;
19
20import java.util.Collection;
21import java.util.Objects;
22
Madan Jampaniab7e7cd2016-01-14 14:02:32 -080023import com.google.common.base.MoreObjects;
24import com.google.common.collect.ImmutableSet;
25import com.google.common.collect.Sets;
26
27/**
28 * Default {@link Partition} implementation.
29 */
30public class DefaultPartition implements Partition {
31
32 private final PartitionId id;
33 private final Collection<NodeId> members;
34
35 private DefaultPartition() {
36 id = null;
37 members = null;
38 }
39
40 public DefaultPartition(PartitionId id, Collection<NodeId> members) {
41 this.id = checkNotNull(id);
42 this.members = ImmutableSet.copyOf(members);
43 }
44
45 @Override
46 public PartitionId getId() {
47 return this.id;
48 }
49
50 @Override
51 public Collection<NodeId> getMembers() {
52 return this.members;
53 }
54
55 @Override
56 public String toString() {
57 return MoreObjects.toStringHelper(getClass())
58 .add("id", id)
59 .add("members", members)
60 .toString();
61 }
62
63 @Override
64 public int hashCode() {
65 return Objects.hash(id, members);
66 }
67
68 @Override
69 public boolean equals(Object other) {
70 if (!(other instanceof DefaultPartition)) {
71 return false;
72 }
73 DefaultPartition that = (DefaultPartition) other;
74 return this.getId().equals(that.getId()) &&
75 Sets.symmetricDifference(Sets.newHashSet(this.members), Sets.newHashSet(that.members)).isEmpty();
76 }
77}