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 | 61a8025 | 2017-08-07 11:16:08 -0700 | [diff] [blame] | 47 | DELETED, |
Yuta HIGUCHI | 6b2ec28 | 2017-11-03 21:08:38 -0700 | [diff] [blame] | 48 | /** |
| 49 | * Signifies beginning of Transaction events. |
| 50 | * |
| 51 | * Note: {@link DocumentTreeEvent#path()} will contain |
| 52 | * single element representing TransactionId. |
| 53 | */ |
Sithara Punnassery | 61a8025 | 2017-08-07 11:16:08 -0700 | [diff] [blame] | 54 | TRANSACTION_START, |
Yuta HIGUCHI | 6b2ec28 | 2017-11-03 21:08:38 -0700 | [diff] [blame] | 55 | /** |
| 56 | * Signifies end of Transaction events. |
| 57 | * |
| 58 | * Note: {@link DocumentTreeEvent#path()} will contain |
| 59 | * single element representing TransactionId. |
| 60 | */ |
Sithara Punnassery | 61a8025 | 2017-08-07 11:16:08 -0700 | [diff] [blame] | 61 | TRANSACTION_END |
Madan Jampani | 64ff13b | 2016-09-08 11:18:50 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | private final DocumentPath path; |
| 65 | private final Type type; |
| 66 | private final Optional<Versioned<V>> newValue; |
| 67 | private final Optional<Versioned<V>> oldValue; |
| 68 | |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 69 | @SuppressWarnings("unused") |
| 70 | private DocumentTreeEvent() { |
| 71 | this.path = null; |
| 72 | this.type = null; |
| 73 | this.newValue = null; |
| 74 | this.oldValue = null; |
| 75 | } |
| 76 | |
Madan Jampani | 64ff13b | 2016-09-08 11:18:50 -0700 | [diff] [blame] | 77 | /** |
| 78 | * Constructs a new {@code DocumentTreeEvent}. |
| 79 | * |
| 80 | * @param path path to the node |
| 81 | * @param type type of change |
| 82 | * @param newValue optional new value; will be empty if node was deleted |
| 83 | * @param oldValue optional old value; will be empty if node was created |
| 84 | */ |
| 85 | public DocumentTreeEvent(DocumentPath path, |
| 86 | Type type, |
| 87 | Optional<Versioned<V>> newValue, |
| 88 | Optional<Versioned<V>> oldValue) { |
| 89 | this.path = path; |
| 90 | this.type = type; |
| 91 | this.newValue = newValue; |
| 92 | this.oldValue = oldValue; |
| 93 | } |
Yuta HIGUCHI | 6b2ec28 | 2017-11-03 21:08:38 -0700 | [diff] [blame] | 94 | |
Sithara Punnassery | 61a8025 | 2017-08-07 11:16:08 -0700 | [diff] [blame] | 95 | /** |
| 96 | * Constructs a new {@code DocumentTreeEvent}. |
| 97 | * |
| 98 | * @param path path to the node |
| 99 | * @param newValue optional new value; will be empty if node was deleted |
| 100 | * @param oldValue optional old value; will be empty if node was created |
| 101 | */ |
| 102 | public DocumentTreeEvent(DocumentPath path, |
| 103 | Optional<Versioned<V>> newValue, |
| 104 | Optional<Versioned<V>> oldValue) { |
| 105 | this.path = path; |
| 106 | this.newValue = newValue; |
| 107 | this.oldValue = oldValue; |
| 108 | this.type = newValue != null ? |
| 109 | oldValue != null ? Type.UPDATED : Type.CREATED : Type.DELETED; |
| 110 | } |
Madan Jampani | 64ff13b | 2016-09-08 11:18:50 -0700 | [diff] [blame] | 111 | |
| 112 | /** |
| 113 | * Returns the path to the changed node. |
| 114 | * |
| 115 | * @return node path |
| 116 | */ |
| 117 | public DocumentPath path() { |
| 118 | return path; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Returns the change type. |
| 123 | * @return change type |
| 124 | */ |
| 125 | public Type type() { |
| 126 | return type; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Returns the new value. |
| 131 | * |
| 132 | * @return optional new value; will be empty if node was deleted |
| 133 | */ |
| 134 | public Optional<Versioned<V>> newValue() { |
| 135 | return newValue; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Returns the old value. |
| 140 | * |
| 141 | * @return optional old value; will be empty if node was created |
| 142 | */ |
| 143 | public Optional<Versioned<V>> oldValue() { |
| 144 | return oldValue; |
| 145 | } |
| 146 | |
| 147 | @Override |
| 148 | public String toString() { |
| 149 | return MoreObjects.toStringHelper(getClass()) |
| 150 | .add("path", path) |
| 151 | .add("type", type) |
| 152 | .add("newValue", newValue) |
| 153 | .add("oldValue", oldValue) |
| 154 | .toString(); |
| 155 | } |
| 156 | } |