Jonathan Hart | 46bf89b | 2017-02-27 15:56:42 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Jonathan Hart | 46bf89b | 2017-02-27 15:56:42 -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 | */ |
| 16 | package org.onosproject.store.service; |
| 17 | |
| 18 | import com.google.common.base.MoreObjects; |
| 19 | |
| 20 | import java.util.Objects; |
| 21 | |
| 22 | /** |
| 23 | * Representation of a ConsistentMultimap update notification. |
| 24 | * |
| 25 | * @param <K> key type |
| 26 | * @param <V> value type |
| 27 | */ |
| 28 | public class MultimapEvent<K, V> { |
| 29 | |
| 30 | /** |
| 31 | * MultimapEvent type. |
| 32 | */ |
| 33 | public enum Type { |
| 34 | /** |
| 35 | * Entry inserted into the map. |
| 36 | */ |
| 37 | INSERT, |
| 38 | |
| 39 | /** |
| 40 | * Entry removed from map. |
| 41 | */ |
| 42 | REMOVE |
| 43 | } |
| 44 | |
| 45 | private final String name; |
| 46 | private final Type type; |
| 47 | private final K key; |
| 48 | private final V newValue; |
| 49 | private final V oldValue; |
| 50 | |
| 51 | /** |
| 52 | * Creates a new event object. |
| 53 | * |
| 54 | * @param name map name |
| 55 | * @param key key the event concerns |
| 56 | * @param newValue new value key is mapped to |
| 57 | * @param oldValue previous value that was mapped to the key |
| 58 | */ |
| 59 | public MultimapEvent(String name, K key, V newValue, V oldValue) { |
| 60 | this.name = name; |
| 61 | this.key = key; |
| 62 | this.newValue = newValue; |
| 63 | this.oldValue = oldValue; |
| 64 | this.type = newValue != null ? Type.INSERT : Type.REMOVE; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Returns the map name. |
| 69 | * |
| 70 | * @return name of map |
| 71 | */ |
| 72 | public String name() { |
| 73 | return name; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Returns the type of the event. |
| 78 | * |
| 79 | * @return the type of event |
| 80 | */ |
| 81 | public Type type() { |
| 82 | return type; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Returns the key this event concerns. |
| 87 | * |
| 88 | * @return the key |
| 89 | */ |
| 90 | public K key() { |
| 91 | return key; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Returns the new value in the map associated with the key. |
| 96 | * If {@link #type()} returns {@code REMOVE}, |
| 97 | * this method will return {@code null}. |
| 98 | * |
| 99 | * @return the new value for key |
| 100 | */ |
| 101 | public V newValue() { |
| 102 | return newValue; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Returns the old value that was associated with the key. |
| 107 | * If {@link #type()} returns {@code INSERT}, this method will return |
| 108 | * {@code null}. |
| 109 | * |
| 110 | * @return the old value that was mapped to the key |
| 111 | */ |
| 112 | public V oldValue() { |
| 113 | return oldValue; |
| 114 | } |
| 115 | |
| 116 | @Override |
| 117 | public boolean equals(Object o) { |
| 118 | if (!(o instanceof MultimapEvent)) { |
| 119 | return false; |
| 120 | } |
| 121 | |
| 122 | MultimapEvent<K, V> that = (MultimapEvent) o; |
| 123 | return Objects.equals(this.name, that.name) && |
| 124 | Objects.equals(this.type, that.type) && |
| 125 | Objects.equals(this.key, that.key) && |
| 126 | Objects.equals(this.newValue, that.newValue) && |
| 127 | Objects.equals(this.oldValue, that.oldValue); |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public int hashCode() { |
| 132 | return Objects.hash(name, type, key, newValue, oldValue); |
| 133 | } |
| 134 | |
| 135 | @Override |
| 136 | public String toString() { |
| 137 | return MoreObjects.toStringHelper(getClass()) |
| 138 | .add("name", name) |
| 139 | .add("type", type) |
| 140 | .add("key", key) |
| 141 | .add("newValue", newValue) |
| 142 | .add("oldValue", oldValue) |
| 143 | .toString(); |
| 144 | } |
| 145 | } |