blob: ecea9543bad0517c11874b31ac5e6223b1f1fac3 [file] [log] [blame]
Madan Jampani619453b2015-07-22 23:47:09 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Madan Jampani619453b2015-07-22 23:47:09 -07003 *
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 Jampani50589ac2015-06-08 11:38:46 -070016package org.onosproject.store.service;
17
18import java.util.Objects;
19
20import 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 */
28public 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 Jampanif95290a2016-01-27 21:06:11 -080053 private final Versioned<V> newValue;
54 private final Versioned<V> oldValue;
Madan Jampani50589ac2015-06-08 11:38:46 -070055
56 /**
57 * Creates a new event object.
58 *
59 * @param name map name
Madan Jampanicab114c2015-07-23 00:14:19 -070060 * @param key key the event concerns
Madan Jampanif95290a2016-01-27 21:06:11 -080061 * @param currentValue new value key is mapped to
62 * @param previousValue value that was replaced
Madan Jampani50589ac2015-06-08 11:38:46 -070063 */
Madan Jampanif95290a2016-01-27 21:06:11 -080064 public MapEvent(String name, K key, Versioned<V> currentValue, Versioned<V> previousValue) {
Jordan Halterman71635ae2017-07-28 10:35:43 -070065 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 Jampani50589ac2015-06-08 11:38:46 -070080 this.name = name;
Madan Jampani50589ac2015-06-08 11:38:46 -070081 this.key = key;
Madan Jampanif95290a2016-01-27 21:06:11 -080082 this.newValue = currentValue;
83 this.oldValue = previousValue;
Madan Jampani50589ac2015-06-08 11:38:46 -070084 }
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 Jampanicab114c2015-07-23 00:14:19 -070098 * @return the type of event
Madan Jampani50589ac2015-06-08 11:38:46 -070099 */
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 Milkeyea125322016-02-16 13:35:09 -0800119 * @deprecated 1.5.0 Falcon release. Use {@link #newValue()} or {@link #oldValue()} instead.
Madan Jampani50589ac2015-06-08 11:38:46 -0700120 */
Madan Jampanif95290a2016-01-27 21:06:11 -0800121 @Deprecated
Madan Jampani50589ac2015-06-08 11:38:46 -0700122 public Versioned<V> value() {
Madan Jampanif95290a2016-01-27 21:06:11 -0800123 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 Jampani50589ac2015-06-08 11:38:46 -0700143 }
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 Jampanif95290a2016-01-27 21:06:11 -0800155 Objects.equals(this.newValue, that.newValue) &&
156 Objects.equals(this.oldValue, that.oldValue);
Madan Jampani50589ac2015-06-08 11:38:46 -0700157 }
158
159 @Override
160 public int hashCode() {
Madan Jampanif95290a2016-01-27 21:06:11 -0800161 return Objects.hash(name, type, key, newValue, oldValue);
Madan Jampani50589ac2015-06-08 11:38:46 -0700162 }
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 Jampanif95290a2016-01-27 21:06:11 -0800170 .add("newValue", newValue)
171 .add("oldValue", oldValue)
Madan Jampani50589ac2015-06-08 11:38:46 -0700172 .toString();
173 }
174}