blob: 491f9a9e06a8ba8ce724d8dd4808728bb3e6c9f7 [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
Madan Jampaniab7e7cd2016-01-14 14:02:32 -080018import com.google.common.base.MoreObjects;
19import com.google.common.collect.ImmutableSet;
20import com.google.common.collect.Sets;
Ray Milkey4f7e3632019-02-19 15:35:20 -080021
22import java.util.Collection;
23import java.util.Objects;
Madan Jampaniab7e7cd2016-01-14 14:02:32 -080024
Jordan Halterman00e92da2018-05-22 23:05:52 -070025import static com.google.common.base.Preconditions.checkNotNull;
26
Madan Jampaniab7e7cd2016-01-14 14:02:32 -080027/**
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 */
Jordan Halterman00e92da2018-05-22 23:05:52 -070049 public DefaultPartition(PartitionId id, Collection<NodeId> members) {
Madan Jampaniab7e7cd2016-01-14 14:02:32 -080050 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() {
Jordan Halterman07f052b2017-10-08 14:22:41 -070066 return id;
67 }
68
69 @Override
Madan Jampaniab7e7cd2016-01-14 14:02:32 -080070 public Collection<NodeId> getMembers() {
Jordan Halterman07f052b2017-10-08 14:22:41 -070071 return members;
Madan Jampaniab7e7cd2016-01-14 14:02:32 -080072 }
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}