2010-05-27 Jb Evain <jbevain@novell.com>
[mcs.git] / class / corlib / System.Collections.ObjectModel / Collection.cs
bloba9d8d92c9825b6c00fa00f69b7fbb5545af56bc8
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
14 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
15 // Copyright (C) 2005 David Waite
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.
37 using System;
38 using System.Collections;
39 using System.Collections.Generic;
40 using System.Runtime.InteropServices;
42 namespace System.Collections.ObjectModel
44 [ComVisible (false)]
45 [Serializable]
46 public class Collection <T> : IList <T>, ICollection <T>, IEnumerable <T>, IList, ICollection, IEnumerable
48 IList <T> list;
49 object syncRoot;
51 public Collection ()
53 List <T> l = new List <T> ();
54 IList l2 = l as IList;
55 syncRoot = l2.SyncRoot;
56 list = l;
59 public Collection (IList <T> list)
61 if (list == null)
62 throw new ArgumentNullException ("list");
63 this.list = list;
64 ICollection l = list as ICollection;
65 syncRoot = (l != null) ? l.SyncRoot : new object ();
68 public void Add (T item)
70 int idx = list.Count;
71 InsertItem (idx, item);
74 public void Clear ()
76 ClearItems ();
79 protected virtual void ClearItems ()
81 list.Clear ();
84 public bool Contains (T item)
86 return list.Contains (item);
89 public void CopyTo (T [] array, int index)
91 list.CopyTo (array, index);
94 public IEnumerator <T> GetEnumerator ()
96 return list.GetEnumerator ();
99 public int IndexOf (T item)
101 return list.IndexOf (item);
104 public void Insert (int index, T item)
106 InsertItem (index, item);
109 protected virtual void InsertItem (int index, T item)
111 list.Insert (index, item);
114 protected IList<T> Items {
115 get { return list; }
118 public bool Remove (T item)
120 int idx = IndexOf (item);
121 if (idx == -1)
122 return false;
124 RemoveItem (idx);
126 return true;
129 public void RemoveAt (int index)
131 RemoveItem (index);
134 protected virtual void RemoveItem (int index)
136 list.RemoveAt (index);
139 public int Count {
140 get { return list.Count; }
143 public T this [int index] {
144 get { return list [index]; }
145 set { SetItem (index, value); }
148 bool ICollection<T>.IsReadOnly {
149 get { return list.IsReadOnly; }
152 protected virtual void SetItem (int index, T item)
154 list[index] = item;
158 #region Helper methods for non-generic interfaces
160 internal static bool IsValidItem (object item)
162 return (item is T || (item == null && ! typeof (T).IsValueType));
165 internal static T ConvertItem (object item)
167 if (IsValidItem (item))
168 return (T)item;
169 throw new ArgumentException ("item");
172 internal static void CheckWritable (IList <T> list)
174 if (list.IsReadOnly)
175 throw new NotSupportedException ();
178 internal static bool IsSynchronized (IList <T> list)
180 ICollection c = list as ICollection;
181 return (c != null) ? c.IsSynchronized : false;
184 internal static bool IsFixedSize (IList <T> list)
186 IList l = list as IList;
187 return (l != null) ? l.IsFixedSize : false;
189 #endregion
191 #region Not generic interface implementations
192 void ICollection.CopyTo (Array array, int index)
194 ((ICollection)list).CopyTo (array, index);
197 IEnumerator IEnumerable.GetEnumerator ()
199 return (IEnumerator) list.GetEnumerator ();
202 int IList.Add (object value)
204 int idx = list.Count;
205 InsertItem (idx, ConvertItem (value));
206 return idx;
209 bool IList.Contains (object value)
211 if (IsValidItem (value))
212 return list.Contains ((T) value);
213 return false;
216 int IList.IndexOf (object value)
218 if (IsValidItem (value))
219 return list.IndexOf ((T) value);
220 return -1;
223 void IList.Insert (int index, object value)
225 InsertItem (index, ConvertItem (value));
228 void IList.Remove (object value)
230 CheckWritable (list);
232 int idx = IndexOf (ConvertItem (value));
234 RemoveItem (idx);
237 bool ICollection.IsSynchronized {
238 get { return IsSynchronized (list); }
241 object ICollection.SyncRoot {
242 get { return syncRoot; }
244 bool IList.IsFixedSize {
245 get { return IsFixedSize (list); }
248 bool IList.IsReadOnly {
249 get { return list.IsReadOnly; }
252 object IList.this [int index] {
253 get { return list [index]; }
254 set { SetItem (index, ConvertItem (value)); }
256 #endregion