CollectionBase.cs 5.2 KB

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