Madan Jampani | 619453b | 2015-07-22 23:47:09 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
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) { |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 65 | this.name = name; |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 66 | this.key = key; |
Madan Jampani | f95290a | 2016-01-27 21:06:11 -0800 | [diff] [blame] | 67 | this.newValue = currentValue; |
| 68 | this.oldValue = previousValue; |
| 69 | this.type = currentValue != null ? |
| 70 | previousValue != null ? Type.UPDATE : Type.INSERT : Type.REMOVE; |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Returns the map name. |
| 75 | * |
| 76 | * @return name of map |
| 77 | */ |
| 78 | public String name() { |
| 79 | return name; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Returns the type of the event. |
| 84 | * |
Madan Jampani | cab114c | 2015-07-23 00:14:19 -0700 | [diff] [blame] | 85 | * @return the type of event |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 86 | */ |
| 87 | public Type type() { |
| 88 | return type; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Returns the key this event concerns. |
| 93 | * |
| 94 | * @return the key |
| 95 | */ |
| 96 | public K key() { |
| 97 | return key; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Returns the value associated with this event. If type is REMOVE, |
| 102 | * this is the value that was removed. If type is INSERT/UPDATE, this is |
| 103 | * the new value. |
| 104 | * |
| 105 | * @return the value |
Ray Milkey | ea12532 | 2016-02-16 13:35:09 -0800 | [diff] [blame] | 106 | * @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] | 107 | */ |
Madan Jampani | f95290a | 2016-01-27 21:06:11 -0800 | [diff] [blame] | 108 | @Deprecated |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 109 | public Versioned<V> value() { |
Madan Jampani | f95290a | 2016-01-27 21:06:11 -0800 | [diff] [blame] | 110 | return type == Type.REMOVE ? oldValue() : newValue(); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Returns the new value in the map associated with the key. If {@link #type()} returns {@code REMOVE}, |
| 115 | * this method will return {@code null}. |
| 116 | * |
| 117 | * @return the new value for key |
| 118 | */ |
| 119 | public Versioned<V> newValue() { |
| 120 | return newValue; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Returns the value associated with the key, before it was updated. |
| 125 | * |
| 126 | * @return previous value in map for the key |
| 127 | */ |
| 128 | public Versioned<V> oldValue() { |
| 129 | return oldValue; |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public boolean equals(Object o) { |
| 134 | if (!(o instanceof MapEvent)) { |
| 135 | return false; |
| 136 | } |
| 137 | |
| 138 | MapEvent<K, V> that = (MapEvent) o; |
| 139 | return Objects.equals(this.name, that.name) && |
| 140 | Objects.equals(this.type, that.type) && |
| 141 | Objects.equals(this.key, that.key) && |
Madan Jampani | f95290a | 2016-01-27 21:06:11 -0800 | [diff] [blame] | 142 | Objects.equals(this.newValue, that.newValue) && |
| 143 | Objects.equals(this.oldValue, that.oldValue); |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public int hashCode() { |
Madan Jampani | f95290a | 2016-01-27 21:06:11 -0800 | [diff] [blame] | 148 | return Objects.hash(name, type, key, newValue, oldValue); |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public String toString() { |
| 153 | return MoreObjects.toStringHelper(getClass()) |
| 154 | .add("name", name) |
| 155 | .add("type", type) |
| 156 | .add("key", key) |
Madan Jampani | f95290a | 2016-01-27 21:06:11 -0800 | [diff] [blame] | 157 | .add("newValue", newValue) |
| 158 | .add("oldValue", oldValue) |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 159 | .toString(); |
| 160 | } |
| 161 | } |