Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.store.primitives; |
| 18 | |
Madan Jampani | 1184bc7 | 2016-09-08 08:37:05 -0700 | [diff] [blame] | 19 | import static com.google.common.base.Preconditions.checkNotNull; |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 20 | |
| 21 | import java.util.Comparator; |
| 22 | import java.util.Iterator; |
| 23 | import java.util.Objects; |
| 24 | import java.util.TreeSet; |
| 25 | |
Madan Jampani | 1184bc7 | 2016-09-08 08:37:05 -0700 | [diff] [blame] | 26 | import org.onosproject.store.service.DocumentPath; |
| 27 | |
| 28 | import com.google.common.base.MoreObjects; |
| 29 | import com.google.common.collect.Sets; |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 30 | |
| 31 | /** |
Madan Jampani | 1184bc7 | 2016-09-08 08:37:05 -0700 | [diff] [blame] | 32 | * A {@code DocumentTree} node. |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 33 | */ |
| 34 | public class DocumentTreeNode<V> { |
| 35 | private final DocumentPath key; |
| 36 | private V value; |
Madan Jampani | 1184bc7 | 2016-09-08 08:37:05 -0700 | [diff] [blame] | 37 | private long version; |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 38 | 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 Jampani | 1184bc7 | 2016-09-08 08:37:05 -0700 | [diff] [blame] | 46 | private final DocumentTreeNode<V> parent; |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 47 | |
Madan Jampani | 1184bc7 | 2016-09-08 08:37:05 -0700 | [diff] [blame] | 48 | public DocumentTreeNode(DocumentPath key, |
| 49 | V value, |
| 50 | long version, |
| 51 | DocumentTreeNode<V> parent) { |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 52 | this.key = checkNotNull(key); |
| 53 | this.value = checkNotNull(value); |
Madan Jampani | 1184bc7 | 2016-09-08 08:37:05 -0700 | [diff] [blame] | 54 | this.version = version; |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 55 | this.parent = parent; |
| 56 | } |
| 57 | |
| 58 | /** |
Madan Jampani | 1184bc7 | 2016-09-08 08:37:05 -0700 | [diff] [blame] | 59 | * Returns this node's key. |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 60 | * |
| 61 | * @return the key |
| 62 | */ |
| 63 | public DocumentPath getKey() { |
| 64 | return key; |
| 65 | } |
| 66 | |
| 67 | /** |
Madan Jampani | 1184bc7 | 2016-09-08 08:37:05 -0700 | [diff] [blame] | 68 | * Returns this node's value. |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 69 | * |
| 70 | * @return the value |
| 71 | */ |
| 72 | public V getValue() { |
| 73 | return value; |
| 74 | } |
| 75 | |
| 76 | /** |
Madan Jampani | 1184bc7 | 2016-09-08 08:37:05 -0700 | [diff] [blame] | 77 | * Returns this node's version. |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 78 | * |
Madan Jampani | 1184bc7 | 2016-09-08 08:37:05 -0700 | [diff] [blame] | 79 | * @return the version |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 80 | */ |
Madan Jampani | 1184bc7 | 2016-09-08 08:37:05 -0700 | [diff] [blame] | 81 | 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 Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Returns a collection of the children of this node. |
| 98 | * |
Madan Jampani | 1184bc7 | 2016-09-08 08:37:05 -0700 | [diff] [blame] | 99 | * @return iterator for the children of this node. |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 100 | */ |
| 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 Jampani | 1184bc7 | 2016-09-08 08:37:05 -0700 | [diff] [blame] | 109 | * @return {@code true} if the child set was modified as a result of this call, {@code false} otherwise |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 110 | */ |
| 111 | public boolean addChild(DocumentTreeNode<V> child) { |
| 112 | return children.add(child); |
| 113 | } |
| 114 | |
| 115 | /** |
Madan Jampani | 1184bc7 | 2016-09-08 08:37:05 -0700 | [diff] [blame] | 116 | * Removes a child node. |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 117 | * |
| 118 | * @param child the child node to be removed |
Madan Jampani | 1184bc7 | 2016-09-08 08:37:05 -0700 | [diff] [blame] | 119 | * @return {@code true} if the child set was modified as a result of this call, {@code false} otherwise |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 120 | */ |
| 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 Jampani | 1184bc7 | 2016-09-08 08:37:05 -0700 | [diff] [blame] | 142 | DocumentTreeNode<V> that = (DocumentTreeNode<V>) obj; |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 143 | if (this.parent.equals(that.parent)) { |
| 144 | if (this.children.size() == that.children.size()) { |
Madan Jampani | 1184bc7 | 2016-09-08 08:37:05 -0700 | [diff] [blame] | 145 | for (DocumentTreeNode<V> child : this.children) { |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 146 | 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 Jampani | 1184bc7 | 2016-09-08 08:37:05 -0700 | [diff] [blame] | 164 | for (DocumentTreeNode<V> child : children) { |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 165 | helper = helper.add("child", child.key); |
| 166 | } |
| 167 | return helper.toString(); |
| 168 | } |
| 169 | |
| 170 | } |