CollectionBase.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. //
  2. // System.Collections.CollectionBase.cs
  3. //
  4. // Author:
  5. // Nick Drochak II ([email protected])
  6. //
  7. // (C) 2001 Nick Drochak II
  8. //
  9. using System;
  10. namespace System.Collections {
  11. public abstract class CollectionBase : IList, ICollection, IEnumerable {
  12. // private instance properties
  13. private System.Collections.ArrayList myList;
  14. // public instance properties
  15. public virtual int Count { get { return InnerList.Count; } }
  16. // Public Instance Methods
  17. public virtual System.Collections.IEnumerator GetEnumerator() { return InnerList.GetEnumerator(); }
  18. public virtual void Clear() {
  19. OnClear();
  20. InnerList.Clear();
  21. OnClearComplete();
  22. }
  23. public virtual void RemoveAt (int index) {
  24. InnerList.RemoveAt(index);
  25. }
  26. // Protected Instance Constructors
  27. protected CollectionBase() {
  28. this.myList = new System.Collections.ArrayList();
  29. }
  30. // Protected Instance Properties
  31. protected System.Collections.ArrayList InnerList {get { return this.myList; } }
  32. protected System.Collections.IList List {get { return this; } }
  33. // Protected Instance Methods
  34. protected virtual void OnClear() { }
  35. protected virtual void OnClearComplete() { }
  36. protected virtual void OnInsert(int index, object value) { }
  37. protected virtual void OnInsertComplete(int index, object value) { }
  38. protected virtual void OnRemove(int index, object value) { }
  39. protected virtual void OnRemoveComplete(int index, object value) { }
  40. protected virtual void OnSet(int index, object oldValue, object newValue) { }
  41. protected virtual void OnSetComplete(int index, object oldValue, object newValue) { }
  42. protected virtual void OnValidate(object value) {
  43. if (null == value) {
  44. throw new System.ArgumentNullException("CollectionBase.OnValidate: Invalid parameter value passed to method: null");
  45. }
  46. }
  47. // ICollection methods
  48. void ICollection.CopyTo(Array array, int index) {
  49. lock (InnerList) { InnerList.CopyTo(array, index); }
  50. }
  51. object ICollection.SyncRoot {
  52. get { return InnerList.SyncRoot; }
  53. }
  54. bool ICollection.IsSynchronized {
  55. get { return InnerList.IsSynchronized; }
  56. }
  57. // IList methods
  58. int IList.Add (object value) {
  59. int newPosition;
  60. OnValidate(value);
  61. newPosition = InnerList.Count;
  62. OnInsert(newPosition, value);
  63. InnerList.Add(value);
  64. OnInsertComplete(newPosition, value);
  65. return newPosition;
  66. }
  67. bool IList.Contains (object value) {
  68. return InnerList.Contains(value);
  69. }
  70. int IList.IndexOf (object value) {
  71. return InnerList.IndexOf(value);
  72. }
  73. void IList.Insert (int index, object value) {
  74. OnValidate(value);
  75. OnInsert(index, value);
  76. InnerList.Insert(index, value);
  77. OnInsertComplete(index, value);
  78. }
  79. void IList.Remove (object value) {
  80. int removeIndex;
  81. OnValidate(value);
  82. removeIndex = InnerList.IndexOf(value);
  83. OnRemove(removeIndex, value);
  84. InnerList.Remove(value);
  85. OnRemoveComplete(removeIndex, value);
  86. }
  87. // IList properties
  88. bool IList.IsFixedSize {
  89. get { return InnerList.IsFixedSize; }
  90. }
  91. bool IList.IsReadOnly {
  92. get { return InnerList.IsReadOnly; }
  93. }
  94. object IList.this[int index] {
  95. get { return InnerList[index]; }
  96. set {
  97. object oldValue;
  98. // make sure we have been given a valid value
  99. OnValidate(value);
  100. // save a reference to the object that is in the list now
  101. oldValue = InnerList[index];
  102. OnSet(index, oldValue, value);
  103. InnerList[index] = value;
  104. OnSetComplete(index, oldValue, value);
  105. }
  106. }
  107. }
  108. }