blob: f0a73b6815e8ce6054fc05bd44216152d14a9378 [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 java.util.Collection;
19import java.util.Objects;
20
Madan Jampaniab7e7cd2016-01-14 14:02:32 -080021import com.google.common.base.MoreObjects;
22import com.google.common.collect.ImmutableSet;
23import com.google.common.collect.Sets;
Jordan Halterman07f052b2017-10-08 14:22:41 -070024import org.onosproject.core.Version;
Madan Jampaniab7e7cd2016-01-14 14:02:32 -080025
Jordan Halterman00e92da2018-05-22 23:05:52 -070026import static com.google.common.base.Preconditions.checkNotNull;
27
Madan Jampaniab7e7cd2016-01-14 14:02:32 -080028/**
29 * Default {@link Partition} implementation.
30 */
31public class DefaultPartition implements Partition {
32
33 private final PartitionId id;
34 private final Collection<NodeId> members;
35
Ray Milkeycc6310f2016-02-12 19:02:10 -080036 /**
37 * Constructs an empty partition for the serializer.
38 */
39 protected DefaultPartition() {
Madan Jampaniab7e7cd2016-01-14 14:02:32 -080040 id = null;
41 members = null;
42 }
43
Ray Milkeycc6310f2016-02-12 19:02:10 -080044 /**
45 * Constructs a partition.
46 *
47 * @param id partition identifier
48 * @param members partition member nodes
49 */
Jordan Halterman00e92da2018-05-22 23:05:52 -070050 public DefaultPartition(PartitionId id, Collection<NodeId> members) {
Madan Jampaniab7e7cd2016-01-14 14:02:32 -080051 this.id = checkNotNull(id);
52 this.members = ImmutableSet.copyOf(members);
53 }
54
Ray Milkeycc6310f2016-02-12 19:02:10 -080055 /**
56 * Constructs a partition that is a copy of another.
57 *
58 * @param other partition to copy
59 */
Madan Jampanifa242182016-01-22 13:42:54 -080060 public DefaultPartition(Partition other) {
61 this.id = checkNotNull(other.getId());
62 this.members = ImmutableSet.copyOf(other.getMembers());
63 }
64
Madan Jampaniab7e7cd2016-01-14 14:02:32 -080065 @Override
66 public PartitionId getId() {
Jordan Halterman07f052b2017-10-08 14:22:41 -070067 return id;
68 }
69
70 @Override
71 public Version getVersion() {
Jordan Halterman00e92da2018-05-22 23:05:52 -070072 return null;
Madan Jampaniab7e7cd2016-01-14 14:02:32 -080073 }
74
75 @Override
76 public Collection<NodeId> getMembers() {
Jordan Halterman07f052b2017-10-08 14:22:41 -070077 return members;
Madan Jampaniab7e7cd2016-01-14 14:02:32 -080078 }
79
80 @Override
81 public String toString() {
82 return MoreObjects.toStringHelper(getClass())
83 .add("id", id)
84 .add("members", members)
85 .toString();
86 }
87
88 @Override
89 public int hashCode() {
90 return Objects.hash(id, members);
91 }
92
93 @Override
94 public boolean equals(Object other) {
95 if (!(other instanceof DefaultPartition)) {
96 return false;
97 }
98 DefaultPartition that = (DefaultPartition) other;
99 return this.getId().equals(that.getId()) &&
100 Sets.symmetricDifference(Sets.newHashSet(this.members), Sets.newHashSet(that.members)).isEmpty();
101 }
Ray Milkeycc6310f2016-02-12 19:02:10 -0800102}