/Project/Src/Parser/VariableCollection.cs

https://github.com/Visual-Stylecop/Visual-StyleCop · C# · 295 lines · 134 code · 30 blank · 131 comment · 18 complexity · 14b09ca57f3a504242a523711d8448d2 MD5 · raw file

  1. // --------------------------------------------------------------------------------------------------------------------
  2. // <copyright file="VariableCollection.cs" company="http://stylecop.codeplex.com">
  3. // MS-PL
  4. // </copyright>
  5. // <license>
  6. // This source code is subject to terms and conditions of the Microsoft
  7. // Public License. A copy of the license can be found in the License.html
  8. // file at the root of this distribution. If you cannot locate the
  9. // Microsoft Public License, please send an email to dlr@microsoft.com.
  10. // By using this source code in any fashion, you are agreeing to be bound
  11. // by the terms of the Microsoft Public License. You must not remove this
  12. // notice, or any other, from this software.
  13. // </license>
  14. // <summary>
  15. // A collection of variables for an element or code scope.
  16. // </summary>
  17. // --------------------------------------------------------------------------------------------------------------------
  18. namespace StyleCop.CSharp
  19. {
  20. using System;
  21. using System.Collections;
  22. using System.Collections.Generic;
  23. /// <summary>
  24. /// A collection of variables for an element or code scope.
  25. /// </summary>
  26. public sealed class VariableCollection : ICollection<Variable>
  27. {
  28. /// <summary>
  29. /// An empty array of variables.
  30. /// </summary>
  31. private static readonly Variable[] EmptyVariableArray = new Variable[0];
  32. /// <summary>
  33. /// Adapts the enumerator for the empty variable array to a generic type enumerator.
  34. /// </summary>
  35. private static LegacyEnumeratorAdapter<Variable> emptyVariableArrayEnumerator;
  36. /// <summary>
  37. /// The variable collection.
  38. /// </summary>
  39. private Dictionary<string, Variable> variables;
  40. /// <summary>
  41. /// Initializes a new instance of the <see cref="VariableCollection"/> class.
  42. /// </summary>
  43. internal VariableCollection()
  44. {
  45. }
  46. /// <summary>
  47. /// Gets the number of variables in the collection.
  48. /// </summary>
  49. public int Count
  50. {
  51. get
  52. {
  53. if (this.variables == null)
  54. {
  55. return 0;
  56. }
  57. return this.variables.Count;
  58. }
  59. }
  60. /// <summary>
  61. /// Gets a value indicating whether the collection is read-only.
  62. /// </summary>
  63. public bool IsReadOnly
  64. {
  65. get
  66. {
  67. return true;
  68. }
  69. }
  70. /// <summary>
  71. /// Gets the variable with the given name.
  72. /// </summary>
  73. /// <param name="name">
  74. /// The name of the variable to get.
  75. /// </param>
  76. /// <returns>
  77. /// Returns the variable.
  78. /// </returns>
  79. public Variable this[string name]
  80. {
  81. get
  82. {
  83. return this.GetVariable(name);
  84. }
  85. }
  86. /// <summary>
  87. /// Determines whether a variable with the given name is contained within the collection.
  88. /// </summary>
  89. /// <param name="name">
  90. /// The name of the variable.
  91. /// </param>
  92. /// <returns>
  93. /// Returns true if the item is contained within the collection.
  94. /// </returns>
  95. public bool Contains(string name)
  96. {
  97. Param.RequireNotNull(name, "name");
  98. if (this.variables == null)
  99. {
  100. return false;
  101. }
  102. return this.variables.ContainsKey(name);
  103. }
  104. /// <summary>
  105. /// Copies the variable to the given array.
  106. /// </summary>
  107. /// <param name="array">
  108. /// The array to copy the variables into.
  109. /// </param>
  110. /// <param name="arrayIndex">
  111. /// The index in the array at which to begin copying.
  112. /// </param>
  113. public void CopyTo(Variable[] array, int arrayIndex)
  114. {
  115. Param.RequireNotNull(array, "array");
  116. Param.RequireGreaterThanOrEqualToZero(arrayIndex, "arrayIndex");
  117. if (this.variables != null)
  118. {
  119. int i = arrayIndex;
  120. foreach (Variable variable in this.variables.Values)
  121. {
  122. array[i++] = variable;
  123. }
  124. }
  125. }
  126. /// <summary>
  127. /// Gets an enumerator for iterating through the variables in the collection.
  128. /// </summary>
  129. /// <returns>Returns the enumerator.</returns>
  130. public IEnumerator<Variable> GetEnumerator()
  131. {
  132. if (this.variables == null)
  133. {
  134. if (emptyVariableArrayEnumerator == null)
  135. {
  136. emptyVariableArrayEnumerator = new LegacyEnumeratorAdapter<Variable>(EmptyVariableArray.GetEnumerator());
  137. }
  138. return emptyVariableArrayEnumerator;
  139. }
  140. return this.variables.Values.GetEnumerator();
  141. }
  142. /// <summary>
  143. /// Gets the variable with the given name.
  144. /// </summary>
  145. /// <param name="name">
  146. /// The name of the variable to get.
  147. /// </param>
  148. /// <returns>
  149. /// Returns the variable.
  150. /// </returns>
  151. public Variable GetVariable(string name)
  152. {
  153. Param.RequireValidString(name, "name");
  154. if (this.variables != null)
  155. {
  156. Variable variable;
  157. if (this.variables.TryGetValue(name, out variable))
  158. {
  159. return variable;
  160. }
  161. }
  162. return null;
  163. }
  164. /// <summary>
  165. /// Adds the given variable.
  166. /// </summary>
  167. /// <param name="variable">
  168. /// The variable to add.
  169. /// </param>
  170. /// <remarks>
  171. /// This method is not supported.
  172. /// </remarks>
  173. void ICollection<Variable>.Add(Variable variable)
  174. {
  175. Param.Ignore(variable);
  176. throw new NotSupportedException();
  177. }
  178. /// <summary>
  179. /// Clears the contents of the collection.
  180. /// </summary>
  181. /// <remarks>This method is not supported.</remarks>
  182. void ICollection<Variable>.Clear()
  183. {
  184. throw new NotSupportedException();
  185. }
  186. /// <summary>
  187. /// Determines whether the given variable is contained within the collection.
  188. /// </summary>
  189. /// <param name="variable">
  190. /// The variable.
  191. /// </param>
  192. /// <returns>
  193. /// Returns true if the item is contained within the collection.
  194. /// </returns>
  195. /// <remarks>
  196. /// This method is not supported.
  197. /// </remarks>
  198. bool ICollection<Variable>.Contains(Variable variable)
  199. {
  200. Param.Ignore(variable);
  201. throw new NotSupportedException();
  202. }
  203. /// <summary>
  204. /// Gets an enumerator for iterating through the variables in the collection.
  205. /// </summary>
  206. /// <returns>Gets the enumerator.</returns>
  207. IEnumerator System.Collections.IEnumerable.GetEnumerator()
  208. {
  209. if (this.variables == null)
  210. {
  211. return EmptyVariableArray.GetEnumerator();
  212. }
  213. return this.variables.Values.GetEnumerator();
  214. }
  215. /// <summary>
  216. /// Removes the given variable.
  217. /// </summary>
  218. /// <param name="variable">
  219. /// The variable to remove.
  220. /// </param>
  221. /// <returns>
  222. /// Returns true if the item was removed from the collection.
  223. /// </returns>
  224. /// <remarks>
  225. /// This method is not supported.
  226. /// </remarks>
  227. bool ICollection<Variable>.Remove(Variable variable)
  228. {
  229. Param.Ignore(variable);
  230. throw new NotSupportedException();
  231. }
  232. /// <summary>
  233. /// Adds a variable to the collection.
  234. /// </summary>
  235. /// <param name="variable">
  236. /// The variable to add.
  237. /// </param>
  238. internal void Add(Variable variable)
  239. {
  240. Param.AssertNotNull(variable, "variable");
  241. if (this.variables == null)
  242. {
  243. this.variables = new Dictionary<string, Variable>();
  244. }
  245. if (!this.variables.ContainsKey(variable.Name))
  246. {
  247. this.variables.Add(variable.Name, variable);
  248. }
  249. }
  250. /// <summary>
  251. /// Adds a range of variables to the collection.
  252. /// </summary>
  253. /// <param name="items">
  254. /// The variables to add.
  255. /// </param>
  256. internal void AddRange(IEnumerable<Variable> items)
  257. {
  258. Param.AssertNotNull(items, "items");
  259. foreach (Variable variable in items)
  260. {
  261. this.Add(variable);
  262. }
  263. }
  264. }
  265. }