PageRenderTime 92ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/Epi Info 7.1.4.0 Release - Web Enter Integration/Epi.Core/Collections/NamedObjectCollection.cs

#
C# | 277 lines | 147 code | 27 blank | 103 comment | 2 complexity | afb88b0f310930ed8c633bbe25be22a2 MD5 | raw file
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using Epi;
  5. namespace Epi.Collections
  6. {
  7. /// <summary>
  8. /// Class NamedObjectCollection
  9. /// </summary>
  10. /// <typeparam name="T">Type</typeparam>
  11. public class NamedObjectCollection<T> : ICollection, IDisposable
  12. {
  13. #region Private Class Members
  14. private object syncRoot = null;
  15. private Dictionary<string, T> master = null;
  16. private Dictionary<string, object> tags = null;
  17. #endregion Private Class Members
  18. #region Constructors
  19. /// <summary>
  20. /// Default constructor
  21. /// </summary>
  22. public NamedObjectCollection()
  23. {
  24. ////Only objects that implement INamedObject interface can be used in this collection.
  25. //string interfaceName = typeof(Epi.INamedObject).FullName;
  26. //if (typeof(T).GetInterface(interfaceName, true) == null)
  27. //{
  28. // throw new System.Exception("Only objects that implement Epi.INamedObject interface can be used in NamedObjectCollection");
  29. //}
  30. master = new Dictionary<string, T>(StringComparer.OrdinalIgnoreCase);
  31. tags = new Dictionary<string, object>();
  32. syncRoot = new object();
  33. }
  34. #endregion Constructors
  35. #region Public Properties
  36. /// <summary>
  37. /// Gets the number of key/value pairs contained in
  38. /// <see cref="System.Collections.Generic.Dictionary&lt;TKey, TValue&gt;"/>.
  39. /// </summary>
  40. public int Count
  41. {
  42. get
  43. {
  44. return master.Count;
  45. }
  46. }
  47. /// <summary>
  48. /// Gets/sets Synchronization Root.
  49. /// </summary>
  50. public object SyncRoot
  51. {
  52. get
  53. {
  54. return this.syncRoot;
  55. }
  56. }
  57. /// <summary>
  58. /// Gets the is Synchronized flag.
  59. /// </summary>
  60. public bool IsSynchronized
  61. {
  62. get
  63. {
  64. return false;
  65. }
  66. }
  67. /// <summary>
  68. /// Gets a collection containing the keys in the
  69. /// <see cref="System.Collections.Generic.Dictionary&lt;TKey, TValue&gt;"/>.
  70. /// </summary>
  71. public Dictionary<string, T>.KeyCollection Keys
  72. {
  73. get
  74. {
  75. return master.Keys;
  76. }
  77. }
  78. /// <summary>
  79. /// Returns a list of names of the objects contained in the collection
  80. /// </summary>
  81. public List<string> Names
  82. {
  83. get
  84. {
  85. List<string> namesList = new List<string>();
  86. foreach (INamedObject obj in this)
  87. {
  88. namesList.Add(obj.Name);
  89. }
  90. return namesList;
  91. }
  92. }
  93. #endregion Public Properties
  94. #region Public Methods
  95. /// <summary>
  96. /// Addes the specified key and value to the dictionary.
  97. /// </summary>
  98. /// <param name="obj">The value of the element to add.</param>
  99. public virtual void Add(T obj)
  100. {
  101. string name = ((INamedObject)obj).Name.ToLower();
  102. master.Add(name, obj);
  103. }
  104. /// <summary>
  105. /// Addes the specified key and value to the dictionary.
  106. /// </summary>
  107. /// <param name="obj"></param>
  108. /// <param name="tag">The value of the element to add.</param>
  109. public void Add(T obj, object tag)
  110. {
  111. string name = ((INamedObject)obj).Name.ToLower();
  112. Add(obj);
  113. tags.Add(name, tag);
  114. }
  115. /// <summary>
  116. /// Removes the value with the specified key from the System.Collections.Generic.SortedDictionary&lt;T1,T2&gt;.
  117. /// </summary>
  118. /// <param name="obj">Named object to remove.</param>
  119. /// <returns>true if the value is successfully removed; otherwise, false.
  120. /// This method also returns false if key is not found in the System.Collections.Generic.SortedDictionary&lt;T1,T2&gt;.</returns>
  121. public virtual bool Remove(T obj)
  122. {
  123. INamedObject namedObject = obj as INamedObject;
  124. return master.Remove(namedObject.Name.ToLower());
  125. }
  126. /// <summary>
  127. /// Remove a string
  128. /// </summary>
  129. /// <param name="name">Name to remove</param>
  130. public virtual void Remove(string name)
  131. {
  132. T obj = this[name.ToLower()];
  133. Remove(obj);
  134. }
  135. /// <summary>
  136. /// Check to see if an object is contained
  137. /// </summary>
  138. /// <param name="obj">Object</param>
  139. /// <returns>Boolean</returns>
  140. public virtual bool Contains(T obj)
  141. {
  142. INamedObject namedObject = obj as INamedObject;
  143. return Contains(namedObject.Name.ToLower());
  144. }
  145. /// <summary>
  146. /// Check to see if an string name is contained
  147. /// </summary>
  148. /// <param name="name">Name</param>
  149. /// <returns>Boolean</returns>
  150. public virtual bool Contains(string name)
  151. {
  152. return master.ContainsKey(name.ToLower());
  153. }
  154. /// <summary>
  155. /// Copies and array to a given index
  156. /// </summary>
  157. /// <param name="array">Array</param>
  158. /// <param name="index">Index</param>
  159. public virtual void CopyTo(T[] array, int index)
  160. {
  161. master.Values.CopyTo(array, index);
  162. }
  163. /// <summary>
  164. /// Copies and array to a given index
  165. /// </summary>
  166. /// <param name="array">Array</param>
  167. /// <param name="index">Index</param>
  168. void ICollection.CopyTo(Array array, int index)
  169. {
  170. CopyTo((T[])array, index);
  171. }
  172. /// <summary>
  173. /// Add an array of Named objects to collection
  174. /// </summary>
  175. /// <param name="collection">A generic NamedObjectCollection</param>
  176. public void Add(NamedObjectCollection<T> collection)
  177. {
  178. foreach (T obj in collection)
  179. {
  180. Add(obj);
  181. }
  182. }
  183. /// <summary>
  184. /// Get Enum
  185. /// </summary>
  186. /// <returns>IEnumerator</returns>
  187. public virtual IEnumerator GetEnumerator()
  188. {
  189. return this.master.Values.GetEnumerator();
  190. }
  191. /// <summary>
  192. /// Type Name
  193. /// </summary>
  194. /// <param name="name">Name</param>
  195. /// <returns>NamedObjectCollection</returns>
  196. public T this[string name]
  197. {
  198. get
  199. {
  200. return master[name];
  201. }
  202. }
  203. /// <summary>
  204. /// Dispose master
  205. /// </summary>
  206. public virtual void Dispose()
  207. {
  208. if (master != null)
  209. {
  210. master.Clear();
  211. master = null;
  212. }
  213. }
  214. /// <summary>
  215. /// Check if name exists
  216. /// </summary>
  217. /// <param name="name">Name</param>
  218. /// <returns>Boolean</returns>
  219. public bool Exists(string name)
  220. {
  221. return master.ContainsKey(name.ToLower());
  222. }
  223. /// <summary>
  224. /// Check if object exists
  225. /// </summary>
  226. /// <param name="obj">Object</param>
  227. /// <returns>Boolean</returns>
  228. public bool Exists(T obj)
  229. {
  230. return master.ContainsValue(obj);
  231. }
  232. /// <summary>
  233. /// Clear master
  234. /// </summary>
  235. public void Clear()
  236. {
  237. master.Clear();
  238. }
  239. /// <summary>
  240. /// Returns the tag associated with the object.
  241. /// </summary>
  242. /// <param name="name">Name</param>
  243. /// <returns>object</returns>
  244. public object GetTag(string name)
  245. {
  246. return tags[name];
  247. }
  248. #endregion Public Methods
  249. }
  250. }