Madan Jampani | ec1df02 | 2015-10-13 21:23:03 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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 | */ |
| 16 | package org.onosproject.cluster; |
| 17 | |
David K. Bainbridge | e676dab | 2015-10-23 16:13:07 -0700 | [diff] [blame] | 18 | import java.util.Arrays; |
Madan Jampani | ec1df02 | 2015-10-13 21:23:03 -0700 | [diff] [blame] | 19 | import java.util.Collection; |
| 20 | import java.util.Set; |
| 21 | |
| 22 | import com.google.common.collect.ImmutableSet; |
David K. Bainbridge | e676dab | 2015-10-23 16:13:07 -0700 | [diff] [blame] | 23 | import com.google.common.collect.Sets; |
Madan Jampani | ec1df02 | 2015-10-13 21:23:03 -0700 | [diff] [blame] | 24 | |
| 25 | import static com.google.common.base.Preconditions.checkNotNull; |
| 26 | |
| 27 | /** |
| 28 | * A data partition. |
| 29 | * <p> |
| 30 | * Partition represents a slice of the data space and is made up of a collection |
| 31 | * of {@link org.onosproject.cluster.ControllerNode nodes} |
| 32 | * that all maintain copies of this data. |
| 33 | */ |
| 34 | public class Partition { |
| 35 | |
| 36 | private final String name; |
| 37 | private final Set<NodeId> members; |
| 38 | |
| 39 | private Partition() { |
| 40 | name = null; |
| 41 | members = null; |
| 42 | } |
| 43 | |
| 44 | public Partition(String name, Collection<NodeId> members) { |
| 45 | this.name = checkNotNull(name); |
| 46 | this.members = ImmutableSet.copyOf(checkNotNull(members)); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Returns the partition name. |
| 51 | * <p> |
| 52 | * Each partition is identified by a unique name. |
| 53 | * @return partition name |
| 54 | */ |
| 55 | public String getName() { |
| 56 | return this.name; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Returns the collection of controller node identifiers that make up this partition. |
| 61 | * @return collection of controller node identifiers |
| 62 | */ |
| 63 | public Collection<NodeId> getMembers() { |
| 64 | return this.members; |
| 65 | } |
David K. Bainbridge | e676dab | 2015-10-23 16:13:07 -0700 | [diff] [blame] | 66 | |
| 67 | @Override |
| 68 | public int hashCode() { |
| 69 | return Arrays.deepHashCode(new Object[] {name, members}); |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public boolean equals(Object other) { |
| 74 | if (this == other) { |
| 75 | return true; |
| 76 | } |
| 77 | |
| 78 | if (other == null || !Partition.class.isInstance(other)) { |
| 79 | return false; |
| 80 | } |
| 81 | |
| 82 | Partition that = (Partition) other; |
| 83 | |
| 84 | if (!this.name.equals(that.name) || (this.members == null && that.members != null) |
| 85 | || (this.members != null && that.members == null) || this.members.size() != that.members.size()) { |
| 86 | return false; |
| 87 | } |
| 88 | |
| 89 | return Sets.symmetricDifference(this.members, that.members).isEmpty(); |
| 90 | } |
Madan Jampani | ec1df02 | 2015-10-13 21:23:03 -0700 | [diff] [blame] | 91 | } |