2008-01-20 Juraj Skripsky <js@hotfeet.ch>
[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                 
50                 public ReadOnlyCollection (IList <T> list)
51                 {
52                         if (list == null)
53                                 throw new ArgumentNullException ("list");
54                         this.list = list;
55                 }
56
57                 void ICollection<T>.Add (T item)
58                 {
59                         throw new NotSupportedException ();
60                 }
61                 
62                 void ICollection<T>.Clear ()
63                 {
64                         throw new NotSupportedException ();
65                 }
66
67                 public bool Contains (T item)
68                 {
69                         return list.Contains (item);
70                 }
71
72                 public void CopyTo (T [] array, int index)
73                 {
74                         list.CopyTo (array, index);
75                 }
76
77                 public IEnumerator <T> GetEnumerator ()
78                 {
79                         return list.GetEnumerator ();
80                 }
81
82                 public int IndexOf (T item)
83                 {
84                         return list.IndexOf (item);
85                 }
86
87                 void IList<T>.Insert (int index, T item)
88                 {
89                         throw new NotSupportedException ();
90                 }
91
92                 bool ICollection<T>.Remove (T item)
93                 {
94                         throw new NotSupportedException ();
95                 }
96
97                 void IList<T>.RemoveAt (int index)
98                 {
99                         throw new NotSupportedException ();
100                 }
101
102                 public int Count {
103                         get { return list.Count; }
104                 }
105
106                 protected IList<T> Items {
107                         get { return list; }
108                 }
109
110                 public T this [int index] {
111                         get { return list [index]; }
112                 }
113
114                 T IList<T>.this [int index] {
115                         get { return this [index]; }
116                         set { throw new NotSupportedException (); }
117                 }
118
119                 bool ICollection<T>.IsReadOnly {
120                         get { return true; }
121                 }
122
123 #region Not generic interface implementations
124                 void ICollection.CopyTo (Array array, int arrayIndex)
125                 {
126                         ((ICollection)list).CopyTo (array, arrayIndex);
127                 }
128                                 
129                 IEnumerator IEnumerable.GetEnumerator ()
130                 {
131                         return ((IEnumerable) list).GetEnumerator ();
132                 }
133                 
134                 int IList.Add (object item)
135                 {
136                         throw new NotSupportedException ();
137                 }
138                 
139                 void IList.Clear ()
140                 {
141                         throw new NotSupportedException ();
142                 }
143
144                 bool IList.Contains (object item)
145                 {
146                         if (Collection <T>.IsValidItem (item))
147                                 return list.Contains ((T) item);
148                         return false;
149                 }
150                 
151                 int IList.IndexOf (object item)
152                 {
153                         if (Collection <T>.IsValidItem (item))
154                                 return list.IndexOf ((T) item);
155                         return -1;
156                 }
157                 
158                 void IList.Insert (int index, object item)
159                 {
160                         throw new NotSupportedException ();
161                 }
162                 
163                 void IList.Remove (object item)
164                 {
165                         throw new NotSupportedException ();
166                 }
167                 
168                 void IList.RemoveAt (int index)
169                 {
170                         throw new NotSupportedException ();
171                 }
172
173                 bool ICollection.IsSynchronized {
174                         get { return false; }
175                 }
176                 
177                 object ICollection.SyncRoot {
178                         get { return this; }
179                 }
180
181                 bool IList.IsFixedSize {
182                         get { return true; }
183                 }
184                 
185                 bool IList.IsReadOnly {
186                         get { return true; }
187                 }
188                 
189                 object IList.this [int index] {
190                         get { return list [index]; }
191                         set { throw new NotSupportedException (); }
192                 }
193 #endregion
194         }
195 }
196 #endif