Collection.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. public bool Remove (T item)
  101. {
  102. int idx = IndexOf (item);
  103. if (idx == -1)
  104. return false;
  105. RemoveItem (idx);
  106. return true;
  107. }
  108. public void RemoveAt (int index)
  109. {
  110. RemoveItem (index);
  111. }
  112. protected virtual void RemoveItem (int index)
  113. {
  114. list.RemoveAt (index);
  115. }
  116. public virtual int Count {
  117. get { return list.Count; }
  118. }
  119. public virtual T this [int index] {
  120. get { return list [index]; }
  121. set { SetItem (index, value); }
  122. }
  123. public bool IsReadOnly {
  124. get { return list.IsReadOnly; }
  125. }
  126. protected virtual void SetItem (int index, T item)
  127. {
  128. list[index] = item;
  129. }
  130. #region Helper methods for non-generic interfaces
  131. internal static bool IsValidItem (object item)
  132. {
  133. return (item is T || (item == null && ! typeof (T).IsValueType));
  134. }
  135. internal static T ConvertItem (object item)
  136. {
  137. if (IsValidItem (item))
  138. return (T)item;
  139. throw new ArgumentException ("item");
  140. }
  141. internal static void CheckWritable (IList <T> list)
  142. {
  143. if (list.IsReadOnly)
  144. throw new NotSupportedException ();
  145. }
  146. internal static bool IsSynchronized (IList <T> list)
  147. {
  148. ICollection c = list as ICollection;
  149. return (c != null) ? c.IsSynchronized : false;
  150. }
  151. internal static bool IsFixedSize (IList <T> list)
  152. {
  153. IList l = list as IList;
  154. return (l != null) ? l.IsFixedSize : false;
  155. }
  156. #endregion
  157. #region Not generic interface implementations
  158. void ICollection.CopyTo (Array array, int arrayIndex)
  159. {
  160. T [] target = array as T [];
  161. if (target == null)
  162. throw new ArgumentException ("array");
  163. list.CopyTo (target, arrayIndex);
  164. }
  165. IEnumerator IEnumerable.GetEnumerator ()
  166. {
  167. return (IEnumerator) list.GetEnumerator ();
  168. }
  169. int IList.Add (object item)
  170. {
  171. int idx = list.Count;
  172. InsertItem (idx, ConvertItem (item));
  173. return idx;
  174. }
  175. bool IList.Contains (object item)
  176. {
  177. if (IsValidItem (item))
  178. return list.Contains ((T) item);
  179. return false;
  180. }
  181. int IList.IndexOf (object item)
  182. {
  183. if (IsValidItem (item))
  184. return list.IndexOf ((T) item);
  185. return -1;
  186. }
  187. void IList.Insert (int index, object item)
  188. {
  189. InsertItem (index, ConvertItem (item));
  190. }
  191. void IList.Remove (object item)
  192. {
  193. CheckWritable (list);
  194. int idx = IndexOf (ConvertItem (item));
  195. RemoveItem (idx);
  196. }
  197. bool ICollection.IsSynchronized {
  198. get { return IsSynchronized (list); }
  199. }
  200. object ICollection.SyncRoot {
  201. get { return syncRoot; }
  202. }
  203. bool IList.IsFixedSize {
  204. get { return IsFixedSize (list); }
  205. }
  206. bool IList.IsReadOnly {
  207. get { return list.IsReadOnly; }
  208. }
  209. object IList.this [int index] {
  210. get { return list [index]; }
  211. set { SetItem (index, ConvertItem (value)); }
  212. }
  213. #endregion
  214. }
  215. }
  216. #endif