2 // System.Collections.CollectionBase.cs
5 // Nick Drochak II (ndrochak@gol.com)
7 // (C) 2001 Nick Drochak II
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 namespace System.Collections {
38 public abstract class CollectionBase : IList, ICollection, IEnumerable {
40 // private instance properties
41 private ArrayList list;
43 // public instance properties
44 public int Count { get { return InnerList.Count; } }
46 // Public Instance Methods
47 public IEnumerator GetEnumerator() { return InnerList.GetEnumerator(); }
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);
62 // Protected Instance Constructors
63 protected CollectionBase() {
66 // Protected Instance Properties
67 protected ArrayList InnerList {
70 list = new ArrayList ();
75 protected IList List {get { return this; } }
77 // Protected Instance Methods
78 protected virtual void OnClear() { }
79 protected virtual void OnClearComplete() { }
81 protected virtual void OnInsert(int index, object value) { }
82 protected virtual void OnInsertComplete(int index, object value) { }
84 protected virtual void OnRemove(int index, object value) { }
85 protected virtual void OnRemoveComplete(int index, object value) { }
87 protected virtual void OnSet(int index, object oldValue, object newValue) { }
88 protected virtual void OnSetComplete(int index, object oldValue, object newValue) { }
90 protected virtual void OnValidate(object value) {
92 throw new System.ArgumentNullException("CollectionBase.OnValidate: Invalid parameter value passed to method: null");
96 // ICollection methods
97 void ICollection.CopyTo(Array array, int index) {
98 InnerList.CopyTo(array, index);
100 object ICollection.SyncRoot {
101 get { return InnerList.SyncRoot; }
103 bool ICollection.IsSynchronized {
104 get { return InnerList.IsSynchronized; }
108 int IList.Add (object value) {
111 newPosition = InnerList.Count;
112 OnInsert(newPosition, value);
113 InnerList.Add(value);
115 OnInsertComplete(newPosition, value);
117 InnerList.RemoveAt (newPosition);
124 bool IList.Contains (object value) {
125 return InnerList.Contains(value);
128 int IList.IndexOf (object value) {
129 return InnerList.IndexOf(value);
132 void IList.Insert (int index, object value) {
134 OnInsert(index, value);
135 InnerList.Insert(index, value);
137 OnInsertComplete(index, value);
139 InnerList.RemoveAt (index);
144 void IList.Remove (object value) {
147 removeIndex = InnerList.IndexOf(value);
148 if (removeIndex == -1)
149 throw new ArgumentException ("The element cannot be found.", "value");
150 OnRemove(removeIndex, value);
151 InnerList.Remove(value);
152 OnRemoveComplete(removeIndex, value);
156 bool IList.IsFixedSize {
157 get { return InnerList.IsFixedSize; }
160 bool IList.IsReadOnly {
161 get { return InnerList.IsReadOnly; }
164 object IList.this[int index] {
165 get { return InnerList[index]; }
167 if (index < 0 || index >= InnerList.Count)
168 throw new ArgumentOutOfRangeException ("index");
171 // make sure we have been given a valid value
173 // save a reference to the object that is in the list now
174 oldValue = InnerList[index];
176 OnSet(index, oldValue, value);
177 InnerList[index] = value;
179 OnSetComplete(index, oldValue, value);
181 InnerList[index] = oldValue;