Andrew Brown | 388c51d | 2016-09-09 17:56:48 -0700 | [diff] [blame] | 1 | /* |
| 2 | * jndn-utils |
| 3 | * Copyright (c) 2016, Intel Corporation. |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms and conditions of the GNU Lesser General Public License, |
| 7 | * version 3, as published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope it will be useful, but WITHOUT ANY |
| 10 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 11 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for |
| 12 | * more details. |
| 13 | */ |
| 14 | |
| 15 | package com.intel.jndn.utils; |
| 16 | |
| 17 | import net.named_data.jndn.Name; |
| 18 | |
| 19 | import java.util.Collection; |
| 20 | import java.util.Optional; |
| 21 | |
| 22 | /** |
Andrew Brown | 354d9e0 | 2016-09-12 20:51:58 -0700 | [diff] [blame] | 23 | * Represent a tree of nodes, branching based on their component values |
| 24 | * |
Andrew Brown | 388c51d | 2016-09-09 17:56:48 -0700 | [diff] [blame] | 25 | * @author Andrew Brown, andrew.brown@intel.com |
| 26 | */ |
| 27 | public interface NameTree<T> { |
Andrew Brown | 354d9e0 | 2016-09-12 20:51:58 -0700 | [diff] [blame] | 28 | /** |
| 29 | * @return the full name leading from the root of the tree to this node |
| 30 | */ |
Andrew Brown | 388c51d | 2016-09-09 17:56:48 -0700 | [diff] [blame] | 31 | Name fullName(); |
| 32 | |
Andrew Brown | 354d9e0 | 2016-09-12 20:51:58 -0700 | [diff] [blame] | 33 | /** |
| 34 | * @return the last component identifying this node; e.g. if the {@link #fullName()} is {@code /a/b/c} for this node, |
| 35 | * this method must return {@code c} |
| 36 | */ |
| 37 | Name.Component lastComponent(); |
| 38 | |
| 39 | /** |
| 40 | * @return the optional content stored at this location in the tree |
| 41 | */ |
| 42 | Optional<T> content(); |
| 43 | |
| 44 | /** |
| 45 | * @return the children of this node or an empty collection |
| 46 | */ |
Andrew Brown | 388c51d | 2016-09-09 17:56:48 -0700 | [diff] [blame] | 47 | Collection<NameTree<T>> children(); |
| 48 | |
Andrew Brown | 354d9e0 | 2016-09-12 20:51:58 -0700 | [diff] [blame] | 49 | /** |
| 50 | * @return the parent of this node; note that calling this on the root node will return {@code null} |
| 51 | */ |
Andrew Brown | 388c51d | 2016-09-09 17:56:48 -0700 | [diff] [blame] | 52 | NameTree<T> parent(); |
| 53 | |
Andrew Brown | 354d9e0 | 2016-09-12 20:51:58 -0700 | [diff] [blame] | 54 | /** |
| 55 | * @param name the full name leading to the location in the tree; if intervening nodes do not yet exist, they will be |
| 56 | * created |
| 57 | * @param content the content to store at this location; this may overwrite previously existing content, much like a |
| 58 | * {@link java.util.Map} |
| 59 | * @return a reference to the node inserted |
| 60 | */ |
Andrew Brown | 388c51d | 2016-09-09 17:56:48 -0700 | [diff] [blame] | 61 | NameTree<T> insert(Name name, T content); |
| 62 | |
Andrew Brown | 354d9e0 | 2016-09-12 20:51:58 -0700 | [diff] [blame] | 63 | /** |
| 64 | * @param query the name to use as a path through the tree |
| 65 | * @return an optional node; if there is no node at the end of the query, the {@link Optional} will be empty |
| 66 | */ |
Andrew Brown | 388c51d | 2016-09-09 17:56:48 -0700 | [diff] [blame] | 67 | Optional<NameTree<T>> find(Name query); |
| 68 | |
Andrew Brown | 354d9e0 | 2016-09-12 20:51:58 -0700 | [diff] [blame] | 69 | /** |
| 70 | * @param name the name to use as a path through the tree |
| 71 | * @return the removed node or an empty {@link Optional} if the node was not found |
| 72 | */ |
Andrew Brown | 388c51d | 2016-09-09 17:56:48 -0700 | [diff] [blame] | 73 | Optional<NameTree<T>> delete(Name name); |
| 74 | |
Andrew Brown | 354d9e0 | 2016-09-12 20:51:58 -0700 | [diff] [blame] | 75 | /** |
| 76 | * @return the count of all nodes in the tree below this one that are non-empty (e.g. have some content) |
| 77 | */ |
Andrew Brown | 388c51d | 2016-09-09 17:56:48 -0700 | [diff] [blame] | 78 | int count(); |
| 79 | |
Andrew Brown | 354d9e0 | 2016-09-12 20:51:58 -0700 | [diff] [blame] | 80 | /** |
| 81 | * Remove all nodes beneath this one; will have no effect on a leaf node |
| 82 | */ |
Andrew Brown | 388c51d | 2016-09-09 17:56:48 -0700 | [diff] [blame] | 83 | void clear(); |
| 84 | } |