CollectionBase.cs 5.4 KB

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