private bool RemoveAnonymousItem(Queue <string> nameParts, U item, bool failQuiet) { if (nameParts.Count == 0) { var removed = AnonymousItems.Remove(item); if (OnItemRemoved != null) { OnItemRemoved(null, item); } return(removed); } else { var first = nameParts.Dequeue(); if (!Subnodes.ContainsKey(first)) { if (failQuiet) { return(false); } throw new UriTreeException( String.Format( "Could not remove item '{0}' from group with full name '{1}'; " + "missing intermediate node with name '{2}'.", item, FullName, first ) ); } return(Subnodes[first].RemoveAnonymousItem(nameParts, item, failQuiet)); } }
private void RemoveItem(Queue <string> nameParts, bool failQuiet) { var first = nameParts.Dequeue(); if (nameParts.Count == 0) { if (!Items.ContainsKey(first)) { if (failQuiet) { return; } throw CouldNotRemoveItem(nameParts); } var item = Items[first]; Items.Remove(first); if (OnItemRemoved != null) { OnItemRemoved(first, item); } } else { if (!Subnodes.ContainsKey(first)) { if (failQuiet) { return; } throw CouldNotRemoveItem(nameParts); } Subnodes[first].RemoveItem(nameParts, failQuiet); } }
private void AddAnonymousItem(Queue <string> nameParts, U item) { if (nameParts.Count == 0) { AnonymousItems.Add(item); if (OnItemAdded != null) { OnItemAdded(null, item); } } else { var first = nameParts.Dequeue(); if (!Subnodes.ContainsKey(first)) { throw new UriTreeException( String.Format( "Could not add item '{0}' to group with full name '{1}'; " + "missing intermediate node with name '{2}'. Consider using InsertItem instead.", item, FullName, first ) ); } Subnodes[first].AddAnonymousItem(nameParts, item); } }
internal U GetItem(Queue <string> nameParts, bool useDefault, U defaultVal) { var first = nameParts.Dequeue(); if (nameParts.Count > 0 && !Subnodes.ContainsKey(first)) { if (useDefault) { return(defaultVal); } throw CouldNotRetrieveItem(first); } else if (nameParts.Count == 0) { if (!Items.ContainsKey(first)) { if (useDefault) { return(defaultVal); } throw CouldNotRetrieveItem(first); } return(Items[first]); } return(Subnodes[first].GetItem(nameParts, useDefault, defaultVal)); }
private void AddItem(Queue <string> nameParts, U item, bool disallowDuplicates) { var first = nameParts.Dequeue(); if (nameParts.Count == 0) { if (Items.ContainsKey(first)) { if (disallowDuplicates) { throw new UriTreeException( String.Format( "Could not add item '{0}' with name '{1}' to group with full name '{2}', " + "an item with that name already exists.", item, first, FullName ) ); } Items[first] = item; } else { Items.Add(first, item); } if (OnItemAdded != null) { OnItemAdded(first, item); } } else { if (!Subnodes.ContainsKey(first)) { throw new UriTreeException( String.Format( "Could not add item '{0}' with name '{1}' to group with full name '{2}; " + "missing intermediate node with name '{3}'. Consider using InsertItem instead.", item, nameParts, FullName, first ) ); } Subnodes[first].AddItem(nameParts, item, disallowDuplicates); } }
private void InsertAnonymousItem <NodeType>(Queue <string> nameParts, U item) where NodeType : UriTreeMutableGroup <T, U> { if (nameParts.Count == 0) { AnonymousItems.Add(item); if (OnItemAdded != null) { OnItemAdded(null, item); } } else { var first = nameParts.Dequeue(); if (!Subnodes.ContainsKey(first)) { var newNode = (T)Activator.CreateInstance(typeof(NodeType), first); newNode.SetParent((T)this); } Subnodes[first].InsertAnonymousItem <NodeType>(nameParts, item); } }
private void InsertItem <NodeType>(Queue <string> nameParts, U item, bool disallowDuplicates) where NodeType : UriTreeMutableGroup <T, U> { var first = nameParts.Dequeue(); if (nameParts.Count == 1) { if (Items.ContainsKey(first)) { if (disallowDuplicates) { throw new UriTreeException( String.Format( "Cannot insert item '{0}' with name '{1}' to group with full name '{2}', " + "an item with that name already exists.", item, first, FullName ) ); } Items[first] = item; } else { Items.Add(first, item); } if (OnItemAdded != null) { OnItemAdded(first, item); } } else { if (!Subnodes.ContainsKey(first)) { var newNode = (T)Activator.CreateInstance(typeof(NodeType), first); newNode.SetParent((T)this); // will add to Subnodes automatically. } Subnodes[first].InsertItem <NodeType>(nameParts, item, disallowDuplicates); } }