blob: d0f2f3d480d57c64cbcfecf08f9d95e9ed806427 [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;
Jordan Halterman07f052b2017-10-08 14:22:41 -070026import org.onosproject.core.Version;
Madan Jampaniab7e7cd2016-01-14 14:02:32 -080027
28/**
29 * Default {@link Partition} implementation.
30 */
31public class DefaultPartition implements Partition {
32
33 private final PartitionId id;
Jordan Halterman07f052b2017-10-08 14:22:41 -070034 private final Version version;
Madan Jampaniab7e7cd2016-01-14 14:02:32 -080035 private final Collection<NodeId> members;
36
Ray Milkeycc6310f2016-02-12 19:02:10 -080037 /**
38 * Constructs an empty partition for the serializer.
39 */
40 protected DefaultPartition() {
Madan Jampaniab7e7cd2016-01-14 14:02:32 -080041 id = null;
Jordan Halterman07f052b2017-10-08 14:22:41 -070042 version = null;
Madan Jampaniab7e7cd2016-01-14 14:02:32 -080043 members = null;
44 }
45
Ray Milkeycc6310f2016-02-12 19:02:10 -080046 /**
47 * Constructs a partition.
48 *
49 * @param id partition identifier
Jordan Halterman07f052b2017-10-08 14:22:41 -070050 * @param version partition version
Ray Milkeycc6310f2016-02-12 19:02:10 -080051 * @param members partition member nodes
52 */
Jordan Halterman07f052b2017-10-08 14:22:41 -070053 public DefaultPartition(PartitionId id, Version version, Collection<NodeId> members) {
Madan Jampaniab7e7cd2016-01-14 14:02:32 -080054 this.id = checkNotNull(id);
Jordan Halterman07f052b2017-10-08 14:22:41 -070055 this.version = version;
Madan Jampaniab7e7cd2016-01-14 14:02:32 -080056 this.members = ImmutableSet.copyOf(members);
57 }
58
Ray Milkeycc6310f2016-02-12 19:02:10 -080059 /**
60 * Constructs a partition that is a copy of another.
61 *
62 * @param other partition to copy
63 */
Madan Jampanifa242182016-01-22 13:42:54 -080064 public DefaultPartition(Partition other) {
65 this.id = checkNotNull(other.getId());
Jordan Halterman07f052b2017-10-08 14:22:41 -070066 this.version = checkNotNull(other.getVersion());
Madan Jampanifa242182016-01-22 13:42:54 -080067 this.members = ImmutableSet.copyOf(other.getMembers());
68 }
69
Madan Jampaniab7e7cd2016-01-14 14:02:32 -080070 @Override
71 public PartitionId getId() {
Jordan Halterman07f052b2017-10-08 14:22:41 -070072 return id;
73 }
74
75 @Override
76 public Version getVersion() {
77 return version;
Madan Jampaniab7e7cd2016-01-14 14:02:32 -080078 }
79
80 @Override
81 public Collection<NodeId> getMembers() {
Jordan Halterman07f052b2017-10-08 14:22:41 -070082 return members;
Madan Jampaniab7e7cd2016-01-14 14:02:32 -080083 }
84
85 @Override
86 public String toString() {
87 return MoreObjects.toStringHelper(getClass())
88 .add("id", id)
Jordan Halterman07f052b2017-10-08 14:22:41 -070089 .add("version", version)
Madan Jampaniab7e7cd2016-01-14 14:02:32 -080090 .add("members", members)
91 .toString();
92 }
93
94 @Override
95 public int hashCode() {
96 return Objects.hash(id, members);
97 }
98
99 @Override
100 public boolean equals(Object other) {
101 if (!(other instanceof DefaultPartition)) {
102 return false;
103 }
104 DefaultPartition that = (DefaultPartition) other;
105 return this.getId().equals(that.getId()) &&
106 Sets.symmetricDifference(Sets.newHashSet(this.members), Sets.newHashSet(that.members)).isEmpty();
107 }
Ray Milkeycc6310f2016-02-12 19:02:10 -0800108}