blob: f37954fe346f7cb2ee2d0d21288a41d13e1ea932 [file] [log] [blame]
Madan Jampani64ff13b2016-09-08 11:18:50 -07001/*
2 * Copyright 2016-present Open Networking Laboratory
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 */
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 */
47 DELETED
48 }
49
50 private final DocumentPath path;
51 private final Type type;
52 private final Optional<Versioned<V>> newValue;
53 private final Optional<Versioned<V>> oldValue;
54
Madan Jampani79924fa2016-09-13 13:57:03 -070055 @SuppressWarnings("unused")
56 private DocumentTreeEvent() {
57 this.path = null;
58 this.type = null;
59 this.newValue = null;
60 this.oldValue = null;
61 }
62
Madan Jampani64ff13b2016-09-08 11:18:50 -070063 /**
64 * Constructs a new {@code DocumentTreeEvent}.
65 *
66 * @param path path to the node
67 * @param type type of change
68 * @param newValue optional new value; will be empty if node was deleted
69 * @param oldValue optional old value; will be empty if node was created
70 */
71 public DocumentTreeEvent(DocumentPath path,
72 Type type,
73 Optional<Versioned<V>> newValue,
74 Optional<Versioned<V>> oldValue) {
75 this.path = path;
76 this.type = type;
77 this.newValue = newValue;
78 this.oldValue = oldValue;
79 }
80
81 /**
82 * Returns the path to the changed node.
83 *
84 * @return node path
85 */
86 public DocumentPath path() {
87 return path;
88 }
89
90 /**
91 * Returns the change type.
92 * @return change type
93 */
94 public Type type() {
95 return type;
96 }
97
98 /**
99 * Returns the new value.
100 *
101 * @return optional new value; will be empty if node was deleted
102 */
103 public Optional<Versioned<V>> newValue() {
104 return newValue;
105 }
106
107 /**
108 * Returns the old value.
109 *
110 * @return optional old value; will be empty if node was created
111 */
112 public Optional<Versioned<V>> oldValue() {
113 return oldValue;
114 }
115
116 @Override
117 public String toString() {
118 return MoreObjects.toStringHelper(getClass())
119 .add("path", path)
120 .add("type", type)
121 .add("newValue", newValue)
122 .add("oldValue", oldValue)
123 .toString();
124 }
125}