blob: a07c791f79f1e5385b4bb1a749f341b1c2f0c052 [file] [log] [blame]
Andrew Brown388c51d2016-09-09 17:56:48 -07001/*
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
15package com.intel.jndn.utils;
16
17import net.named_data.jndn.Name;
18
19import java.util.Collection;
20import java.util.Optional;
21
22/**
Andrew Brown354d9e02016-09-12 20:51:58 -070023 * Represent a tree of nodes, branching based on their component values
24 *
Andrew Brown388c51d2016-09-09 17:56:48 -070025 * @author Andrew Brown, andrew.brown@intel.com
26 */
27public interface NameTree<T> {
Andrew Brown354d9e02016-09-12 20:51:58 -070028 /**
29 * @return the full name leading from the root of the tree to this node
30 */
Andrew Brown388c51d2016-09-09 17:56:48 -070031 Name fullName();
32
Andrew Brown354d9e02016-09-12 20:51:58 -070033 /**
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 Brown388c51d2016-09-09 17:56:48 -070047 Collection<NameTree<T>> children();
48
Andrew Brown354d9e02016-09-12 20:51:58 -070049 /**
50 * @return the parent of this node; note that calling this on the root node will return {@code null}
51 */
Andrew Brown388c51d2016-09-09 17:56:48 -070052 NameTree<T> parent();
53
Andrew Brown354d9e02016-09-12 20:51:58 -070054 /**
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 Brown388c51d2016-09-09 17:56:48 -070061 NameTree<T> insert(Name name, T content);
62
Andrew Brown354d9e02016-09-12 20:51:58 -070063 /**
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 Brown388c51d2016-09-09 17:56:48 -070067 Optional<NameTree<T>> find(Name query);
68
Andrew Brown354d9e02016-09-12 20:51:58 -070069 /**
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 Brown388c51d2016-09-09 17:56:48 -070073 Optional<NameTree<T>> delete(Name name);
74
Andrew Brown354d9e02016-09-12 20:51:58 -070075 /**
76 * @return the count of all nodes in the tree below this one that are non-empty (e.g. have some content)
77 */
Andrew Brown388c51d2016-09-09 17:56:48 -070078 int count();
79
Andrew Brown354d9e02016-09-12 20:51:58 -070080 /**
81 * Remove all nodes beneath this one; will have no effect on a leaf node
82 */
Andrew Brown388c51d2016-09-09 17:56:48 -070083 void clear();
84}