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