* support-test-*.cs: Rename from test-*-p2.cs.
[mono.git] / mcs / class / corlib / System.Collections.ObjectModel / ReadOnlyCollection.cs
1 // -*- Mode: csharp; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
2 //
3 // System.Collections.ObjectModel.ReadOnlyCollection
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.Generic;
40 using System.Runtime.InteropServices;
41
42 namespace System.Collections.ObjectModel
43 {
44         [ComVisible (false)]
45         [Serializable]
46         public class ReadOnlyCollection <T> : IList <T>, ICollection <T>, IEnumerable <T>, IList, ICollection, IEnumerable
47         {
48                 IList <T> list;
49                 object syncRoot;
50                 
51                 public ReadOnlyCollection (IList <T> list)
52                 {
53                         if (list == null)
54                                 throw new ArgumentNullException ("list");
55                         this.list = list;
56                         ICollection c = list as ICollection;
57                         syncRoot = (c != null) ? c.SyncRoot : new object ();
58                 }
59
60                 void ICollection<T>.Add (T item)
61                 {
62                         throw new NotSupportedException ();
63                 }
64                 
65                 void ICollection<T>.Clear ()
66                 {
67                         throw new NotSupportedException ();
68                 }
69
70                 public bool Contains (T item)
71                 {
72                         return list.Contains (item);
73                 }
74
75                 public void CopyTo (T [] array, int index)
76                 {
77                         list.CopyTo (array, index);
78                 }
79
80                 public IEnumerator <T> GetEnumerator ()
81                 {
82                         return list.GetEnumerator ();
83                 }
84
85                 public int IndexOf (T item)
86                 {
87                         return list.IndexOf (item);
88                 }
89
90                 void IList<T>.Insert (int index, T item)
91                 {
92                         throw new NotSupportedException ();
93                 }
94
95                 bool ICollection<T>.Remove (T item)
96                 {
97                         throw new NotSupportedException ();
98                 }
99
100                 void IList<T>.RemoveAt (int index)
101                 {
102                         throw new NotSupportedException ();
103                 }
104
105                 public int Count {
106                         get { return list.Count; }
107                 }
108
109                 protected IList<T> Items {
110                         get { return list; }
111                 }
112
113                 public T this [int index] {
114                         get { return list [index]; }
115                 }
116
117                 T IList<T>.this [int index] {
118                         set { throw new NotSupportedException (); }
119                 }
120
121                 bool ICollection<T>.IsReadOnly {
122                         get { return true; }
123                 }
124
125 #region Not generic interface implementations
126                 void ICollection.CopyTo (Array array, int arrayIndex)
127                 {
128                         T [] target = array as T [];
129                         if (target == null)
130                                 throw new ArgumentException ("array");
131                         list.CopyTo (target, arrayIndex);
132                 }
133                                 
134                 IEnumerator IEnumerable.GetEnumerator ()
135                 {
136                         return ((IEnumerable) list).GetEnumerator ();
137                 }
138                 
139                 int IList.Add (object item)
140                 {
141                         throw new NotSupportedException ();
142                 }
143                 
144                 void IList.Clear ()
145                 {
146                         throw new NotSupportedException ();
147                 }
148
149                 bool IList.Contains (object item)
150                 {
151                         if (Collection <T>.IsValidItem (item))
152                                 return list.Contains ((T) item);
153                         return false;
154                 }
155                 
156                 int IList.IndexOf (object item)
157                 {
158                         if (Collection <T>.IsValidItem (item))
159                                 return list.IndexOf ((T) item);
160                         return -1;
161                 }
162                 
163                 void IList.Insert (int index, object item)
164                 {
165                         throw new NotSupportedException ();
166                 }
167                 
168                 void IList.Remove (object item)
169                 {
170                         throw new NotSupportedException ();
171                 }
172                 
173                 void IList.RemoveAt (int index)
174                 {
175                         throw new NotSupportedException ();
176                 }
177
178                 bool ICollection.IsSynchronized {
179                         get { return Collection <T>.IsSynchronized (list); }
180                 }
181                 
182                 object ICollection.SyncRoot {
183                         get { return syncRoot; }
184                 }
185
186                 bool IList.IsFixedSize {
187                         get { return Collection <T>.IsFixedSize (list); }
188                 }
189                 
190                 bool IList.IsReadOnly {
191                         get { return true; }
192                 }
193                 
194                 object IList.this [int index] {
195                         get { return list [index]; }
196                         set { throw new NotSupportedException (); }
197                 }
198 #endregion
199         }
200 }
201 #endif