merge -r 53370:58178
[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                 public void Add (T item)
61                 {
62                         throw new NotSupportedException ();
63                 }
64                 
65                 public void 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                 public void Insert (int index, T item)
91                 {
92                         throw new NotSupportedException ();
93                 }
94
95                 public bool Remove (T item)
96                 {
97                         throw new NotSupportedException ();
98                 }
99
100                 public void RemoveAt (int index)
101                 {
102                         throw new NotSupportedException ();
103                 }
104
105                 public int Count {
106                         get { return list.Count; }
107                 }
108
109                 public T this [int index] {
110                         get { return list [index]; }
111                         set { throw new NotSupportedException (); }
112                 }
113
114                 public bool IsReadOnly {
115                         get { return true; }
116                 }
117
118 #region Not generic interface implementations
119                 void ICollection.CopyTo (Array array, int arrayIndex)
120                 {
121                         T [] target = array as T [];
122                         if (target == null)
123                                 throw new ArgumentException ("array");
124                         list.CopyTo (target, arrayIndex);
125                 }
126                                 
127                 IEnumerator IEnumerable.GetEnumerator ()
128                 {
129                         return ((IEnumerable) list).GetEnumerator ();
130                 }
131                 
132                 int IList.Add (object item)
133                 {
134                         throw new NotSupportedException ();
135                 }
136                 
137                 bool IList.Contains (object item)
138                 {
139                         if (Collection <T>.IsValidItem (item))
140                                 return list.Contains ((T) item);
141                         return false;
142                 }
143                 
144                 int IList.IndexOf (object item)
145                 {
146                         if (Collection <T>.IsValidItem (item))
147                                 return list.IndexOf ((T) item);
148                         return -1;
149                 }
150                 
151                 void IList.Insert (int index, object item)
152                 {
153                         throw new NotSupportedException ();
154                 }
155                 
156                 void IList.Remove (object item)
157                 {
158                         throw new NotSupportedException ();
159                 }
160                 
161                 bool ICollection.IsSynchronized {
162                         get { return Collection <T>.IsSynchronized (list); }
163                 }
164                 
165                 object ICollection.SyncRoot {
166                         get { return syncRoot; }
167                 }
168
169                 bool IList.IsFixedSize {
170                         get { return Collection <T>.IsFixedSize (list); }
171                 }
172                 
173                 bool IList.IsReadOnly {
174                         get { return true; }
175                 }
176                 
177                 object IList.this [int index] {
178                         get { return list [index]; }
179                         set { throw new NotSupportedException (); }
180                 }
181 #endregion
182         }
183 }
184 #endif