PageRenderTime 101ms CodeModel.GetById 40ms app.highlight 6ms RepoModel.GetById 34ms app.codeStats 0ms

/Utilities/Collections/TreeNodeCollection.cs

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