blob: 0f7247ab6e4293b9ccfcae8b432462d626797c17 [file] [log] [blame]
Jonathan Hartdb3af892015-01-26 13:19:07 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Jonathan Hartdb3af892015-01-26 13:19:07 -08003 *
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 */
Jonathan Hart6ec029a2015-03-24 17:12:35 -070016package org.onosproject.store.service;
Jonathan Hartdb3af892015-01-26 13:19:07 -080017
Jonathan Hart584d2f32015-01-27 19:46:14 -080018import com.google.common.base.MoreObjects;
19
20import java.util.Objects;
21
Jonathan Hartdb3af892015-01-26 13:19:07 -080022/**
Madan Jampanicab114c2015-07-23 00:14:19 -070023 * Representation of a EventuallyConsistentMap update notification.
Jonathan Hartdb3af892015-01-26 13:19:07 -080024 */
Ray Milkeyd083bc42015-07-22 16:34:22 -070025public final class EventuallyConsistentMapEvent<K, V> {
Jonathan Hartdb3af892015-01-26 13:19:07 -080026
27 public enum Type {
Madan Jampanicab114c2015-07-23 00:14:19 -070028 /**
29 * Entry added to map or existing entry updated.
30 */
Jonathan Hartdb3af892015-01-26 13:19:07 -080031 PUT,
Madan Jampanicab114c2015-07-23 00:14:19 -070032
33 /**
34 * Entry removed from map.
35 */
Jonathan Hartdb3af892015-01-26 13:19:07 -080036 REMOVE
37 }
38
Madan Jampanicab114c2015-07-23 00:14:19 -070039 private final String name;
Jonathan Hartdb3af892015-01-26 13:19:07 -080040 private final Type type;
41 private final K key;
42 private final V value;
43
44 /**
45 * Creates a new event object.
46 *
Madan Jampanicab114c2015-07-23 00:14:19 -070047 * @param name map name
Jonathan Hartdb3af892015-01-26 13:19:07 -080048 * @param type the type of the event
49 * @param key the key the event concerns
Madan Jampanicab114c2015-07-23 00:14:19 -070050 * @param value the value mapped to the key
Jonathan Hartdb3af892015-01-26 13:19:07 -080051 */
Madan Jampanicab114c2015-07-23 00:14:19 -070052 public EventuallyConsistentMapEvent(String name, Type type, K key, V value) {
53 this.name = name;
Jonathan Hartdb3af892015-01-26 13:19:07 -080054 this.type = type;
55 this.key = key;
56 this.value = value;
57 }
58
59 /**
Madan Jampanicab114c2015-07-23 00:14:19 -070060 * Returns the map name.
61 *
62 * @return name of map
63 */
64 public String name() {
65 return name;
66 }
67
68 /**
Jonathan Hartdb3af892015-01-26 13:19:07 -080069 * Returns the type of the event.
70 *
71 * @return the type of the event
72 */
73 public Type type() {
74 return type;
75 }
76
77 /**
78 * Returns the key this event concerns.
79 *
80 * @return the key
81 */
82 public K key() {
83 return key;
84 }
85
86 /**
Madan Jampanicab114c2015-07-23 00:14:19 -070087 * Returns the value associated with this event. If type is REMOVE,
88 * this is the value that was removed. If type is PUT, this is
89 * the new value.
Jonathan Hartdb3af892015-01-26 13:19:07 -080090 *
Madan Jampanicab114c2015-07-23 00:14:19 -070091 * @return the value
Jonathan Hartdb3af892015-01-26 13:19:07 -080092 */
93 public V value() {
94 return value;
95 }
Jonathan Hart584d2f32015-01-27 19:46:14 -080096
97 @Override
98 public boolean equals(Object o) {
99 if (!(o instanceof EventuallyConsistentMapEvent)) {
100 return false;
101 }
102
103 EventuallyConsistentMapEvent that = (EventuallyConsistentMapEvent) o;
104 return Objects.equals(this.type, that.type) &&
105 Objects.equals(this.key, that.key) &&
Ray Milkey92ea9b32015-07-24 09:28:09 -0700106 Objects.equals(this.value, that.value) &&
107 Objects.equals(this.name, that.name);
Jonathan Hart584d2f32015-01-27 19:46:14 -0800108 }
109
110 @Override
111 public int hashCode() {
112 return Objects.hash(type, key, value);
113 }
114
115 @Override
116 public String toString() {
117 return MoreObjects.toStringHelper(getClass())
Ray Milkey92ea9b32015-07-24 09:28:09 -0700118 .add("name", name)
Jonathan Hart584d2f32015-01-27 19:46:14 -0800119 .add("type", type)
120 .add("key", key)
121 .add("value", value)
122 .toString();
123 }
Jonathan Hartdb3af892015-01-26 13:19:07 -0800124}