blob: 665355fbbbba52d963e72ef851efdc4f4cb9c2b6 [file] [log] [blame]
Jordan Haltermanf7554092017-07-30 15:05:51 -07001/*
2 * Copyright 2017-present Open Networking Foundation
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.service;
18
19import java.util.Iterator;
20import java.util.Objects;
21import java.util.TreeMap;
22
23import com.google.common.base.MoreObjects;
24import com.google.common.collect.ImmutableList;
25import com.google.common.collect.Maps;
26import com.google.common.collect.Sets;
27
28import static com.google.common.base.Preconditions.checkNotNull;
29
30/**
31 * A {@code DocumentTree} node.
32 */
33public class TestDocumentTreeNode<V> implements DocumentTreeNode<V> {
34 private final DocumentPath key;
35 private Versioned<V> value;
36 private final TreeMap<String, DocumentTreeNode<V>> children = Maps.newTreeMap();
37 private final DocumentTreeNode<V> parent;
38
39 public TestDocumentTreeNode(DocumentPath key,
40 V value,
41 long version,
42 DocumentTreeNode<V> parent) {
43 this.key = checkNotNull(key);
44 this.value = new Versioned<>(value, version);
45 this.parent = parent;
46 }
47
48 @Override
49 public DocumentPath path() {
50 return key;
51 }
52
53 @Override
54 public Versioned<V> value() {
55 return value;
56 }
57
58 @Override
59 public Iterator<DocumentTreeNode<V>> children() {
60 return ImmutableList.copyOf(children.values()).iterator();
61 }
62
63 @Override
64 public DocumentTreeNode<V> child(String name) {
65 return children.get(name);
66 }
67
68
69 public DocumentTreeNode<V> parent() {
70 return parent;
71 }
72
73 /**
74 * Adds a new child only if one does not exist with the name.
75 * @param name relative path name of the child node
76 * @param newValue new value to set
77 * @param newVersion new version to set
78 * @return previous value; can be {@code null} if no child currently exists with that relative path name.
Frank Wangd8ab0962017-08-11 11:09:30 +080079 * a non null return value indicates child already exists and no modification occurred.
Jordan Haltermanf7554092017-07-30 15:05:51 -070080 */
81 public Versioned<V> addChild(String name, V newValue, long newVersion) {
82 TestDocumentTreeNode<V> child = (TestDocumentTreeNode<V>) children.get(name);
83 if (child != null) {
84 return child.value();
85 }
86 children.put(name, new TestDocumentTreeNode<>(new DocumentPath(name, path()), newValue, newVersion, this));
87 return null;
88 }
89
90 /**
91 * Updates the node value.
92 *
93 * @param newValue new value to set
94 * @param newVersion new version to set
95 * @return previous value
96 */
97 public Versioned<V> update(V newValue, long newVersion) {
98 Versioned<V> previousValue = value;
99 value = new Versioned<>(newValue, newVersion);
100 return previousValue;
101 }
102
103
104 /**
105 * Removes a child node.
106 *
107 * @param name the name of child node to be removed
108 * @return {@code true} if the child set was modified as a result of this call, {@code false} otherwise
109 */
110 public boolean removeChild(String name) {
111 return children.remove(name) != null;
112 }
113
114 @Override
115 public int hashCode() {
116 return Objects.hash(this.key);
117 }
118
119 @Override
120 public boolean equals(Object obj) {
121 if (obj instanceof TestDocumentTreeNode) {
122 TestDocumentTreeNode<V> that = (TestDocumentTreeNode<V>) obj;
123 if (this.parent.equals(that.parent)) {
124 if (this.children.size() == that.children.size()) {
125 return Sets.symmetricDifference(this.children.keySet(), that.children.keySet()).isEmpty();
126 }
127 }
128 }
129 return false;
130 }
131
132 @Override
133 public String toString() {
134 MoreObjects.ToStringHelper helper =
135 MoreObjects.toStringHelper(getClass())
136 .add("parent", this.parent)
137 .add("key", this.key)
138 .add("value", this.value);
139 for (DocumentTreeNode<V> child : children.values()) {
140 helper = helper.add("child", "\n" + child.path().pathElements()
141 .get(child.path().pathElements().size() - 1) +
142 " : " + child.value());
143 }
144 return helper.toString();
145 }
146}