blob: 82a3ba779afe8bc2a2bf4923c1025d50324ce783 [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 */
16package org.onosproject.store.cluster.impl;
17
18import static com.google.common.base.Preconditions.checkNotNull;
19
20import java.util.Collection;
21import java.util.Objects;
22
23import org.onosproject.cluster.NodeId;
24import org.onosproject.cluster.Partition;
25import org.onosproject.cluster.PartitionId;
26
27import com.google.common.base.MoreObjects;
28import com.google.common.collect.ImmutableSet;
29import com.google.common.collect.Sets;
30
31/**
32 * Default {@link Partition} implementation.
33 */
34public class DefaultPartition implements Partition {
35
36 private final PartitionId id;
37 private final Collection<NodeId> members;
38
39 private DefaultPartition() {
40 id = null;
41 members = null;
42 }
43
44 public DefaultPartition(PartitionId id, Collection<NodeId> members) {
45 this.id = checkNotNull(id);
46 this.members = ImmutableSet.copyOf(members);
47 }
48
49 @Override
50 public PartitionId getId() {
51 return this.id;
52 }
53
54 @Override
55 public Collection<NodeId> getMembers() {
56 return this.members;
57 }
58
59 @Override
60 public String toString() {
61 return MoreObjects.toStringHelper(getClass())
62 .add("id", id)
63 .add("members", members)
64 .toString();
65 }
66
67 @Override
68 public int hashCode() {
69 return Objects.hash(id, members);
70 }
71
72 @Override
73 public boolean equals(Object other) {
74 if (!(other instanceof DefaultPartition)) {
75 return false;
76 }
77 DefaultPartition that = (DefaultPartition) other;
78 return this.getId().equals(that.getId()) &&
79 Sets.symmetricDifference(Sets.newHashSet(this.members), Sets.newHashSet(that.members)).isEmpty();
80 }
81}