PageRenderTime 78ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/mcs/class/corlib/System.Collections/CollectionBase.cs

https://bitbucket.org/danipen/mono
C# | 217 lines | 144 code | 32 blank | 41 comment | 12 complexity | e060c3aef8e18ce27e293ea3ce8c0c3e 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.Collections.CollectionBase.cs
  3. //
  4. // Author:
  5. // Nick Drochak II (ndrochak@gol.com)
  6. //
  7. // (C) 2001 Nick Drochak II
  8. //
  9. //
  10. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System;
  32. using System.Runtime.InteropServices;
  33. namespace System.Collections {
  34. [ComVisible(true)]
  35. [Serializable]
  36. #if INSIDE_CORLIB
  37. public
  38. #else
  39. internal
  40. #endif
  41. abstract class CollectionBase : IList, ICollection, IEnumerable {
  42. // private instance properties
  43. private ArrayList list;
  44. // public instance properties
  45. public int Count { get { return InnerList.Count; } }
  46. // Public Instance Methods
  47. public IEnumerator GetEnumerator() { return InnerList.GetEnumerator(); }
  48. public void Clear() {
  49. OnClear();
  50. InnerList.Clear();
  51. OnClearComplete();
  52. }
  53. public void RemoveAt (int index) {
  54. object objectToRemove;
  55. objectToRemove = InnerList[index];
  56. OnValidate(objectToRemove);
  57. OnRemove(index, objectToRemove);
  58. InnerList.RemoveAt(index);
  59. OnRemoveComplete(index, objectToRemove);
  60. }
  61. // Protected Instance Constructors
  62. protected CollectionBase()
  63. {
  64. }
  65. protected CollectionBase (int capacity)
  66. {
  67. list = new ArrayList (capacity);
  68. }
  69. [ComVisible (false)]
  70. public int Capacity {
  71. get {
  72. if (list == null)
  73. list = new ArrayList ();
  74. return list.Capacity;
  75. }
  76. set {
  77. if (list == null)
  78. list = new ArrayList ();
  79. list.Capacity = value;
  80. }
  81. }
  82. // Protected Instance Properties
  83. protected ArrayList InnerList {
  84. get {
  85. if (list == null)
  86. list = new ArrayList ();
  87. return list;
  88. }
  89. }
  90. protected IList List {get { return this; } }
  91. // Protected Instance Methods
  92. protected virtual void OnClear() { }
  93. protected virtual void OnClearComplete() { }
  94. protected virtual void OnInsert(int index, object value) { }
  95. protected virtual void OnInsertComplete(int index, object value) { }
  96. protected virtual void OnRemove(int index, object value) { }
  97. protected virtual void OnRemoveComplete(int index, object value) { }
  98. protected virtual void OnSet(int index, object oldValue, object newValue) { }
  99. protected virtual void OnSetComplete(int index, object oldValue, object newValue) { }
  100. protected virtual void OnValidate(object value) {
  101. if (null == value) {
  102. throw new System.ArgumentNullException("CollectionBase.OnValidate: Invalid parameter value passed to method: null");
  103. }
  104. }
  105. // ICollection methods
  106. void ICollection.CopyTo(Array array, int index) {
  107. InnerList.CopyTo(array, index);
  108. }
  109. object ICollection.SyncRoot {
  110. get { return InnerList.SyncRoot; }
  111. }
  112. bool ICollection.IsSynchronized {
  113. get { return InnerList.IsSynchronized; }
  114. }
  115. // IList methods
  116. int IList.Add (object value) {
  117. int newPosition;
  118. OnValidate(value);
  119. newPosition = InnerList.Count;
  120. OnInsert(newPosition, value);
  121. InnerList.Add(value);
  122. try {
  123. OnInsertComplete(newPosition, value);
  124. } catch {
  125. InnerList.RemoveAt (newPosition);
  126. throw;
  127. }
  128. return newPosition;
  129. }
  130. bool IList.Contains (object value) {
  131. return InnerList.Contains(value);
  132. }
  133. int IList.IndexOf (object value) {
  134. return InnerList.IndexOf(value);
  135. }
  136. void IList.Insert (int index, object value) {
  137. OnValidate(value);
  138. OnInsert(index, value);
  139. InnerList.Insert(index, value);
  140. try {
  141. OnInsertComplete(index, value);
  142. } catch {
  143. InnerList.RemoveAt (index);
  144. throw;
  145. }
  146. }
  147. void IList.Remove (object value) {
  148. int removeIndex;
  149. OnValidate(value);
  150. removeIndex = InnerList.IndexOf(value);
  151. if (removeIndex == -1)
  152. throw new ArgumentException ("The element cannot be found.", "value");
  153. OnRemove(removeIndex, value);
  154. InnerList.Remove(value);
  155. OnRemoveComplete(removeIndex, value);
  156. }
  157. // IList properties
  158. bool IList.IsFixedSize {
  159. get { return InnerList.IsFixedSize; }
  160. }
  161. bool IList.IsReadOnly {
  162. get { return InnerList.IsReadOnly; }
  163. }
  164. object IList.this[int index] {
  165. get { return InnerList[index]; }
  166. set {
  167. if (index < 0 || index >= InnerList.Count)
  168. throw new ArgumentOutOfRangeException ("index");
  169. object oldValue;
  170. // make sure we have been given a valid value
  171. OnValidate(value);
  172. // save a reference to the object that is in the list now
  173. oldValue = InnerList[index];
  174. OnSet(index, oldValue, value);
  175. InnerList[index] = value;
  176. try {
  177. OnSetComplete(index, oldValue, value);
  178. } catch {
  179. InnerList[index] = oldValue;
  180. throw;
  181. }
  182. }
  183. }
  184. }
  185. }