New test.
[mono.git] / mcs / class / corlib / System.Collections.ObjectModel / Collection.cs
1 // -*- Mode: csharp; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
2 //
3 // System.Collections.ObjectModel.Collection
4 //
5 // Author:
6 //    Zoltan Varga (vargaz@gmail.com)
7 //    David Waite (mass@akuma.org)
8 //
9 // (C) 2005 Novell, Inc.
10 // (C) 2005 David Waite
11 //
12
13 //
14 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
15 // Copyright (C) 2005 David Waite
16 //
17 // Permission is hereby granted, free of charge, to any person obtaining
18 // a copy of this software and associated documentation files (the
19 // "Software"), to deal in the Software without restriction, including
20 // without limitation the rights to use, copy, modify, merge, publish,
21 // distribute, sublicense, and/or sell copies of the Software, and to
22 // permit persons to whom the Software is furnished to do so, subject to
23 // the following conditions:
24 // 
25 // The above copyright notice and this permission notice shall be
26 // included in all copies or substantial portions of the Software.
27 // 
28 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
32 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
33 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 //
36
37 #if NET_2_0
38 using System;
39 using System.Collections;
40 using System.Collections.Generic;
41 using System.Runtime.InteropServices;
42
43 namespace System.Collections.ObjectModel
44 {
45         [ComVisible (false)]
46         [Serializable]
47         public class Collection <T> : IList <T>, ICollection <T>, IEnumerable <T>, IList, ICollection, IEnumerable
48         {
49                 IList <T> list;
50                 object syncRoot;
51                 
52                 public Collection ()
53                 {
54                         List <T> l = new List <T> ();
55                         IList l2 = l as IList;
56                         syncRoot = l2.SyncRoot;
57                         list = l;
58                 }
59
60                 public Collection (IList <T> list)
61                 {
62                         if (list == null)
63                                 throw new ArgumentNullException ("list");
64                         this.list = list;
65                         ICollection l = list as ICollection;
66                         syncRoot = (l != null) ? l.SyncRoot : new object ();
67                 }
68
69                 public void Add (T item)
70                 {
71                         int idx = list.Count;
72                         InsertItem (idx, item);
73                 }
74
75                 public void Clear ()
76                 {
77                         ClearItems ();
78                 }
79
80                 protected virtual void ClearItems ()
81                 {
82                         list.Clear ();
83                 }
84
85                 public bool Contains (T item)
86                 {
87                         return list.Contains (item);
88                 }
89
90                 public void CopyTo (T [] array, int index)
91                 {
92                         list.CopyTo (array, index);
93                 }
94
95                 public IEnumerator <T> GetEnumerator ()
96                 {
97                         return list.GetEnumerator ();
98                 }
99
100                 public int IndexOf (T item)
101                 {
102                         return list.IndexOf (item);
103                 }
104
105                 public void Insert (int index, T item)
106                 {
107                         InsertItem (index, item);
108                 }
109
110                 protected virtual void InsertItem (int index, T item)
111                 {
112                         list.Insert (index, item);
113                 }
114
115                 protected IList<T> Items {
116                         get { return list; }
117                 }
118
119                 public bool Remove (T item)
120                 {
121                         int idx = IndexOf (item);
122                         if (idx == -1) 
123                                 return false;
124                         
125                         RemoveItem (idx);
126                         
127                         return true;
128                 }
129
130                 public void RemoveAt (int index)
131                 {
132                         RemoveItem (index);
133                 }
134
135                 protected virtual void RemoveItem (int index)
136                 {
137                         list.RemoveAt (index);
138                 }
139
140                 public int Count {
141                         get { return list.Count; }
142                 }
143
144                 public T this [int index] {
145                         get { return list [index]; }
146                         set { SetItem (index, value); }
147                 }
148
149                 bool ICollection<T>.IsReadOnly {
150                         get { return list.IsReadOnly; }
151                 }
152
153                 protected virtual void SetItem (int index, T item)
154                 {
155                         list[index] = item;
156                 }
157
158                 
159 #region Helper methods for non-generic interfaces
160                 
161                 internal static bool IsValidItem (object item)
162                 {
163                         return (item is T || (item == null && ! typeof (T).IsValueType));
164                 }
165                 
166                 internal static T ConvertItem (object item)
167                 {
168                         if (IsValidItem (item))
169                                 return (T)item;
170                         throw new ArgumentException ("item");
171                 }
172                 
173                 internal static void CheckWritable (IList <T> list)
174                 {
175                         if (list.IsReadOnly)
176                                 throw new NotSupportedException ();
177                 }
178                 
179                 internal static bool IsSynchronized (IList <T> list)
180                 {
181                         ICollection c = list as ICollection;
182                         return (c != null) ? c.IsSynchronized : false;
183                 }
184                 
185                 internal static bool IsFixedSize (IList <T> list)
186                 {
187                         IList l = list as IList;
188                         return (l != null) ? l.IsFixedSize : false;
189                 }
190 #endregion
191
192 #region Not generic interface implementations
193                 void ICollection.CopyTo (Array array, int arrayIndex)
194                 {
195                         T [] target = array as T [];
196                         if (target == null)
197                                 throw new ArgumentException ("array");
198                         list.CopyTo (target, arrayIndex);
199                 }
200                 
201                 IEnumerator IEnumerable.GetEnumerator ()
202                 {
203                         return (IEnumerator) list.GetEnumerator ();
204                 }
205                                 
206                 int IList.Add (object item)
207                 {
208                         int idx = list.Count;
209                         InsertItem (idx, ConvertItem (item));
210                         return idx;
211                 }
212                 
213                 bool IList.Contains (object item)
214                 {
215                         if (IsValidItem (item))
216                                 return list.Contains ((T) item);
217                         return false;
218                 }
219                 
220                 int IList.IndexOf (object item)
221                 {
222                         if (IsValidItem (item))
223                                 return list.IndexOf ((T) item);
224                         return -1;
225                 }
226                 
227                 void IList.Insert (int index, object item)
228                 {
229                         InsertItem (index, ConvertItem (item));
230                 }
231                 
232                 void IList.Remove (object item)
233                 {
234                         CheckWritable (list);
235
236                         int idx = IndexOf (ConvertItem (item));
237
238                         RemoveItem (idx);
239                 }
240                 
241                 bool ICollection.IsSynchronized {
242                         get { return IsSynchronized (list); }
243                 }
244                 
245                 object ICollection.SyncRoot {
246                         get { return syncRoot; }
247                 }
248                 bool IList.IsFixedSize {
249                         get { return IsFixedSize (list); }
250                 }
251                 
252                 bool IList.IsReadOnly {
253                         get { return list.IsReadOnly; }
254                 }
255                 
256                 object IList.this [int index] {
257                         get { return list [index]; }
258                         set { SetItem (index, ConvertItem (value)); }
259                 }
260 #endregion
261         }
262 }
263 #endif