PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/Utilities/Collections/TreeNodeCollection.cs

#
C# | 206 lines | 129 code | 16 blank | 61 comment | 3 complexity | 0ff80430600b974d68ccdd50c2779faf MD5 | raw file
Possible License(s): Apache-2.0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using NUnit.Framework;
  5. namespace Delta.Utilities.Collections
  6. {
  7. /// <summary>
  8. /// Tree node collection
  9. /// </summary>
  10. /// <typeparam name="T">T</typeparam>
  11. public class TreeNodeCollection<T>
  12. : IEnumerable<TreeNode<T>>
  13. {
  14. #region Count (Public)
  15. /// <summary>
  16. /// Count
  17. /// </summary>
  18. /// <returns>Int</returns>
  19. public int Count
  20. {
  21. get
  22. {
  23. return mList.Count;
  24. }
  25. }
  26. #endregion
  27. #region Owner (Public)
  28. /// <summary>
  29. /// Owner
  30. /// </summary>
  31. /// <returns>Node</returns>
  32. public TreeNode<T> Owner
  33. {
  34. get
  35. {
  36. return mOwner;
  37. }
  38. }
  39. #endregion
  40. #region Item (Public)
  41. /// <summary>
  42. /// Indexer
  43. /// </summary>
  44. /// <returns>Node</returns>
  45. public TreeNode<T> this[int index]
  46. {
  47. get
  48. {
  49. return mList[index];
  50. }
  51. }
  52. #endregion
  53. #region Private
  54. #region mList (Private)
  55. /// <summary>
  56. /// mList List
  57. /// </summary>
  58. /// <typeparam name="T">T</typeparam>
  59. private readonly List<TreeNode<T>> mList = new List<TreeNode<T>>();
  60. #endregion
  61. #region mOwner (Private)
  62. /// <summary>
  63. /// mOwner Node
  64. /// </summary>
  65. /// <typeparam name="T">T</typeparam>
  66. private readonly TreeNode<T> mOwner;
  67. #endregion
  68. #endregion
  69. #region Constructors
  70. /// <summary>
  71. /// Create tree node collection
  72. /// </summary>
  73. /// <param name="owner">owner</param>
  74. public TreeNodeCollection(TreeNode<T> owner)
  75. {
  76. if (null == owner)
  77. {
  78. throw new ArgumentNullException("owner");
  79. }
  80. mOwner = owner;
  81. }
  82. #endregion
  83. #region IEnumerable Members
  84. /// <summary>
  85. /// Standard implementation.
  86. /// </summary>
  87. /// <returns>A IEnumerator on the current tree node collection</returns>
  88. IEnumerator
  89. IEnumerable.GetEnumerator()
  90. {
  91. return mList.GetEnumerator();
  92. }
  93. #endregion
  94. #region IEnumerable<TreeNode<T>> Members
  95. /// <summary>
  96. /// Get enumerator
  97. /// </summary>
  98. /// <returns>IEnumerator</returns>
  99. public IEnumerator<TreeNode<T>> GetEnumerator()
  100. {
  101. return mList.GetEnumerator();
  102. }
  103. #endregion
  104. #region Add (Public)
  105. /// <summary>
  106. /// Add
  107. /// </summary>
  108. /// <param name="rhs">rhs</param>
  109. public void Add(TreeNode<T> rhs)
  110. {
  111. if (mOwner.DoesShareHierarchyWith(rhs))
  112. {
  113. throw new InvalidOperationException(
  114. "Cannot add an ancestor or descendant.");
  115. }
  116. mList.Add(rhs);
  117. rhs.Parent = mOwner;
  118. }
  119. #endregion
  120. #region Remove (Public)
  121. /// <summary>
  122. /// Remove
  123. /// </summary>
  124. /// <param name="rhs">rhs</param>
  125. public void Remove(TreeNode<T> rhs)
  126. {
  127. mList.Remove(rhs);
  128. rhs.Parent = null;
  129. }
  130. #endregion
  131. #region Contains (Public)
  132. /// <summary>
  133. /// Does this node contains the given node directly?
  134. /// </summary>
  135. /// <param name="rhs">rhs</param>
  136. /// <returns>True if the list contains the given node</returns>
  137. public bool Contains(TreeNode<T> rhs)
  138. {
  139. return mList.Contains(rhs);
  140. }
  141. #endregion
  142. #region Clear (Public)
  143. /// <summary>
  144. /// Clear
  145. /// </summary>
  146. public void Clear()
  147. {
  148. foreach (TreeNode<T> n in this)
  149. {
  150. n.Parent = null;
  151. }
  152. mList.Clear();
  153. }
  154. #endregion
  155. }
  156. /// <summary>
  157. /// TreeNodeCollection tests
  158. /// </summary>
  159. internal class TreeNodeCollectionTests
  160. {
  161. #region TestTreeNode
  162. /// <summary>
  163. /// Test tree node
  164. /// </summary>
  165. [Test]
  166. public void TestTreeNode()
  167. {
  168. TreeNode<string> root = new TreeNode<string>("ROOT");
  169. root.Children.Add(new TreeNode<string>("hallo"));
  170. root.Children.Add(new TreeNode<string>("world"));
  171. Assert.Equal(root.Children.Count, 2);
  172. }
  173. #endregion
  174. #region TestTreeNodeCollection
  175. /// <summary>
  176. /// Test tree node collection
  177. /// </summary>
  178. [Test]
  179. public void TestTreeNodeCollection()
  180. {
  181. TreeNodeCollection<int> testTreeCollection =
  182. new TreeNodeCollection<int>(new TreeNode<int>(10));
  183. testTreeCollection.Add(new TreeNode<int>(2));
  184. testTreeCollection.Add(new TreeNode<int>(15));
  185. Assert.Equal(2, testTreeCollection[0].Data);
  186. Assert.Equal(15, testTreeCollection[1].Data);
  187. }
  188. #endregion
  189. }
  190. }