blob: 81b28ac4bd3d20b4f87dcd3c167b0ff6610d4d73 [file] [log] [blame]
Madan Jampani64ff13b2016-09-08 11:18:50 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Madan Jampani64ff13b2016-09-08 11:18:50 -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.Optional;
19
20import com.google.common.base.MoreObjects;
21
22/**
23 * A document tree modification event.
24 *
25 * @param <V> tree node value type
26 */
27public 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 Punnasseryc3658c72017-08-07 11:16:08 -070047 DELETED,
48 TRANSACTION_START,
49 TRANSACTION_END
Madan Jampani64ff13b2016-09-08 11:18:50 -070050 }
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 Jampani79924fa2016-09-13 13:57:03 -070057 @SuppressWarnings("unused")
58 private DocumentTreeEvent() {
59 this.path = null;
60 this.type = null;
61 this.newValue = null;
62 this.oldValue = null;
63 }
64
Madan Jampani64ff13b2016-09-08 11:18:50 -070065 /**
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 Punnasseryc3658c72017-08-07 11:16:08 -070082 /**
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 Jampani64ff13b2016-09-08 11:18:50 -070098
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}