PageRenderTime 43ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/danipen/mono
C# | 223 lines | 152 code | 44 blank | 27 comment | 15 complexity | 70ee721f93e08d3106ec001e6f696b9e 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. // System.ServiceModel.SynchronizedReadOnlyCollection.cs
  3. //
  4. // Author: Duncan Mak (duncan@novell.com)
  5. //
  6. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining
  9. // a copy of this software and associated documentation files (the
  10. // "Software"), to deal in the Software without restriction, including
  11. // without limitation the rights to use, copy, modify, merge, publish,
  12. // distribute, sublicense, and/or sell copies of the Software, and to
  13. // permit persons to whom the Software is furnished to do so, subject to
  14. // the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be
  17. // included in all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  21. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  23. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  24. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  25. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. //
  27. using System;
  28. using System.Collections;
  29. using System.Runtime.InteropServices;
  30. namespace System.Collections.Generic
  31. {
  32. [ComVisible (false)]
  33. public class SynchronizedReadOnlyCollection<T>
  34. : IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable
  35. {
  36. List<T> l;
  37. object sync_root;
  38. public SynchronizedReadOnlyCollection ()
  39. : this (new object ())
  40. {
  41. }
  42. public SynchronizedReadOnlyCollection (object sync_root)
  43. : this (sync_root, new List<T> ())
  44. {
  45. }
  46. public SynchronizedReadOnlyCollection (object sync_root, IEnumerable<T> list)
  47. {
  48. if (sync_root == null)
  49. throw new ArgumentNullException ("sync_root");
  50. if (list == null)
  51. throw new ArgumentNullException ("list");
  52. this.sync_root = sync_root;
  53. this.l = new List<T> (list);
  54. }
  55. public SynchronizedReadOnlyCollection (object sync_root, params T [] list)
  56. : this (sync_root, (IEnumerable<T>) list)
  57. {
  58. }
  59. public SynchronizedReadOnlyCollection (object sync_root, List<T> list, bool make_copy)
  60. : this (sync_root,
  61. list == null ? null : make_copy ? new List<T> (list) : list)
  62. {
  63. }
  64. public bool Contains (T value)
  65. {
  66. bool retval;
  67. lock (sync_root) {
  68. retval = l.Contains (value);
  69. }
  70. return retval;
  71. }
  72. public void CopyTo (T [] array, int index)
  73. {
  74. lock (sync_root) {
  75. l.CopyTo (array, index);
  76. }
  77. }
  78. public IEnumerator<T> GetEnumerator ()
  79. {
  80. IEnumerator<T> retval;
  81. lock (sync_root) {
  82. retval = l.GetEnumerator ();
  83. }
  84. return retval;
  85. }
  86. public int IndexOf (T value)
  87. {
  88. int retval;
  89. lock (sync_root) {
  90. retval = l.IndexOf (value);
  91. }
  92. return retval;
  93. }
  94. void ICollection<T>.Add (T value) { throw new NotSupportedException (); }
  95. void ICollection<T>.Clear () { throw new NotSupportedException (); }
  96. bool ICollection<T>.Remove (T value) { throw new NotSupportedException (); }
  97. void IList<T>.Insert (int index, T value) { throw new NotSupportedException (); }
  98. void IList<T>.RemoveAt (int index) { throw new NotSupportedException (); }
  99. void ICollection.CopyTo (Array array, int index)
  100. {
  101. ICollection<T> a = array as ICollection<T>;
  102. if (a == null)
  103. throw new ArgumentException ("The array type is not compatible.");
  104. lock (sync_root) {
  105. ((ICollection) l).CopyTo (array, index);
  106. }
  107. }
  108. IEnumerator IEnumerable.GetEnumerator ()
  109. {
  110. return GetEnumerator ();
  111. }
  112. int IList.Add (object value) { throw new NotSupportedException (); }
  113. void IList.Clear () { throw new NotSupportedException (); }
  114. bool IList.Contains (object value)
  115. {
  116. if (typeof (T).IsValueType)
  117. throw new ArgumentException ("This is a collection of ValueTypes.");
  118. // null always gets thru
  119. if (value is T == false && value != null)
  120. throw new ArgumentException ("value is not of the same type as this collection.");
  121. bool retval;
  122. T val = (T) value;
  123. lock (sync_root) {
  124. retval = l.Contains (val);
  125. }
  126. return retval;
  127. }
  128. int IList.IndexOf (object value)
  129. {
  130. if (typeof (T).IsValueType)
  131. throw new ArgumentException ("This is a collection of ValueTypes.");
  132. if (value is T == false)
  133. throw new ArgumentException ("value is not of the same type as this collection.");
  134. int retval;
  135. T val = (T) value;
  136. lock (sync_root) {
  137. retval = l.IndexOf (val);
  138. }
  139. return retval;
  140. }
  141. void IList.Insert (int index, object value) { throw new NotSupportedException (); }
  142. void IList.Remove (object value) { throw new NotSupportedException (); }
  143. void IList.RemoveAt (int index) { throw new NotSupportedException (); }
  144. public int Count {
  145. get {
  146. int retval;
  147. lock (sync_root) {
  148. retval = l.Count;
  149. }
  150. return retval;
  151. }
  152. }
  153. public T this [int index] {
  154. get {
  155. T retval;
  156. lock (sync_root) {
  157. retval = l [index];
  158. }
  159. return retval;
  160. }
  161. }
  162. protected IList<T> Items {
  163. get { return l; }
  164. }
  165. bool ICollection<T>.IsReadOnly { get { return true; }}
  166. bool ICollection.IsSynchronized { get { return true; }}
  167. object ICollection.SyncRoot { get { return sync_root; }}
  168. bool IList.IsFixedSize { get { return true; }}
  169. bool IList.IsReadOnly { get { return true; }}
  170. T IList<T>.this [int index] {
  171. get { return this [index]; }
  172. set { throw new NotSupportedException (); }
  173. }
  174. object IList.this [int index] {
  175. get { return this [index]; }
  176. set { throw new NotSupportedException (); }
  177. }
  178. }
  179. }