PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/mcs/class/System.ServiceModel/System.Collections.Generic/SynchronizedCollection.cs

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