1 // -*- Mode: csharp; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
3 // System.Collections.ObjectModel.Collection
6 // Zoltan Varga (vargaz@gmail.com)
7 // David Waite (mass@akuma.org)
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:
25 // The above copyright notice and this permission notice shall be
26 // included in all copies or substantial portions of the Software.
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.
39 using System.Collections;
40 using System.Collections.Generic;
41 using System.Runtime.InteropServices;
43 namespace System.Collections.ObjectModel
47 public class Collection <T> : IList <T>, ICollection <T>, IEnumerable <T>, IList, ICollection, IEnumerable
54 List <T> l = new List <T> ();
55 IList l2 = l as IList;
56 syncRoot = l2.SyncRoot;
60 public Collection (IList <T> list)
63 throw new ArgumentNullException ("list");
65 ICollection l = list as ICollection;
66 syncRoot = (l != null) ? l.SyncRoot : new object ();
69 public void Add (T item)
72 InsertItem (idx, item);
80 protected virtual void ClearItems ()
85 public bool Contains (T item)
87 return list.Contains (item);
90 public void CopyTo (T [] array, int index)
92 list.CopyTo (array, index);
95 public IEnumerator <T> GetEnumerator ()
97 return list.GetEnumerator ();
100 public int IndexOf (T item)
102 return list.IndexOf (item);
105 public void Insert (int index, T item)
107 InsertItem (index, item);
110 protected virtual void InsertItem (int index, T item)
112 list.Insert (index, item);
115 protected IList<T> Items {
119 public bool Remove (T item)
121 int idx = IndexOf (item);
130 public void RemoveAt (int index)
135 protected virtual void RemoveItem (int index)
137 list.RemoveAt (index);
141 get { return list.Count; }
144 public T this [int index] {
145 get { return list [index]; }
146 set { SetItem (index, value); }
149 bool ICollection<T>.IsReadOnly {
150 get { return list.IsReadOnly; }
153 protected virtual void SetItem (int index, T item)
159 #region Helper methods for non-generic interfaces
161 internal static bool IsValidItem (object item)
163 return (item is T || (item == null && ! typeof (T).IsValueType));
166 internal static T ConvertItem (object item)
168 if (IsValidItem (item))
170 throw new ArgumentException ("item");
173 internal static void CheckWritable (IList <T> list)
176 throw new NotSupportedException ();
179 internal static bool IsSynchronized (IList <T> list)
181 ICollection c = list as ICollection;
182 return (c != null) ? c.IsSynchronized : false;
185 internal static bool IsFixedSize (IList <T> list)
187 IList l = list as IList;
188 return (l != null) ? l.IsFixedSize : false;
192 #region Not generic interface implementations
193 void ICollection.CopyTo (Array array, int arrayIndex)
195 T [] target = array as T [];
197 throw new ArgumentException ("array");
198 list.CopyTo (target, arrayIndex);
201 IEnumerator IEnumerable.GetEnumerator ()
203 return (IEnumerator) list.GetEnumerator ();
206 int IList.Add (object item)
208 int idx = list.Count;
209 InsertItem (idx, ConvertItem (item));
213 bool IList.Contains (object item)
215 if (IsValidItem (item))
216 return list.Contains ((T) item);
220 int IList.IndexOf (object item)
222 if (IsValidItem (item))
223 return list.IndexOf ((T) item);
227 void IList.Insert (int index, object item)
229 InsertItem (index, ConvertItem (item));
232 void IList.Remove (object item)
234 CheckWritable (list);
236 int idx = IndexOf (ConvertItem (item));
241 bool ICollection.IsSynchronized {
242 get { return IsSynchronized (list); }
245 object ICollection.SyncRoot {
246 get { return syncRoot; }
248 bool IList.IsFixedSize {
249 get { return IsFixedSize (list); }
252 bool IList.IsReadOnly {
253 get { return list.IsReadOnly; }
256 object IList.this [int index] {
257 get { return list [index]; }
258 set { SetItem (index, ConvertItem (value)); }