blob: e2258afeb6acf0c08479da40785434c652d4794c [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 /**
Madan Jampanif95290a2016-01-27 21:06:11 -0800114 * 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 Jampani50589ac2015-06-08 11:38:46 -0700130 }
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 Jampanif95290a2016-01-27 21:06:11 -0800142 Objects.equals(this.newValue, that.newValue) &&
143 Objects.equals(this.oldValue, that.oldValue);
Madan Jampani50589ac2015-06-08 11:38:46 -0700144 }
145
146 @Override
147 public int hashCode() {
Madan Jampanif95290a2016-01-27 21:06:11 -0800148 return Objects.hash(name, type, key, newValue, oldValue);
Madan Jampani50589ac2015-06-08 11:38:46 -0700149 }
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 Jampanif95290a2016-01-27 21:06:11 -0800157 .add("newValue", newValue)
158 .add("oldValue", oldValue)
Madan Jampani50589ac2015-06-08 11:38:46 -0700159 .toString();
160 }
161}