blob: cfb5460011b4ca19bf97686e5ed243c4a8ab36ac [file] [log] [blame]
Madan Jampani762246d2015-07-21 15:40:59 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Madan Jampani762246d2015-07-21 15:40:59 -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 */
16package org.onosproject.store.service;
17
18import java.util.Objects;
19
20import com.google.common.base.MoreObjects;
21
22/**
23 * Representation of a AtomicValue update notification.
24 *
25 * @param <V> atomic value type
26 */
Ray Milkey67d53cc2015-07-23 16:32:36 -070027public final class AtomicValueEvent<V> {
Madan Jampani762246d2015-07-21 15:40:59 -070028
29 /**
30 * AtomicValueEvent type.
31 */
32 public enum Type {
33
34 /**
35 * Value was updated.
36 */
37 UPDATE,
38 }
39
40 private final String name;
Madan Jampani533ef982016-02-01 00:16:35 -080041 private final V newValue;
42 private final V oldValue;
Madan Jampani762246d2015-07-21 15:40:59 -070043
44 /**
45 * Creates a new event object.
46 *
47 * @param name AtomicValue name
Madan Jampani533ef982016-02-01 00:16:35 -080048 * @param newValue the new value
49 * @param oldValue the old value
Madan Jampani762246d2015-07-21 15:40:59 -070050 */
Madan Jampani533ef982016-02-01 00:16:35 -080051 public AtomicValueEvent(String name, V newValue, V oldValue) {
Madan Jampani762246d2015-07-21 15:40:59 -070052 this.name = name;
Madan Jampani533ef982016-02-01 00:16:35 -080053 this.newValue = newValue;
54 this.oldValue = oldValue;
Madan Jampani762246d2015-07-21 15:40:59 -070055 }
56
57 /**
58 * Returns the AtomicValue name.
59 *
60 * @return name of atomic value
61 */
62 public String name() {
63 return name;
64 }
65
66 /**
67 * Returns the type of the event.
68 *
69 * @return the type of the event
70 */
71 public Type type() {
Madan Jampani533ef982016-02-01 00:16:35 -080072 return AtomicValueEvent.Type.UPDATE;
Madan Jampani762246d2015-07-21 15:40:59 -070073 }
74
75 /**
Madan Jampani533ef982016-02-01 00:16:35 -080076 * Returns the newly set value.
Madan Jampani762246d2015-07-21 15:40:59 -070077 *
Madan Jampani533ef982016-02-01 00:16:35 -080078 * @return the new value
Madan Jampani762246d2015-07-21 15:40:59 -070079 */
Madan Jampani533ef982016-02-01 00:16:35 -080080 public V newValue() {
81 return newValue;
82 }
83
84 /**
85 * Returns the old replaced value.
86 *
87 * @return the old value
88 */
89 public V oldValue() {
90 return oldValue;
Madan Jampani762246d2015-07-21 15:40:59 -070091 }
92
93 @Override
94 public boolean equals(Object o) {
95 if (!(o instanceof AtomicValueEvent)) {
96 return false;
97 }
98
Ray Milkey67d53cc2015-07-23 16:32:36 -070099 AtomicValueEvent that = (AtomicValueEvent) o;
Madan Jampani762246d2015-07-21 15:40:59 -0700100 return Objects.equals(this.name, that.name) &&
Madan Jampani533ef982016-02-01 00:16:35 -0800101 Objects.equals(this.newValue, that.newValue) &&
102 Objects.equals(this.oldValue, that.oldValue);
Madan Jampani762246d2015-07-21 15:40:59 -0700103 }
104
105 @Override
106 public int hashCode() {
Madan Jampani533ef982016-02-01 00:16:35 -0800107 return Objects.hash(name, newValue, oldValue);
Madan Jampani762246d2015-07-21 15:40:59 -0700108 }
109
110 @Override
111 public String toString() {
112 return MoreObjects.toStringHelper(getClass())
113 .add("name", name)
Madan Jampani533ef982016-02-01 00:16:35 -0800114 .add("newValue", newValue)
115 .add("oldValue", oldValue)
Madan Jampani762246d2015-07-21 15:40:59 -0700116 .toString();
117 }
Ray Milkey67d53cc2015-07-23 16:32:36 -0700118}