blob: 818e749cf5dfded3359cad3c809f27156fb01ca8 [file] [log] [blame]
Madan Jampani619453b2015-07-22 23:47:09 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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) {
Madan Jampani50589ac2015-06-08 11:38:46 -070065 this.name = name;
Madan Jampani50589ac2015-06-08 11:38:46 -070066 this.key = key;
Madan Jampanif95290a2016-01-27 21:06:11 -080067 this.newValue = currentValue;
68 this.oldValue = previousValue;
69 this.type = currentValue != null ?
70 previousValue != null ? Type.UPDATE : Type.INSERT : Type.REMOVE;
Madan Jampani50589ac2015-06-08 11:38:46 -070071 }
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 Jampanicab114c2015-07-23 00:14:19 -070085 * @return the type of event
Madan Jampani50589ac2015-06-08 11:38:46 -070086 */
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 Milkeyea125322016-02-16 13:35:09 -0800106 * @deprecated 1.5.0 Falcon release. Use {@link #newValue()} or {@link #oldValue()} instead.
Madan Jampani50589ac2015-06-08 11:38:46 -0700107 */
Madan Jampanif95290a2016-01-27 21:06:11 -0800108 @Deprecated
Madan Jampani50589ac2015-06-08 11:38:46 -0700109 public Versioned<V> value() {
Madan Jampanif95290a2016-01-27 21:06:11 -0800110 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 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}