Madan Jampani | 762246d | 2015-07-21 15:40:59 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Madan Jampani | 762246d | 2015-07-21 15:40:59 -0700 | [diff] [blame] | 3 | * |
| 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 | */ |
| 16 | package org.onosproject.store.service; |
| 17 | |
| 18 | import java.util.Objects; |
| 19 | |
| 20 | import com.google.common.base.MoreObjects; |
| 21 | |
| 22 | /** |
| 23 | * Representation of a AtomicValue update notification. |
| 24 | * |
| 25 | * @param <V> atomic value type |
| 26 | */ |
Ray Milkey | 67d53cc | 2015-07-23 16:32:36 -0700 | [diff] [blame] | 27 | public final class AtomicValueEvent<V> { |
Madan Jampani | 762246d | 2015-07-21 15:40:59 -0700 | [diff] [blame] | 28 | |
| 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 Jampani | 533ef98 | 2016-02-01 00:16:35 -0800 | [diff] [blame] | 41 | private final V newValue; |
| 42 | private final V oldValue; |
Madan Jampani | 762246d | 2015-07-21 15:40:59 -0700 | [diff] [blame] | 43 | |
| 44 | /** |
| 45 | * Creates a new event object. |
| 46 | * |
| 47 | * @param name AtomicValue name |
Madan Jampani | 533ef98 | 2016-02-01 00:16:35 -0800 | [diff] [blame] | 48 | * @param newValue the new value |
| 49 | * @param oldValue the old value |
Madan Jampani | 762246d | 2015-07-21 15:40:59 -0700 | [diff] [blame] | 50 | */ |
Madan Jampani | 533ef98 | 2016-02-01 00:16:35 -0800 | [diff] [blame] | 51 | public AtomicValueEvent(String name, V newValue, V oldValue) { |
Madan Jampani | 762246d | 2015-07-21 15:40:59 -0700 | [diff] [blame] | 52 | this.name = name; |
Madan Jampani | 533ef98 | 2016-02-01 00:16:35 -0800 | [diff] [blame] | 53 | this.newValue = newValue; |
| 54 | this.oldValue = oldValue; |
Madan Jampani | 762246d | 2015-07-21 15:40:59 -0700 | [diff] [blame] | 55 | } |
| 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 Jampani | 533ef98 | 2016-02-01 00:16:35 -0800 | [diff] [blame] | 72 | return AtomicValueEvent.Type.UPDATE; |
Madan Jampani | 762246d | 2015-07-21 15:40:59 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | /** |
Madan Jampani | 533ef98 | 2016-02-01 00:16:35 -0800 | [diff] [blame] | 76 | * Returns the newly set value. |
Madan Jampani | 762246d | 2015-07-21 15:40:59 -0700 | [diff] [blame] | 77 | * |
Madan Jampani | 533ef98 | 2016-02-01 00:16:35 -0800 | [diff] [blame] | 78 | * @return the new value |
Madan Jampani | 762246d | 2015-07-21 15:40:59 -0700 | [diff] [blame] | 79 | */ |
Madan Jampani | 533ef98 | 2016-02-01 00:16:35 -0800 | [diff] [blame] | 80 | 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 Jampani | 762246d | 2015-07-21 15:40:59 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public boolean equals(Object o) { |
| 95 | if (!(o instanceof AtomicValueEvent)) { |
| 96 | return false; |
| 97 | } |
| 98 | |
Ray Milkey | 67d53cc | 2015-07-23 16:32:36 -0700 | [diff] [blame] | 99 | AtomicValueEvent that = (AtomicValueEvent) o; |
Madan Jampani | 762246d | 2015-07-21 15:40:59 -0700 | [diff] [blame] | 100 | return Objects.equals(this.name, that.name) && |
Madan Jampani | 533ef98 | 2016-02-01 00:16:35 -0800 | [diff] [blame] | 101 | Objects.equals(this.newValue, that.newValue) && |
| 102 | Objects.equals(this.oldValue, that.oldValue); |
Madan Jampani | 762246d | 2015-07-21 15:40:59 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public int hashCode() { |
Madan Jampani | 533ef98 | 2016-02-01 00:16:35 -0800 | [diff] [blame] | 107 | return Objects.hash(name, newValue, oldValue); |
Madan Jampani | 762246d | 2015-07-21 15:40:59 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public String toString() { |
| 112 | return MoreObjects.toStringHelper(getClass()) |
| 113 | .add("name", name) |
Madan Jampani | 533ef98 | 2016-02-01 00:16:35 -0800 | [diff] [blame] | 114 | .add("newValue", newValue) |
| 115 | .add("oldValue", oldValue) |
Madan Jampani | 762246d | 2015-07-21 15:40:59 -0700 | [diff] [blame] | 116 | .toString(); |
| 117 | } |
Ray Milkey | 67d53cc | 2015-07-23 16:32:36 -0700 | [diff] [blame] | 118 | } |