blob: 18c50d711278eaa653fb53bf1ff612c702922430 [file] [log] [blame]
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -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 */
16
17package org.onosproject.store.primitives;
18
Madan Jampani1184bc72016-09-08 08:37:05 -070019import static com.google.common.base.Preconditions.checkNotNull;
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070020
21import java.util.Comparator;
22import java.util.Iterator;
23import java.util.Objects;
24import java.util.TreeSet;
25
Madan Jampani1184bc72016-09-08 08:37:05 -070026import org.onosproject.store.service.DocumentPath;
27
28import com.google.common.base.MoreObjects;
29import com.google.common.collect.Sets;
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070030
31/**
Madan Jampani1184bc72016-09-08 08:37:05 -070032 * A {@code DocumentTree} node.
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070033 */
34public class DocumentTreeNode<V> {
35 private final DocumentPath key;
36 private V value;
Madan Jampani1184bc72016-09-08 08:37:05 -070037 private long version;
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070038 private final TreeSet<DocumentTreeNode<V>> children =
39 Sets.newTreeSet(new Comparator<DocumentTreeNode<V>>() {
40 @Override
41 public int compare(DocumentTreeNode<V> o1,
42 DocumentTreeNode<V> o2) {
43 return o1.getKey().compareTo(o2.getKey());
44 }
45 });
Madan Jampani1184bc72016-09-08 08:37:05 -070046 private final DocumentTreeNode<V> parent;
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070047
Madan Jampani1184bc72016-09-08 08:37:05 -070048 public DocumentTreeNode(DocumentPath key,
49 V value,
50 long version,
51 DocumentTreeNode<V> parent) {
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070052 this.key = checkNotNull(key);
53 this.value = checkNotNull(value);
Madan Jampani1184bc72016-09-08 08:37:05 -070054 this.version = version;
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070055 this.parent = parent;
56 }
57
58 /**
Madan Jampani1184bc72016-09-08 08:37:05 -070059 * Returns this node's key.
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070060 *
61 * @return the key
62 */
63 public DocumentPath getKey() {
64 return key;
65 }
66
67 /**
Madan Jampani1184bc72016-09-08 08:37:05 -070068 * Returns this node's value.
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070069 *
70 * @return the value
71 */
72 public V getValue() {
73 return value;
74 }
75
76 /**
Madan Jampani1184bc72016-09-08 08:37:05 -070077 * Returns this node's version.
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070078 *
Madan Jampani1184bc72016-09-08 08:37:05 -070079 * @return the version
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070080 */
Madan Jampani1184bc72016-09-08 08:37:05 -070081 public long getVersion() {
82 return version;
83 }
84
85 /**
86 * Updates this node.
87 *
88 * @param newValue new value to be set
89 * @param newVersion new version to be set
90 */
91 public void update(V newValue, long newVersion) {
92 this.value = newValue;
93 this.version = newVersion;
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070094 }
95
96 /**
97 * Returns a collection of the children of this node.
98 *
Madan Jampani1184bc72016-09-08 08:37:05 -070099 * @return iterator for the children of this node.
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -0700100 */
101 public Iterator<DocumentTreeNode<V>> getChildren() {
102 return children.iterator();
103 }
104
105 /**
106 * Adds a child to this node.
107 *
108 * @param child the child node to be added
Madan Jampani1184bc72016-09-08 08:37:05 -0700109 * @return {@code true} if the child set was modified as a result of this call, {@code false} otherwise
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -0700110 */
111 public boolean addChild(DocumentTreeNode<V> child) {
112 return children.add(child);
113 }
114
115 /**
Madan Jampani1184bc72016-09-08 08:37:05 -0700116 * Removes a child node.
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -0700117 *
118 * @param child the child node to be removed
Madan Jampani1184bc72016-09-08 08:37:05 -0700119 * @return {@code true} if the child set was modified as a result of this call, {@code false} otherwise
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -0700120 */
121 public boolean removeChild(String child) {
122 return children.remove(child);
123 }
124
125 /**
126 * Returns the parent of this node.
127 *
128 * @return the parent node of this node, which may be null
129 */
130 public DocumentTreeNode<V> getParent() {
131 return parent;
132 }
133
134 @Override
135 public int hashCode() {
136 return Objects.hash(this.key);
137 }
138
139 @Override
140 public boolean equals(Object obj) {
141 if (obj instanceof DocumentTreeNode) {
Madan Jampani1184bc72016-09-08 08:37:05 -0700142 DocumentTreeNode<V> that = (DocumentTreeNode<V>) obj;
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -0700143 if (this.parent.equals(that.parent)) {
144 if (this.children.size() == that.children.size()) {
Madan Jampani1184bc72016-09-08 08:37:05 -0700145 for (DocumentTreeNode<V> child : this.children) {
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -0700146 if (!that.children.contains(child)) {
147 return false;
148 }
149 }
150 return true;
151 }
152 }
153 }
154 return false;
155 }
156
157 @Override
158 public String toString() {
159 MoreObjects.ToStringHelper helper =
160 MoreObjects.toStringHelper(getClass())
161 .add("parent", this.parent)
162 .add("key", this.key)
163 .add("value", this.value);
Madan Jampani1184bc72016-09-08 08:37:05 -0700164 for (DocumentTreeNode<V> child : children) {
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -0700165 helper = helper.add("child", child.key);
166 }
167 return helper.toString();
168 }
169
170}