blob: 07a5d71ea59dcd0e8db616275e78cbf9f6b61598 [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
Madan Jampanifa242182016-01-22 13:42:54 -080045 public DefaultPartition(Partition other) {
46 this.id = checkNotNull(other.getId());
47 this.members = ImmutableSet.copyOf(other.getMembers());
48 }
49
Madan Jampaniab7e7cd2016-01-14 14:02:32 -080050 @Override
51 public PartitionId getId() {
52 return this.id;
53 }
54
55 @Override
56 public Collection<NodeId> getMembers() {
57 return this.members;
58 }
59
60 @Override
61 public String toString() {
62 return MoreObjects.toStringHelper(getClass())
63 .add("id", id)
64 .add("members", members)
65 .toString();
66 }
67
68 @Override
69 public int hashCode() {
70 return Objects.hash(id, members);
71 }
72
73 @Override
74 public boolean equals(Object other) {
75 if (!(other instanceof DefaultPartition)) {
76 return false;
77 }
78 DefaultPartition that = (DefaultPartition) other;
79 return this.getId().equals(that.getId()) &&
80 Sets.symmetricDifference(Sets.newHashSet(this.members), Sets.newHashSet(that.members)).isEmpty();
81 }
82}