blob: 83ab5b97a00cee1a83a48b1336fb564ff4103a43 [file] [log] [blame]
Madan Jampaniab7e7cd2016-01-14 14:02:32 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Madan Jampaniab7e7cd2016-01-14 14:02:32 -08003 *
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
Ray Milkeycc6310f2016-02-12 19:02:10 -080035 /**
36 * Constructs an empty partition for the serializer.
37 */
38 protected DefaultPartition() {
Madan Jampaniab7e7cd2016-01-14 14:02:32 -080039 id = null;
40 members = null;
41 }
42
Ray Milkeycc6310f2016-02-12 19:02:10 -080043 /**
44 * Constructs a partition.
45 *
46 * @param id partition identifier
47 * @param members partition member nodes
48 */
Madan Jampaniab7e7cd2016-01-14 14:02:32 -080049 public DefaultPartition(PartitionId id, Collection<NodeId> members) {
50 this.id = checkNotNull(id);
51 this.members = ImmutableSet.copyOf(members);
52 }
53
Ray Milkeycc6310f2016-02-12 19:02:10 -080054 /**
55 * Constructs a partition that is a copy of another.
56 *
57 * @param other partition to copy
58 */
Madan Jampanifa242182016-01-22 13:42:54 -080059 public DefaultPartition(Partition other) {
60 this.id = checkNotNull(other.getId());
61 this.members = ImmutableSet.copyOf(other.getMembers());
62 }
63
Madan Jampaniab7e7cd2016-01-14 14:02:32 -080064 @Override
65 public PartitionId getId() {
66 return this.id;
67 }
68
69 @Override
70 public Collection<NodeId> getMembers() {
71 return this.members;
72 }
73
74 @Override
75 public String toString() {
76 return MoreObjects.toStringHelper(getClass())
77 .add("id", id)
78 .add("members", members)
79 .toString();
80 }
81
82 @Override
83 public int hashCode() {
84 return Objects.hash(id, members);
85 }
86
87 @Override
88 public boolean equals(Object other) {
89 if (!(other instanceof DefaultPartition)) {
90 return false;
91 }
92 DefaultPartition that = (DefaultPartition) other;
93 return this.getId().equals(that.getId()) &&
94 Sets.symmetricDifference(Sets.newHashSet(this.members), Sets.newHashSet(that.members)).isEmpty();
95 }
Ray Milkeycc6310f2016-02-12 19:02:10 -080096}