示例#1
0
        /// <summary>
        /// Retrieves an item at the given path. If the item doesn't exist,
        /// then a NotFoundException.
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public TValue Get(HierarchicalPath path)
        {
            // If we are at the top-level path, return ourselves.
            if (path.Count == 0)
            {
                // If we have the item, then return it. If we don't have the
                // item, then throw an exception.
                if (HasItem)
                {
                    // We have the item, so return it.
                    return(Item);
                }

                // Throw a not found exception since we can't find it.
                throw new KeyNotFoundException("Cannot retrieve value at path " + path);
            }

            // Get the top-level element for a child and make sure we have it.
            string topLevel = path.First;

            if (!nodes.ContainsKey(topLevel))
            {
                throw new KeyNotFoundException("Cannot retrieve value at path " + path);
            }

            // Pass the request into the child level.
            HierarchicalPath childPath = path.Splice(1);
            HierarchicalPathTreeCollection <TValue> child = nodes[topLevel];

            return(child.Get(childPath));
        }
示例#2
0
        /// <summary>
        /// Adds an item at the specified path.
        /// </summary>
        /// <param name="path">The path.</param>
        /// <param name="item">The item.</param>
        public void Add(
            HierarchicalPath path,
            TValue item)
        {
            // If are the top-level path, set the item.
            if (path.Count == 0)
            {
                Item    = item;
                HasItem = true;
                return;
            }

            // We have at least one more level. Figure out the top-level string
            // and see if we have to create the child tree node for it.
            string topLevel = path.First;

            if (!nodes.ContainsKey(topLevel))
            {
                nodes[topLevel] = CreateChild(topLevel);
            }

            // Pull out the child tree so we can add it.
            HierarchicalPathTreeCollection <TValue> child = nodes[topLevel];
            HierarchicalPath childPath = path.Splice(1);

            child.Add(childPath, item);
        }
示例#3
0
        /// <summary>
        /// Retrieves the child-level tree collection for a given path.
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public HierarchicalPathTreeCollection <TValue> GetChild(HierarchicalPath path)
        {
            // If we are at the top-level path, return ourselves.
            if (path.Count == 0)
            {
                return(this);
            }

            // Get the top-level element for a child and make sure we have it.
            string topLevel = path.First;

            if (!nodes.ContainsKey(topLevel))
            {
                throw new KeyNotFoundException("Cannot retrieve value at path " + path);
            }

            // Pass the request to the child.
            HierarchicalPathTreeCollection <TValue> child = nodes[topLevel];

            return(child.GetChild(path.Splice(1)));
        }