2005-07-21 Lluis Sanchez Gual <lluis@novell.com>
[mono.git] / mcs / class / corlib / System.Collections / CollectionBase.cs
1 //
2 // System.Collections.CollectionBase.cs
3 //
4 // Author:
5 //   Nick Drochak II (ndrochak@gol.com)
6 //
7 // (C) 2001 Nick Drochak II
8 //
9
10 //
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
12 //
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:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
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.
31 //
32
33 using System;
34
35 namespace System.Collections {
36
37         [Serializable]
38         public abstract class CollectionBase : IList, ICollection, IEnumerable {
39
40                 // private instance properties
41                 private ArrayList list;
42                 
43                 // public instance properties
44                 public int Count { get { return InnerList.Count; } }
45                 
46                 // Public Instance Methods
47                 public IEnumerator GetEnumerator() { return InnerList.GetEnumerator(); }
48                 public void Clear() { 
49                         OnClear();
50                         InnerList.Clear(); 
51                         OnClearComplete();
52                 }
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);
60                 }
61                 
62                 // Protected Instance Constructors
63                 protected CollectionBase() { 
64                 }
65                 
66                 // Protected Instance Properties
67                 protected ArrayList InnerList {
68                         get {
69                                 if (list == null)
70                                         list = new ArrayList ();
71                                 return list;
72                         } 
73                 }
74                 
75                 protected IList List {get { return this; } }
76                 
77                 // Protected Instance Methods
78                 protected virtual void OnClear() { }
79                 protected virtual void OnClearComplete() { }
80                 
81                 protected virtual void OnInsert(int index, object value) { }
82                 protected virtual void OnInsertComplete(int index, object value) { }
83
84                 protected virtual void OnRemove(int index, object value) { }
85                 protected virtual void OnRemoveComplete(int index, object value) { }
86
87                 protected virtual void OnSet(int index, object oldValue, object newValue) { }
88                 protected virtual void OnSetComplete(int index, object oldValue, object newValue) { }
89
90                 protected virtual void OnValidate(object value) {
91                         if (null == value) {
92                                 throw new System.ArgumentNullException("CollectionBase.OnValidate: Invalid parameter value passed to method: null");
93                         }
94                 }
95                 
96                 // ICollection methods
97                 void ICollection.CopyTo(Array array, int index) {
98                         InnerList.CopyTo(array, index);
99                 }
100                 object ICollection.SyncRoot {
101                         get { return InnerList.SyncRoot; }
102                 }
103                 bool ICollection.IsSynchronized {
104                         get { return InnerList.IsSynchronized; }
105                 }
106
107                 // IList methods
108                 int IList.Add (object value) {
109                         int newPosition;
110                         OnValidate(value);
111                         newPosition = InnerList.Count;
112                         OnInsert(newPosition, value);
113                         InnerList.Add(value);
114                         try {
115                                 OnInsertComplete(newPosition, value);
116                         } catch {
117                                 InnerList.RemoveAt (newPosition);
118                                 throw;
119                         }
120                         
121                         return newPosition;
122                 }
123                 
124                 bool IList.Contains (object value) {
125                         return InnerList.Contains(value);
126                 }
127
128                 int IList.IndexOf (object value) {
129                         return InnerList.IndexOf(value);
130                 }
131
132                 void IList.Insert (int index, object value) {
133                         OnValidate(value);
134                         OnInsert(index, value);
135                         InnerList.Insert(index, value);
136                         try {
137                                 OnInsertComplete(index, value);
138                         } catch {
139                                 InnerList.RemoveAt (index);
140                                 throw;
141                         }
142                 }
143
144                 void IList.Remove (object value) {
145                         int removeIndex;
146                         OnValidate(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);
153                 }
154
155                 // IList properties
156                 bool IList.IsFixedSize { 
157                         get { return InnerList.IsFixedSize; }
158                 }
159
160                 bool IList.IsReadOnly { 
161                         get { return InnerList.IsReadOnly; }
162                 }
163
164                 object IList.this[int index] { 
165                         get { return InnerList[index]; }
166                         set { 
167                                 if (index < 0 || index >= InnerList.Count)
168                                         throw new ArgumentOutOfRangeException ("index");
169
170                                 object oldValue;
171                                 // make sure we have been given a valid value
172                                 OnValidate(value);
173                                 // save a reference to the object that is in the list now
174                                 oldValue = InnerList[index];
175                                 
176                                 OnSet(index, oldValue, value);
177                                 InnerList[index] = value;
178                                 try {
179                                         OnSetComplete(index, oldValue, value);
180                                 } catch {
181                                         InnerList[index] = oldValue;
182                                         throw;
183                                 }
184                         }
185                 }
186         }
187 }