2004-05-01 Andreas Nahr <ClassDevelopment@A-SoftTech.com>
[mono.git] / mcs / class / corlib / System.Collections / CollectionBase.cs
1 //\r
2 // System.Collections.CollectionBase.cs\r
3 //\r
4 // Author:\r
5 //   Nick Drochak II (ndrochak@gol.com)\r
6 //\r
7 // (C) 2001 Nick Drochak II\r
8 //\r
9 \r
10 using System;\r
11 \r
12 namespace System.Collections {\r
13 \r
14         [Serializable]\r
15         public abstract class CollectionBase : IList, ICollection, IEnumerable {\r
16 \r
17                 // private instance properties\r
18                 private ArrayList list;\r
19                 \r
20                 // public instance properties\r
21                 public int Count { get { return InnerList.Count; } }\r
22                 \r
23                 // Public Instance Methods\r
24                 public IEnumerator GetEnumerator() { return InnerList.GetEnumerator(); }\r
25                 public void Clear() { \r
26                         OnClear();\r
27                         InnerList.Clear(); \r
28                         OnClearComplete();\r
29                 }\r
30                 public void RemoveAt (int index) {\r
31                         object objectToRemove;\r
32                         objectToRemove = InnerList[index];\r
33                         OnValidate(objectToRemove);\r
34                         OnRemove(index, objectToRemove);\r
35                         InnerList.RemoveAt(index);\r
36                         OnRemoveComplete(index, objectToRemove);\r
37                 }\r
38                 \r
39                 // Protected Instance Constructors\r
40                 protected CollectionBase() { \r
41                         this.list = new ArrayList();\r
42                 }\r
43                 \r
44                 // Protected Instance Properties\r
45                 protected ArrayList InnerList {get { return this.list; } }\r
46                 protected IList List {get { return this; } }\r
47                 \r
48                 // Protected Instance Methods\r
49                 protected virtual void OnClear() { }\r
50                 protected virtual void OnClearComplete() { }\r
51                 \r
52                 protected virtual void OnInsert(int index, object value) { }\r
53                 protected virtual void OnInsertComplete(int index, object value) { }\r
54 \r
55                 protected virtual void OnRemove(int index, object value) { }\r
56                 protected virtual void OnRemoveComplete(int index, object value) { }\r
57 \r
58                 protected virtual void OnSet(int index, object oldValue, object newValue) { }\r
59                 protected virtual void OnSetComplete(int index, object oldValue, object newValue) { }\r
60 \r
61                 protected virtual void OnValidate(object value) {\r
62                         if (null == value) {\r
63                                 throw new System.ArgumentNullException("CollectionBase.OnValidate: Invalid parameter value passed to method: null");\r
64                         }\r
65                 }\r
66                 \r
67                 // ICollection methods\r
68                 void ICollection.CopyTo(Array array, int index) {\r
69                         InnerList.CopyTo(array, index);\r
70                 }\r
71                 object ICollection.SyncRoot {\r
72                         get { return InnerList.SyncRoot; }\r
73                 }\r
74                 bool ICollection.IsSynchronized {\r
75                         get { return InnerList.IsSynchronized; }\r
76                 }\r
77 \r
78                 // IList methods\r
79                 int IList.Add (object value) {\r
80                         int newPosition;\r
81                         OnValidate(value);\r
82                         newPosition = InnerList.Count;\r
83                         OnInsert(newPosition, value);\r
84                         InnerList.Add(value);\r
85                         try {\r
86                                 OnInsertComplete(newPosition, value);\r
87                         } catch {\r
88                                 InnerList.RemoveAt (newPosition);\r
89                                 throw;\r
90                         }\r
91                         \r
92                         return newPosition;\r
93                 }\r
94                 \r
95                 bool IList.Contains (object value) {\r
96                         return InnerList.Contains(value);\r
97                 }\r
98 \r
99                 int IList.IndexOf (object value) {\r
100                         return InnerList.IndexOf(value);\r
101                 }\r
102 \r
103                 void IList.Insert (int index, object value) {\r
104                         OnValidate(value);\r
105                         OnInsert(index, value);\r
106                         InnerList.Insert(index, value);\r
107                         try {\r
108                                 OnInsertComplete(index, value);\r
109                         } catch {\r
110                                 InnerList.RemoveAt (index);\r
111                                 throw;\r
112                         }\r
113                 }\r
114 \r
115                 void IList.Remove (object value) {\r
116                         int removeIndex;\r
117                         OnValidate(value);\r
118                         removeIndex = InnerList.IndexOf(value);\r
119                         if (removeIndex == -1)\r
120                                 throw new ArgumentException ("The element cannot be found.", "value");\r
121                         OnRemove(removeIndex, value);\r
122                         InnerList.Remove(value);\r
123                         OnRemoveComplete(removeIndex, value);\r
124                 }\r
125 \r
126                 // IList properties\r
127                 bool IList.IsFixedSize { \r
128                         get { return InnerList.IsFixedSize; }\r
129                 }\r
130 \r
131                 bool IList.IsReadOnly { \r
132                         get { return InnerList.IsReadOnly; }\r
133                 }\r
134 \r
135                 object IList.this[int index] { \r
136                         get { return InnerList[index]; }\r
137                         set { \r
138                                 if (index < 0 || index >= InnerList.Count)\r
139                                         throw new ArgumentOutOfRangeException ("index");\r
140 \r
141                                 object oldValue;\r
142                                 // make sure we have been given a valid value\r
143                                 OnValidate(value);\r
144                                 // save a reference to the object that is in the list now\r
145                                 oldValue = InnerList[index];\r
146                                 \r
147                                 OnSet(index, oldValue, value);\r
148                                 InnerList[index] = value;\r
149                                 try {\r
150                                         OnSetComplete(index, oldValue, value);\r
151                                 } catch {\r
152                                         InnerList[index] = oldValue;\r
153                                         throw;\r
154                                 }\r
155                         }\r
156                 }\r
157         }\r
158 }\r