blob: 6e628c0d0e624b106aa2575aff0c5bf264c477de [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 Punnassery61a80252017-08-07 11:16:08 -070047 DELETED,
Yuta HIGUCHI6b2ec282017-11-03 21:08:38 -070048 /**
49 * Signifies beginning of Transaction events.
50 *
51 * Note: {@link DocumentTreeEvent#path()} will contain
52 * single element representing TransactionId.
53 */
Sithara Punnassery61a80252017-08-07 11:16:08 -070054 TRANSACTION_START,
Yuta HIGUCHI6b2ec282017-11-03 21:08:38 -070055 /**
56 * Signifies end of Transaction events.
57 *
58 * Note: {@link DocumentTreeEvent#path()} will contain
59 * single element representing TransactionId.
60 */
Sithara Punnassery61a80252017-08-07 11:16:08 -070061 TRANSACTION_END
Madan Jampani64ff13b2016-09-08 11:18:50 -070062 }
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 Jampani79924fa2016-09-13 13:57:03 -070069 @SuppressWarnings("unused")
70 private DocumentTreeEvent() {
71 this.path = null;
72 this.type = null;
73 this.newValue = null;
74 this.oldValue = null;
75 }
76
Madan Jampani64ff13b2016-09-08 11:18:50 -070077 /**
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 HIGUCHI6b2ec282017-11-03 21:08:38 -070094
Sithara Punnassery61a80252017-08-07 11:16:08 -070095 /**
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 Jampani64ff13b2016-09-08 11:18:50 -0700111
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}