CollectionBase.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. //
  2. // System.Collections.CollectionBase.cs
  3. //
  4. // Author:
  5. // Nick Drochak II ([email protected])
  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. #if NET_2_0
  35. [ComVisible(true)]
  36. #endif
  37. [Serializable]
  38. #if INSIDE_CORLIB
  39. public
  40. #else
  41. internal
  42. #endif
  43. abstract class CollectionBase : IList, ICollection, IEnumerable {
  44. // private instance properties
  45. private ArrayList list;
  46. // public instance properties
  47. public int Count { get { return InnerList.Count; } }
  48. // Public Instance Methods
  49. public IEnumerator GetEnumerator() { return InnerList.GetEnumerator(); }
  50. public void Clear() {
  51. OnClear();
  52. InnerList.Clear();
  53. OnClearComplete();
  54. }
  55. public void RemoveAt (int index) {
  56. object objectToRemove;
  57. objectToRemove = InnerList[index];
  58. OnValidate(objectToRemove);
  59. OnRemove(index, objectToRemove);
  60. InnerList.RemoveAt(index);
  61. OnRemoveComplete(index, objectToRemove);
  62. }
  63. // Protected Instance Constructors
  64. protected CollectionBase()
  65. {
  66. }
  67. #if NET_2_0
  68. protected CollectionBase (int capacity)
  69. {
  70. list = new ArrayList (capacity);
  71. }
  72. [ComVisible (false)]
  73. public int Capacity {
  74. get {
  75. if (list == null)
  76. list = new ArrayList ();
  77. return list.Capacity;
  78. }
  79. set {
  80. if (list == null)
  81. list = new ArrayList ();
  82. list.Capacity = value;
  83. }
  84. }
  85. #endif
  86. // Protected Instance Properties
  87. protected ArrayList InnerList {
  88. get {
  89. if (list == null)
  90. list = new ArrayList ();
  91. return list;
  92. }
  93. }
  94. protected IList List {get { return this; } }
  95. // Protected Instance Methods
  96. protected virtual void OnClear() { }
  97. protected virtual void OnClearComplete() { }
  98. protected virtual void OnInsert(int index, object value) { }
  99. protected virtual void OnInsertComplete(int index, object value) { }
  100. protected virtual void OnRemove(int index, object value) { }
  101. protected virtual void OnRemoveComplete(int index, object value) { }
  102. protected virtual void OnSet(int index, object oldValue, object newValue) { }
  103. protected virtual void OnSetComplete(int index, object oldValue, object newValue) { }
  104. protected virtual void OnValidate(object value) {
  105. if (null == value) {
  106. throw new System.ArgumentNullException("CollectionBase.OnValidate: Invalid parameter value passed to method: null");
  107. }
  108. }
  109. // ICollection methods
  110. void ICollection.CopyTo(Array array, int index) {
  111. InnerList.CopyTo(array, index);
  112. }
  113. object ICollection.SyncRoot {
  114. get { return InnerList.SyncRoot; }
  115. }
  116. bool ICollection.IsSynchronized {
  117. get { return InnerList.IsSynchronized; }
  118. }
  119. // IList methods
  120. int IList.Add (object value) {
  121. int newPosition;
  122. OnValidate(value);
  123. newPosition = InnerList.Count;
  124. OnInsert(newPosition, value);
  125. InnerList.Add(value);
  126. try {
  127. OnInsertComplete(newPosition, value);
  128. } catch {
  129. InnerList.RemoveAt (newPosition);
  130. throw;
  131. }
  132. return newPosition;
  133. }
  134. bool IList.Contains (object value) {
  135. return InnerList.Contains(value);
  136. }
  137. int IList.IndexOf (object value) {
  138. return InnerList.IndexOf(value);
  139. }
  140. void IList.Insert (int index, object value) {
  141. OnValidate(value);
  142. OnInsert(index, value);
  143. InnerList.Insert(index, value);
  144. try {
  145. OnInsertComplete(index, value);
  146. } catch {
  147. InnerList.RemoveAt (index);
  148. throw;
  149. }
  150. }
  151. void IList.Remove (object value) {
  152. int removeIndex;
  153. OnValidate(value);
  154. removeIndex = InnerList.IndexOf(value);
  155. if (removeIndex == -1)
  156. throw new ArgumentException ("The element cannot be found.", "value");
  157. OnRemove(removeIndex, value);
  158. InnerList.Remove(value);
  159. OnRemoveComplete(removeIndex, value);
  160. }
  161. // IList properties
  162. bool IList.IsFixedSize {
  163. get { return InnerList.IsFixedSize; }
  164. }
  165. bool IList.IsReadOnly {
  166. get { return InnerList.IsReadOnly; }
  167. }
  168. object IList.this[int index] {
  169. get { return InnerList[index]; }
  170. set {
  171. if (index < 0 || index >= InnerList.Count)
  172. throw new ArgumentOutOfRangeException ("index");
  173. object oldValue;
  174. // make sure we have been given a valid value
  175. OnValidate(value);
  176. // save a reference to the object that is in the list now
  177. oldValue = InnerList[index];
  178. OnSet(index, oldValue, value);
  179. InnerList[index] = value;
  180. try {
  181. OnSetComplete(index, oldValue, value);
  182. } catch {
  183. InnerList[index] = oldValue;
  184. throw;
  185. }
  186. }
  187. }
  188. }
  189. }