Collection.cs 5.6 KB

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