Madan Jampani | 64ff13b | 2016-09-08 11:18:50 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Madan Jampani | 64ff13b | 2016-09-08 11:18:50 -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.Optional; |
| 19 | |
| 20 | import com.google.common.base.MoreObjects; |
| 21 | |
| 22 | /** |
| 23 | * A document tree modification event. |
| 24 | * |
| 25 | * @param <V> tree node value type |
| 26 | */ |
| 27 | public class DocumentTreeEvent<V> { |
| 28 | |
| 29 | /** |
| 30 | * Nature of document tree node change. |
| 31 | */ |
| 32 | public enum Type { |
| 33 | |
| 34 | /** |
| 35 | * Signifies node being created. |
| 36 | */ |
| 37 | CREATED, |
| 38 | |
| 39 | /** |
| 40 | * Signifies the value of an existing node being updated. |
| 41 | */ |
| 42 | UPDATED, |
| 43 | |
| 44 | /** |
| 45 | * Signifies an existing node being deleted. |
| 46 | */ |
Sithara Punnassery | c3658c7 | 2017-08-07 11:16:08 -0700 | [diff] [blame] | 47 | DELETED, |
| 48 | TRANSACTION_START, |
| 49 | TRANSACTION_END |
Madan Jampani | 64ff13b | 2016-09-08 11:18:50 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | private final DocumentPath path; |
| 53 | private final Type type; |
| 54 | private final Optional<Versioned<V>> newValue; |
| 55 | private final Optional<Versioned<V>> oldValue; |
| 56 | |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 57 | @SuppressWarnings("unused") |
| 58 | private DocumentTreeEvent() { |
| 59 | this.path = null; |
| 60 | this.type = null; |
| 61 | this.newValue = null; |
| 62 | this.oldValue = null; |
| 63 | } |
| 64 | |
Madan Jampani | 64ff13b | 2016-09-08 11:18:50 -0700 | [diff] [blame] | 65 | /** |
| 66 | * Constructs a new {@code DocumentTreeEvent}. |
| 67 | * |
| 68 | * @param path path to the node |
| 69 | * @param type type of change |
| 70 | * @param newValue optional new value; will be empty if node was deleted |
| 71 | * @param oldValue optional old value; will be empty if node was created |
| 72 | */ |
| 73 | public DocumentTreeEvent(DocumentPath path, |
| 74 | Type type, |
| 75 | Optional<Versioned<V>> newValue, |
| 76 | Optional<Versioned<V>> oldValue) { |
| 77 | this.path = path; |
| 78 | this.type = type; |
| 79 | this.newValue = newValue; |
| 80 | this.oldValue = oldValue; |
| 81 | } |
Sithara Punnassery | c3658c7 | 2017-08-07 11:16:08 -0700 | [diff] [blame] | 82 | /** |
| 83 | * Constructs a new {@code DocumentTreeEvent}. |
| 84 | * |
| 85 | * @param path path to the node |
| 86 | * @param newValue optional new value; will be empty if node was deleted |
| 87 | * @param oldValue optional old value; will be empty if node was created |
| 88 | */ |
| 89 | public DocumentTreeEvent(DocumentPath path, |
| 90 | Optional<Versioned<V>> newValue, |
| 91 | Optional<Versioned<V>> oldValue) { |
| 92 | this.path = path; |
| 93 | this.newValue = newValue; |
| 94 | this.oldValue = oldValue; |
| 95 | this.type = newValue != null ? |
| 96 | oldValue != null ? Type.UPDATED : Type.CREATED : Type.DELETED; |
| 97 | } |
Madan Jampani | 64ff13b | 2016-09-08 11:18:50 -0700 | [diff] [blame] | 98 | |
| 99 | /** |
| 100 | * Returns the path to the changed node. |
| 101 | * |
| 102 | * @return node path |
| 103 | */ |
| 104 | public DocumentPath path() { |
| 105 | return path; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Returns the change type. |
| 110 | * @return change type |
| 111 | */ |
| 112 | public Type type() { |
| 113 | return type; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Returns the new value. |
| 118 | * |
| 119 | * @return optional new value; will be empty if node was deleted |
| 120 | */ |
| 121 | public Optional<Versioned<V>> newValue() { |
| 122 | return newValue; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Returns the old value. |
| 127 | * |
| 128 | * @return optional old value; will be empty if node was created |
| 129 | */ |
| 130 | public Optional<Versioned<V>> oldValue() { |
| 131 | return oldValue; |
| 132 | } |
| 133 | |
| 134 | @Override |
| 135 | public String toString() { |
| 136 | return MoreObjects.toStringHelper(getClass()) |
| 137 | .add("path", path) |
| 138 | .add("type", type) |
| 139 | .add("newValue", newValue) |
| 140 | .add("oldValue", oldValue) |
| 141 | .toString(); |
| 142 | } |
| 143 | } |