Wed May 1 17:05:40 CEST 2002 Paolo Molaro <lupus@ximian.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 myList;\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.myList = new ArrayList();\r
42                 }\r
43                 \r
44                 // Protected Instance Properties\r
45                 protected ArrayList InnerList {get { return this.myList; } }\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                         OnInsertComplete(newPosition, value);\r
86                         return newPosition;\r
87                 }\r
88                 \r
89                 bool IList.Contains (object value) {\r
90                         return InnerList.Contains(value);\r
91                 }\r
92 \r
93                 int IList.IndexOf (object value) {\r
94                         return InnerList.IndexOf(value);\r
95                 }\r
96 \r
97                 void IList.Insert (int index, object value) {\r
98                         OnValidate(value);\r
99                         OnInsert(index, value);\r
100                         InnerList.Insert(index, value);\r
101                         OnInsertComplete(index, value);\r
102                 }\r
103 \r
104                 void IList.Remove (object value) {\r
105                         int removeIndex;\r
106                         OnValidate(value);\r
107                         removeIndex = InnerList.IndexOf(value);\r
108                         OnRemove(removeIndex, value);\r
109                         InnerList.Remove(value);\r
110                         OnRemoveComplete(removeIndex, value);\r
111                 }\r
112 \r
113                 // IList properties\r
114                 bool IList.IsFixedSize { \r
115                         get { return InnerList.IsFixedSize; }\r
116                 }\r
117 \r
118                 bool IList.IsReadOnly { \r
119                         get { return InnerList.IsReadOnly; }\r
120                 }\r
121 \r
122                 object IList.this[int index] { \r
123                         get { return InnerList[index]; }\r
124                         set { \r
125                                 object oldValue;\r
126                                 // make sure we have been given a valid value\r
127                                 OnValidate(value);\r
128                                 // save a reference to the object that is in the list now\r
129                                 oldValue = InnerList[index];\r
130                                 \r
131                                 OnSet(index, oldValue, value);\r
132                                 InnerList[index] = value;\r
133                                 OnSetComplete(index, oldValue, value);\r
134                         }\r
135                 }\r
136         }\r
137 }\r