Madan Jampani | 619453b | 2015-07-22 23:47:09 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Madan Jampani | 619453b | 2015-07-22 23:47:09 -0700 | [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 | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 16 | package org.onosproject.store.service; |
| 17 | |
| 18 | import java.util.Objects; |
| 19 | |
| 20 | import com.google.common.base.MoreObjects; |
| 21 | |
| 22 | /** |
| 23 | * Representation of a ConsistentMap update notification. |
| 24 | * |
| 25 | * @param <K> key type |
| 26 | * @param <V> value type |
| 27 | */ |
| 28 | public class MapEvent<K, V> { |
| 29 | |
| 30 | /** |
| 31 | * MapEvent type. |
| 32 | */ |
| 33 | public enum Type { |
| 34 | /** |
| 35 | * Entry inserted into the map. |
| 36 | */ |
| 37 | INSERT, |
| 38 | |
| 39 | /** |
| 40 | * Existing map entry updated. |
| 41 | */ |
| 42 | UPDATE, |
| 43 | |
| 44 | /** |
| 45 | * Entry removed from map. |
| 46 | */ |
| 47 | REMOVE |
| 48 | } |
| 49 | |
| 50 | private final String name; |
| 51 | private final Type type; |
| 52 | private final K key; |
Madan Jampani | f95290a | 2016-01-27 21:06:11 -0800 | [diff] [blame] | 53 | private final Versioned<V> newValue; |
| 54 | private final Versioned<V> oldValue; |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 55 | |
| 56 | /** |
| 57 | * Creates a new event object. |
| 58 | * |
| 59 | * @param name map name |
Madan Jampani | cab114c | 2015-07-23 00:14:19 -0700 | [diff] [blame] | 60 | * @param key key the event concerns |
Madan Jampani | f95290a | 2016-01-27 21:06:11 -0800 | [diff] [blame] | 61 | * @param currentValue new value key is mapped to |
| 62 | * @param previousValue value that was replaced |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 63 | */ |
Madan Jampani | f95290a | 2016-01-27 21:06:11 -0800 | [diff] [blame] | 64 | public MapEvent(String name, K key, Versioned<V> currentValue, Versioned<V> previousValue) { |
Jordan Halterman | 71635ae | 2017-07-28 10:35:43 -0700 | [diff] [blame] | 65 | this(currentValue != null ? previousValue != null ? Type.UPDATE : Type.INSERT : Type.REMOVE, |
| 66 | name, key, currentValue, previousValue); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Creates a new event object. |
| 71 | * |
| 72 | * @param type event type |
| 73 | * @param name map name |
| 74 | * @param key key the event concerns |
| 75 | * @param currentValue new value key is mapped to |
| 76 | * @param previousValue value that was replaced |
| 77 | */ |
| 78 | public MapEvent(Type type, String name, K key, Versioned<V> currentValue, Versioned<V> previousValue) { |
| 79 | this.type = type; |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 80 | this.name = name; |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 81 | this.key = key; |
Madan Jampani | f95290a | 2016-01-27 21:06:11 -0800 | [diff] [blame] | 82 | this.newValue = currentValue; |
| 83 | this.oldValue = previousValue; |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Returns the map name. |
| 88 | * |
| 89 | * @return name of map |
| 90 | */ |
| 91 | public String name() { |
| 92 | return name; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Returns the type of the event. |
| 97 | * |
Madan Jampani | cab114c | 2015-07-23 00:14:19 -0700 | [diff] [blame] | 98 | * @return the type of event |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 99 | */ |
| 100 | public Type type() { |
| 101 | return type; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Returns the key this event concerns. |
| 106 | * |
| 107 | * @return the key |
| 108 | */ |
| 109 | public K key() { |
| 110 | return key; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Returns the value associated with this event. If type is REMOVE, |
| 115 | * this is the value that was removed. If type is INSERT/UPDATE, this is |
| 116 | * the new value. |
| 117 | * |
| 118 | * @return the value |
Ray Milkey | ea12532 | 2016-02-16 13:35:09 -0800 | [diff] [blame] | 119 | * @deprecated 1.5.0 Falcon release. Use {@link #newValue()} or {@link #oldValue()} instead. |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 120 | */ |
Madan Jampani | f95290a | 2016-01-27 21:06:11 -0800 | [diff] [blame] | 121 | @Deprecated |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 122 | public Versioned<V> value() { |
Madan Jampani | f95290a | 2016-01-27 21:06:11 -0800 | [diff] [blame] | 123 | return type == Type.REMOVE ? oldValue() : newValue(); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Returns the new value in the map associated with the key. If {@link #type()} returns {@code REMOVE}, |
| 128 | * this method will return {@code null}. |
| 129 | * |
| 130 | * @return the new value for key |
| 131 | */ |
| 132 | public Versioned<V> newValue() { |
| 133 | return newValue; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Returns the value associated with the key, before it was updated. |
| 138 | * |
| 139 | * @return previous value in map for the key |
| 140 | */ |
| 141 | public Versioned<V> oldValue() { |
| 142 | return oldValue; |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | @Override |
| 146 | public boolean equals(Object o) { |
| 147 | if (!(o instanceof MapEvent)) { |
| 148 | return false; |
| 149 | } |
| 150 | |
| 151 | MapEvent<K, V> that = (MapEvent) o; |
| 152 | return Objects.equals(this.name, that.name) && |
| 153 | Objects.equals(this.type, that.type) && |
| 154 | Objects.equals(this.key, that.key) && |
Madan Jampani | f95290a | 2016-01-27 21:06:11 -0800 | [diff] [blame] | 155 | Objects.equals(this.newValue, that.newValue) && |
| 156 | Objects.equals(this.oldValue, that.oldValue); |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | @Override |
| 160 | public int hashCode() { |
Madan Jampani | f95290a | 2016-01-27 21:06:11 -0800 | [diff] [blame] | 161 | return Objects.hash(name, type, key, newValue, oldValue); |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | @Override |
| 165 | public String toString() { |
| 166 | return MoreObjects.toStringHelper(getClass()) |
| 167 | .add("name", name) |
| 168 | .add("type", type) |
| 169 | .add("key", key) |
Madan Jampani | f95290a | 2016-01-27 21:06:11 -0800 | [diff] [blame] | 170 | .add("newValue", newValue) |
| 171 | .add("oldValue", oldValue) |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 172 | .toString(); |
| 173 | } |
| 174 | } |