Madan Jampani | 1807057 | 2016-02-29 13:54:45 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 | |
| 18 | import java.util.Objects; |
| 19 | import java.util.Set; |
| 20 | |
| 21 | import com.google.common.base.MoreObjects; |
| 22 | import com.google.common.collect.ImmutableSet; |
| 23 | import com.google.common.collect.Sets; |
| 24 | |
| 25 | /** |
| 26 | * Utility for examining differences between two {@link Partition partition} values. |
| 27 | */ |
| 28 | public class PartitionDiff { |
| 29 | |
| 30 | private final Partition oldValue; |
| 31 | private final Partition newValue; |
| 32 | private final PartitionId partitionId; |
| 33 | private final Set<NodeId> currentMembers; |
| 34 | private final Set<NodeId> newMembers; |
| 35 | |
| 36 | public PartitionDiff(Partition oldValue, Partition newValue) { |
| 37 | this.oldValue = oldValue; |
| 38 | this.newValue = newValue; |
| 39 | this.partitionId = oldValue.getId(); |
| 40 | this.currentMembers = oldValue == null ? ImmutableSet.of() : ImmutableSet.copyOf(oldValue.getMembers()); |
| 41 | this.newMembers = newValue == null ? ImmutableSet.of() : ImmutableSet.copyOf(newValue.getMembers()); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Returns the new partition identifier. |
| 46 | * @return partition id |
| 47 | */ |
| 48 | public PartitionId partitionId() { |
| 49 | return partitionId; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Returns the old partition value. |
| 54 | * @return partition |
| 55 | */ |
| 56 | public Partition oldValue() { |
| 57 | return oldValue; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Returns the new partition value. |
| 62 | * @return partition |
| 63 | */ |
| 64 | public Partition newValue() { |
| 65 | return newValue; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Returns if there are differences between the two values. |
| 70 | * @return {@code true} if yes; {@code false} otherwise |
| 71 | */ |
| 72 | public boolean hasChanged() { |
| 73 | return !Sets.symmetricDifference(currentMembers, newMembers).isEmpty(); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Returns if the specified node is introduced in the new value. |
| 78 | * @param nodeId node identifier |
| 79 | * @return {@code true} if yes; {@code false} otherwise |
| 80 | */ |
| 81 | public boolean isAdded(NodeId nodeId) { |
| 82 | return !currentMembers.contains(nodeId) && newMembers.contains(nodeId); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Returns if the specified node is removed in the new value. |
| 87 | * @param nodeId node identifier |
| 88 | * @return {@code true} if yes; {@code false} otherwise |
| 89 | */ |
| 90 | public boolean isRemoved(NodeId nodeId) { |
| 91 | return currentMembers.contains(nodeId) && !newMembers.contains(nodeId); |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public int hashCode() { |
| 96 | return Objects.hash(oldValue, newValue); |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public boolean equals(Object other) { |
| 101 | if (other == null || !(other instanceof PartitionDiff)) { |
| 102 | return false; |
| 103 | } |
| 104 | PartitionDiff that = (PartitionDiff) other; |
| 105 | return Objects.equals(this.oldValue, that.oldValue) && |
| 106 | Objects.equals(this.newValue, that.newValue); |
| 107 | |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public String toString() { |
| 112 | return MoreObjects.toStringHelper(getClass()) |
| 113 | .add("oldValue", oldValue) |
| 114 | .add("newValue", newValue) |
| 115 | .toString(); |
| 116 | } |
| 117 | } |