Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 1 | package org.onosproject.store.service; |
| 2 | |
| 3 | import java.util.Objects; |
| 4 | |
| 5 | import com.google.common.base.MoreObjects; |
| 6 | |
| 7 | /** |
| 8 | * Representation of a ConsistentMap update notification. |
| 9 | * |
| 10 | * @param <K> key type |
| 11 | * @param <V> value type |
| 12 | */ |
| 13 | public class MapEvent<K, V> { |
| 14 | |
| 15 | /** |
| 16 | * MapEvent type. |
| 17 | */ |
| 18 | public enum Type { |
| 19 | /** |
| 20 | * Entry inserted into the map. |
| 21 | */ |
| 22 | INSERT, |
| 23 | |
| 24 | /** |
| 25 | * Existing map entry updated. |
| 26 | */ |
| 27 | UPDATE, |
| 28 | |
| 29 | /** |
| 30 | * Entry removed from map. |
| 31 | */ |
| 32 | REMOVE |
| 33 | } |
| 34 | |
| 35 | private final String name; |
| 36 | private final Type type; |
| 37 | private final K key; |
| 38 | private final Versioned<V> value; |
| 39 | |
| 40 | /** |
| 41 | * Creates a new event object. |
| 42 | * |
| 43 | * @param name map name |
| 44 | * @param type the type of the event |
| 45 | * @param key the key the event concerns |
| 46 | * @param value the value related to the key, or null for remove events |
| 47 | */ |
| 48 | public MapEvent(String name, Type type, K key, Versioned<V> value) { |
| 49 | this.name = name; |
| 50 | this.type = type; |
| 51 | this.key = key; |
| 52 | this.value = value; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Returns the map name. |
| 57 | * |
| 58 | * @return name of map |
| 59 | */ |
| 60 | public String name() { |
| 61 | return name; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Returns the type of the event. |
| 66 | * |
| 67 | * @return the type of the event |
| 68 | */ |
| 69 | public Type type() { |
| 70 | return type; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Returns the key this event concerns. |
| 75 | * |
| 76 | * @return the key |
| 77 | */ |
| 78 | public K key() { |
| 79 | return key; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Returns the value associated with this event. If type is REMOVE, |
| 84 | * this is the value that was removed. If type is INSERT/UPDATE, this is |
| 85 | * the new value. |
| 86 | * |
| 87 | * @return the value |
| 88 | */ |
| 89 | public Versioned<V> value() { |
| 90 | return value; |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public boolean equals(Object o) { |
| 95 | if (!(o instanceof MapEvent)) { |
| 96 | return false; |
| 97 | } |
| 98 | |
| 99 | MapEvent<K, V> that = (MapEvent) o; |
| 100 | return Objects.equals(this.name, that.name) && |
| 101 | Objects.equals(this.type, that.type) && |
| 102 | Objects.equals(this.key, that.key) && |
| 103 | Objects.equals(this.value, that.value); |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public int hashCode() { |
| 108 | return Objects.hash(type, key, value); |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public String toString() { |
| 113 | return MoreObjects.toStringHelper(getClass()) |
| 114 | .add("name", name) |
| 115 | .add("type", type) |
| 116 | .add("key", key) |
| 117 | .add("value", value) |
| 118 | .toString(); |
| 119 | } |
| 120 | } |