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.service; |
| 18 | |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 19 | import java.util.Iterator; |
| 20 | |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 21 | import org.onosproject.store.primitives.DocumentTreeNode; |
| 22 | |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 23 | /** |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 24 | * A hierarchical <a href="https://en.wikipedia.org/wiki/Document_Object_Model">document tree</a> data structure. |
| 25 | * |
Aaron Kruglikov | 7c99811 | 2016-09-07 17:45:07 -0700 | [diff] [blame^] | 26 | * @param <V> document tree value type |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 27 | */ |
| 28 | public interface DocumentTree<V> { |
| 29 | |
| 30 | /** |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 31 | * Returns the {@link DocumentPath path} to root of the tree. |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 32 | * |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 33 | * @return path to root of the tree |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 34 | */ |
| 35 | DocumentPath root(); |
| 36 | |
| 37 | /** |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 38 | * Returns an iterator for all the first order descendants of a node. |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 39 | * |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 40 | * @param path path to the node |
| 41 | * @return an iterator for the child nodes of the specified node path |
| 42 | * @throws NoSuchDocumentPathException if the path does not point to a valid node |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 43 | */ |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 44 | Iterator<DocumentTreeNode<V>> getChildren(DocumentPath path); |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 45 | |
| 46 | /** |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 47 | * Returns a document tree node. |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 48 | * |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 49 | * @param path path to node |
| 50 | * @return node value or {@code null} if path does not point to a valid node |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 51 | */ |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 52 | DocumentTreeNode<V> getNode(DocumentPath path); |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 53 | |
| 54 | /** |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 55 | * Creates or updates a document tree node. |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 56 | * |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 57 | * @param path path for the node to create or update |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 58 | * @param value the non-null value to be associated with the key |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 59 | * @return the previous mapping or {@code null} if there was no previous mapping |
| 60 | * @throws NoSuchDocumentPathException if the parent node (for the node to create/update) does not exist |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 61 | */ |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 62 | V putNode(DocumentPath path, V value); |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 63 | |
| 64 | /** |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 65 | * Creates a document tree node if one does not exist already. |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 66 | * |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 67 | * @param path path for the node to create |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 68 | * @param value the non-null value to be associated with the key |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 69 | * @return returns {@code true} if the mapping could be added successfully, {@code false} otherwise |
| 70 | * @throws NoSuchDocumentPathException if the parent node (for the node to create) does not exist |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 71 | */ |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 72 | boolean createNode(DocumentPath path, V value); |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 73 | |
| 74 | /** |
Madan Jampani | 1184bc7 | 2016-09-08 08:37:05 -0700 | [diff] [blame] | 75 | * Conditionally updates a tree node if the current version matches a specified version. |
| 76 | * |
| 77 | * @param path path for the node to create |
| 78 | * @param newValue the non-null value to be associated with the key |
| 79 | * @param version current version of the value for update to occur |
| 80 | * @return returns {@code true} if the update was made, {@code false} otherwise |
| 81 | * @throws NoSuchDocumentPathException if the parent node (for the node to create) does not exist |
| 82 | */ |
| 83 | boolean replace(DocumentPath path, V newValue, long version); |
| 84 | |
| 85 | /** |
| 86 | * Conditionally updates a tree node if the current value matches a specified value. |
| 87 | * |
| 88 | * @param path path for the node to create |
| 89 | * @param newValue the non-null value to be associated with the key |
| 90 | * @param currentValue current value for update to occur |
| 91 | * @return returns {@code true} if the update was made, {@code false} otherwise |
| 92 | * @throws NoSuchDocumentPathException if the parent node (for the node to create) does not exist |
| 93 | */ |
| 94 | boolean replace(DocumentPath path, V newValue, V currentValue); |
| 95 | |
| 96 | /** |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 97 | * Removes the node with the specified path. |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 98 | * |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 99 | * is not a leaf node i.e has one or more children |
Thomas Vachuska | f5e6be4 | 2016-09-07 20:58:22 -0700 | [diff] [blame] | 100 | * @param key path for the node to remove |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 101 | * @return the previous value of the node or {@code null} if it did not exist |
| 102 | * @throws IllegalDocumentModificationException if the remove to be removed |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 103 | */ |
| 104 | V removeNode(DocumentPath key); |
Madan Jampani | 64ff13b | 2016-09-08 11:18:50 -0700 | [diff] [blame] | 105 | |
| 106 | /** |
| 107 | * Registers a listener to be notified when a subtree rooted at the specified path |
| 108 | * is modified. |
| 109 | * |
| 110 | * @param path path to root of subtree to monitor for updates |
| 111 | * @param listener listener to be notified |
| 112 | */ |
| 113 | void addListener(DocumentPath path, DocumentTreeListener<V> listener); |
| 114 | |
| 115 | /** |
| 116 | * Unregisters a previously added listener. |
| 117 | * |
| 118 | * @param listener listener to unregister |
| 119 | */ |
| 120 | void removeListener(DocumentTreeListener<V> listener); |
| 121 | |
| 122 | /** |
| 123 | * Registers a listener to be notified when the tree is modified. |
| 124 | * |
| 125 | * @param listener listener to be notified |
| 126 | */ |
| 127 | default void addListener(DocumentTreeListener<V> listener) { |
| 128 | addListener(root(), listener); |
| 129 | } |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 130 | } |