Madan Jampani | 619453b | 2015-07-22 23:47:09 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | */ |
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; |
| 53 | private final Versioned<V> value; |
| 54 | |
| 55 | /** |
| 56 | * Creates a new event object. |
| 57 | * |
| 58 | * @param name map name |
Madan Jampani | cab114c | 2015-07-23 00:14:19 -0700 | [diff] [blame] | 59 | * @param type type of event |
| 60 | * @param key key the event concerns |
| 61 | * @param value value key is mapped to |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 62 | */ |
| 63 | public MapEvent(String name, Type type, K key, Versioned<V> value) { |
| 64 | this.name = name; |
| 65 | this.type = type; |
| 66 | this.key = key; |
| 67 | this.value = value; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Returns the map name. |
| 72 | * |
| 73 | * @return name of map |
| 74 | */ |
| 75 | public String name() { |
| 76 | return name; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Returns the type of the event. |
| 81 | * |
Madan Jampani | cab114c | 2015-07-23 00:14:19 -0700 | [diff] [blame] | 82 | * @return the type of event |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 83 | */ |
| 84 | public Type type() { |
| 85 | return type; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Returns the key this event concerns. |
| 90 | * |
| 91 | * @return the key |
| 92 | */ |
| 93 | public K key() { |
| 94 | return key; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Returns the value associated with this event. If type is REMOVE, |
| 99 | * this is the value that was removed. If type is INSERT/UPDATE, this is |
| 100 | * the new value. |
| 101 | * |
| 102 | * @return the value |
| 103 | */ |
| 104 | public Versioned<V> value() { |
| 105 | return value; |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public boolean equals(Object o) { |
| 110 | if (!(o instanceof MapEvent)) { |
| 111 | return false; |
| 112 | } |
| 113 | |
| 114 | MapEvent<K, V> that = (MapEvent) o; |
| 115 | return Objects.equals(this.name, that.name) && |
| 116 | Objects.equals(this.type, that.type) && |
| 117 | Objects.equals(this.key, that.key) && |
| 118 | Objects.equals(this.value, that.value); |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public int hashCode() { |
Madan Jampani | cab114c | 2015-07-23 00:14:19 -0700 | [diff] [blame] | 123 | return Objects.hash(name, type, key, value); |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public String toString() { |
| 128 | return MoreObjects.toStringHelper(getClass()) |
| 129 | .add("name", name) |
| 130 | .add("type", type) |
| 131 | .add("key", key) |
| 132 | .add("value", value) |
| 133 | .toString(); |
| 134 | } |
| 135 | } |