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.
38 using System
.Collections
;
39 using System
.Collections
.Generic
;
40 using System
.Runtime
.InteropServices
;
42 namespace System
.Collections
.ObjectModel
46 public class Collection
<T
> : IList
<T
>, ICollection
<T
>, IEnumerable
<T
>, IList
, ICollection
, IEnumerable
53 List
<T
> l
= new List
<T
> ();
54 IList l2
= l
as IList
;
55 syncRoot
= l2
.SyncRoot
;
59 public Collection (IList
<T
> list
)
62 throw new ArgumentNullException ("list");
64 ICollection l
= list
as ICollection
;
65 syncRoot
= (l
!= null) ? l
.SyncRoot
: new object ();
68 public void Add (T item
)
71 InsertItem (idx
, item
);
79 protected virtual void ClearItems ()
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
{
118 public bool Remove (T item
)
120 int idx
= IndexOf (item
);
129 public void RemoveAt (int index
)
134 protected virtual void RemoveItem (int index
)
136 list
.RemoveAt (index
);
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
)
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
))
169 throw new ArgumentException ("item");
172 internal static void CheckWritable (IList
<T
> list
)
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;
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));
209 bool IList
.Contains (object value)
211 if (IsValidItem (value))
212 return list
.Contains ((T
) value);
216 int IList
.IndexOf (object value)
218 if (IsValidItem (value))
219 return list
.IndexOf ((T
) value);
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));
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)); }