Madan Jampani | ab7e7cd | 2016-01-14 14:02:32 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Madan Jampani | ab7e7cd | 2016-01-14 14:02:32 -0800 | [diff] [blame] | 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 Jampani | 8474fdd | 2016-01-19 09:56:28 -0800 | [diff] [blame] | 16 | package org.onosproject.cluster; |
Madan Jampani | ab7e7cd | 2016-01-14 14:02:32 -0800 | [diff] [blame] | 17 | |
Madan Jampani | ab7e7cd | 2016-01-14 14:02:32 -0800 | [diff] [blame] | 18 | import com.google.common.base.MoreObjects; |
| 19 | import com.google.common.collect.ImmutableSet; |
| 20 | import com.google.common.collect.Sets; |
Ray Milkey | 4f7e363 | 2019-02-19 15:35:20 -0800 | [diff] [blame] | 21 | |
| 22 | import java.util.Collection; |
| 23 | import java.util.Objects; |
Madan Jampani | ab7e7cd | 2016-01-14 14:02:32 -0800 | [diff] [blame] | 24 | |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 25 | import static com.google.common.base.Preconditions.checkNotNull; |
| 26 | |
Madan Jampani | ab7e7cd | 2016-01-14 14:02:32 -0800 | [diff] [blame] | 27 | /** |
| 28 | * Default {@link Partition} implementation. |
| 29 | */ |
| 30 | public class DefaultPartition implements Partition { |
| 31 | |
| 32 | private final PartitionId id; |
| 33 | private final Collection<NodeId> members; |
| 34 | |
Ray Milkey | cc6310f | 2016-02-12 19:02:10 -0800 | [diff] [blame] | 35 | /** |
| 36 | * Constructs an empty partition for the serializer. |
| 37 | */ |
| 38 | protected DefaultPartition() { |
Madan Jampani | ab7e7cd | 2016-01-14 14:02:32 -0800 | [diff] [blame] | 39 | id = null; |
| 40 | members = null; |
| 41 | } |
| 42 | |
Ray Milkey | cc6310f | 2016-02-12 19:02:10 -0800 | [diff] [blame] | 43 | /** |
| 44 | * Constructs a partition. |
| 45 | * |
| 46 | * @param id partition identifier |
| 47 | * @param members partition member nodes |
| 48 | */ |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 49 | public DefaultPartition(PartitionId id, Collection<NodeId> members) { |
Madan Jampani | ab7e7cd | 2016-01-14 14:02:32 -0800 | [diff] [blame] | 50 | this.id = checkNotNull(id); |
| 51 | this.members = ImmutableSet.copyOf(members); |
| 52 | } |
| 53 | |
Ray Milkey | cc6310f | 2016-02-12 19:02:10 -0800 | [diff] [blame] | 54 | /** |
| 55 | * Constructs a partition that is a copy of another. |
| 56 | * |
| 57 | * @param other partition to copy |
| 58 | */ |
Madan Jampani | fa24218 | 2016-01-22 13:42:54 -0800 | [diff] [blame] | 59 | public DefaultPartition(Partition other) { |
| 60 | this.id = checkNotNull(other.getId()); |
| 61 | this.members = ImmutableSet.copyOf(other.getMembers()); |
| 62 | } |
| 63 | |
Madan Jampani | ab7e7cd | 2016-01-14 14:02:32 -0800 | [diff] [blame] | 64 | @Override |
| 65 | public PartitionId getId() { |
Jordan Halterman | 07f052b | 2017-10-08 14:22:41 -0700 | [diff] [blame] | 66 | return id; |
| 67 | } |
| 68 | |
| 69 | @Override |
Madan Jampani | ab7e7cd | 2016-01-14 14:02:32 -0800 | [diff] [blame] | 70 | public Collection<NodeId> getMembers() { |
Jordan Halterman | 07f052b | 2017-10-08 14:22:41 -0700 | [diff] [blame] | 71 | return members; |
Madan Jampani | ab7e7cd | 2016-01-14 14:02:32 -0800 | [diff] [blame] | 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 Milkey | cc6310f | 2016-02-12 19:02:10 -0800 | [diff] [blame] | 96 | } |