PageRenderTime 60ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/mcs/class/corlib/System.Collections.ObjectModel/Collection.cs

https://bitbucket.org/foobar22/mono
C# | 266 lines | 186 code | 44 blank | 36 comment | 15 complexity | 849695d2610f68e1ae443c030909ad43 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0, Unlicense, Apache-2.0, LGPL-2.0
  1. // -*- Mode: csharp; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
  2. //
  3. // System.Collections.ObjectModel.Collection
  4. //
  5. // Authors:
  6. // Zoltan Varga (vargaz@gmail.com)
  7. // David Waite (mass@akuma.org)
  8. // Marek Safar (marek.safar@gmail.com)
  9. //
  10. // (C) 2005 Novell, Inc.
  11. // (C) 2005 David Waite
  12. //
  13. //
  14. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  15. // Copyright (C) 2005 David Waite
  16. // Copyright (C) 2011 Xamarin, Inc (http://www.xamarin.com)
  17. //
  18. // Permission is hereby granted, free of charge, to any person obtaining
  19. // a copy of this software and associated documentation files (the
  20. // "Software"), to deal in the Software without restriction, including
  21. // without limitation the rights to use, copy, modify, merge, publish,
  22. // distribute, sublicense, and/or sell copies of the Software, and to
  23. // permit persons to whom the Software is furnished to do so, subject to
  24. // the following conditions:
  25. //
  26. // The above copyright notice and this permission notice shall be
  27. // included in all copies or substantial portions of the Software.
  28. //
  29. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  30. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  31. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  32. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  33. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  34. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  35. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  36. //
  37. using System;
  38. using System.Collections;
  39. using System.Collections.Generic;
  40. using System.Runtime.InteropServices;
  41. using System.Diagnostics;
  42. namespace System.Collections.ObjectModel
  43. {
  44. [ComVisible (false)]
  45. [Serializable]
  46. [DebuggerDisplay ("Count={Count}")]
  47. [DebuggerTypeProxy (typeof (CollectionDebuggerView<>))]
  48. public class Collection<T> : IList<T>, IList
  49. #if NET_4_5
  50. , IReadOnlyList<T>
  51. #endif
  52. {
  53. IList <T> list;
  54. object syncRoot;
  55. public Collection ()
  56. {
  57. List <T> l = new List <T> ();
  58. IList l2 = l as IList;
  59. syncRoot = l2.SyncRoot;
  60. list = l;
  61. }
  62. public Collection (IList <T> list)
  63. {
  64. if (list == null)
  65. throw new ArgumentNullException ("list");
  66. this.list = list;
  67. ICollection l = list as ICollection;
  68. syncRoot = (l != null) ? l.SyncRoot : new object ();
  69. }
  70. public void Add (T item)
  71. {
  72. int idx = list.Count;
  73. InsertItem (idx, item);
  74. }
  75. public void Clear ()
  76. {
  77. ClearItems ();
  78. }
  79. protected virtual void ClearItems ()
  80. {
  81. list.Clear ();
  82. }
  83. public bool Contains (T item)
  84. {
  85. return list.Contains (item);
  86. }
  87. public void CopyTo (T [] array, int index)
  88. {
  89. list.CopyTo (array, index);
  90. }
  91. public IEnumerator <T> GetEnumerator ()
  92. {
  93. return list.GetEnumerator ();
  94. }
  95. public int IndexOf (T item)
  96. {
  97. return list.IndexOf (item);
  98. }
  99. public void Insert (int index, T item)
  100. {
  101. InsertItem (index, item);
  102. }
  103. protected virtual void InsertItem (int index, T item)
  104. {
  105. list.Insert (index, item);
  106. }
  107. protected IList<T> Items {
  108. get { return list; }
  109. }
  110. public bool Remove (T item)
  111. {
  112. int idx = IndexOf (item);
  113. if (idx == -1)
  114. return false;
  115. RemoveItem (idx);
  116. return true;
  117. }
  118. public void RemoveAt (int index)
  119. {
  120. RemoveItem (index);
  121. }
  122. protected virtual void RemoveItem (int index)
  123. {
  124. list.RemoveAt (index);
  125. }
  126. public int Count {
  127. get { return list.Count; }
  128. }
  129. public T this [int index] {
  130. get { return list [index]; }
  131. set { SetItem (index, value); }
  132. }
  133. bool ICollection<T>.IsReadOnly {
  134. get { return list.IsReadOnly; }
  135. }
  136. protected virtual void SetItem (int index, T item)
  137. {
  138. list[index] = item;
  139. }
  140. #region Helper methods for non-generic interfaces
  141. internal static bool IsValidItem (object item)
  142. {
  143. return (item is T || (item == null && ! typeof (T).IsValueType));
  144. }
  145. internal static T ConvertItem (object item)
  146. {
  147. if (IsValidItem (item))
  148. return (T)item;
  149. throw new ArgumentException ("item");
  150. }
  151. internal static void CheckWritable (IList <T> list)
  152. {
  153. if (list.IsReadOnly)
  154. throw new NotSupportedException ();
  155. }
  156. internal static bool IsSynchronized (IList <T> list)
  157. {
  158. ICollection c = list as ICollection;
  159. return (c != null) ? c.IsSynchronized : false;
  160. }
  161. internal static bool IsFixedSize (IList <T> list)
  162. {
  163. IList l = list as IList;
  164. return (l != null) ? l.IsFixedSize : false;
  165. }
  166. #endregion
  167. #region Not generic interface implementations
  168. void ICollection.CopyTo (Array array, int index)
  169. {
  170. ((ICollection)list).CopyTo (array, index);
  171. }
  172. IEnumerator IEnumerable.GetEnumerator ()
  173. {
  174. return (IEnumerator) list.GetEnumerator ();
  175. }
  176. int IList.Add (object value)
  177. {
  178. int idx = list.Count;
  179. InsertItem (idx, ConvertItem (value));
  180. return idx;
  181. }
  182. bool IList.Contains (object value)
  183. {
  184. if (IsValidItem (value))
  185. return list.Contains ((T) value);
  186. return false;
  187. }
  188. int IList.IndexOf (object value)
  189. {
  190. if (IsValidItem (value))
  191. return list.IndexOf ((T) value);
  192. return -1;
  193. }
  194. void IList.Insert (int index, object value)
  195. {
  196. InsertItem (index, ConvertItem (value));
  197. }
  198. void IList.Remove (object value)
  199. {
  200. CheckWritable (list);
  201. int idx = IndexOf (ConvertItem (value));
  202. RemoveItem (idx);
  203. }
  204. bool ICollection.IsSynchronized {
  205. get { return IsSynchronized (list); }
  206. }
  207. object ICollection.SyncRoot {
  208. get { return syncRoot; }
  209. }
  210. bool IList.IsFixedSize {
  211. get { return IsFixedSize (list); }
  212. }
  213. bool IList.IsReadOnly {
  214. get { return list.IsReadOnly; }
  215. }
  216. object IList.this [int index] {
  217. get { return list [index]; }
  218. set { SetItem (index, ConvertItem (value)); }
  219. }
  220. #endregion
  221. }
  222. }