Add license and copyright to all source files in corlib
[mono.git] / mcs / class / corlib / System.Runtime.Remoting.Channels / AggregateDictionary.cs
1 //
2 // System.Runtime.Remoting.Channels.AggregateDictionary.cs
3 //
4 // Author: Lluis Sanchez Gual (lluis@ximian.com)
5 //
6 // 2002 (C) Copyright, Novell, Inc.
7 //
8
9 //
10 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 using System.Collections;
33
34 namespace System.Runtime.Remoting.Channels
35 {
36         internal class AggregateDictionary: IDictionary
37         {
38                 IDictionary[] dictionaries;
39                 ArrayList _values;
40                 ArrayList _keys;
41                 
42                 public AggregateDictionary (IDictionary[] dics)
43                 {
44                         dictionaries = dics;
45                 }
46                 
47                 public bool IsFixedSize 
48                 { 
49                         get { return true; }
50                 }               
51                 
52                 public bool IsReadOnly 
53                 { 
54                         get { return true; }
55                 } 
56                 
57                 public object this [object key]
58                 { 
59                         get 
60                         {
61                                 foreach (IDictionary dic in dictionaries)
62                                         if (dic.Contains (key)) return dic [key];
63                                 return null;
64                         }
65                         
66                         set
67                         {
68                                 throw new NotSupportedException ();
69                         }
70                 }
71                 
72                 public ICollection Keys 
73                 { 
74                         get
75                         {
76                                 if (_keys != null) return _keys;
77                                 
78                                 _keys = new ArrayList ();
79                                 foreach (IDictionary dic in dictionaries)
80                                         _keys.AddRange (dic.Keys);
81                                 return _keys;
82                         }
83                 } 
84                 
85                 public ICollection Values 
86                 { 
87                         get
88                         {
89                                 if (_values != null) return _values;
90                                 
91                                 _values = new ArrayList ();
92                                 foreach (IDictionary dic in dictionaries)
93                                         _values.AddRange (dic.Values);
94                                 return _values;
95                         }
96                 }
97                 
98                 public void Add (object key, object value)
99                 {
100                         throw new NotSupportedException ();
101                 }
102                 
103                 public void Clear ()
104                 {
105                         throw new NotSupportedException ();
106                 }
107                 
108                 public bool Contains (object ob)
109                 {
110                         foreach (IDictionary dic in dictionaries)
111                                 if (dic.Contains (ob)) return true;
112                         return false;
113                 }
114                 
115                 public IDictionaryEnumerator GetEnumerator ()
116                 {
117                         return new AggregateEnumerator (dictionaries);
118                 }
119                 
120                 IEnumerator IEnumerable.GetEnumerator ()
121                 {
122                         return new AggregateEnumerator (dictionaries);
123                 }
124                 
125                 public void Remove (object ob)
126                 {
127                         throw new NotSupportedException ();
128                 }
129                 
130                 public void CopyTo (Array array, int index)
131                 {
132                         foreach (object ob in this)
133                                 array.SetValue (ob, index++);
134                 }
135                 
136                 public int Count 
137                 { 
138                         get
139                         {
140                                 int c = 0;
141                                 foreach (IDictionary dic in dictionaries)
142                                         c += dic.Count;
143                                 return c;
144                         }
145                 }
146                 
147                 public bool IsSynchronized 
148                 { 
149                         get { return false; }
150                 }
151                 
152                 public object SyncRoot 
153                 { 
154                         get { return this; }
155                 } 
156         }
157         
158         internal class AggregateEnumerator: IDictionaryEnumerator
159         {
160                 IDictionary[] dictionaries;
161                 int pos = 0;
162                 IDictionaryEnumerator currente;
163                 
164                 public AggregateEnumerator (IDictionary[] dics)
165                 {
166                         dictionaries = dics;
167                         Reset ();
168                 }
169                 
170                 public DictionaryEntry Entry 
171                 { 
172                         get { return currente.Entry; }
173                 }
174                 
175                 public object Key 
176                 {
177                         get { return currente.Key; }
178                 }
179                 
180                 public object Value 
181                 { 
182                         get { return currente.Value; }
183                 }
184                 
185                 public object Current 
186                 { 
187                         get { return currente.Current; }
188                 }
189                 
190                 public bool MoveNext ()
191                 {
192                         if (pos >= dictionaries.Length) return false;
193
194                         if (!currente.MoveNext()) 
195                         {
196                                 pos++;
197                                 if (pos >= dictionaries.Length) return false;
198                                 currente = dictionaries [pos].GetEnumerator ();
199                                 return MoveNext ();
200                         }
201                         
202                         return true;
203                 }
204                 
205                 public void Reset ()
206                 {
207                         pos = 0;
208                         if (dictionaries.Length > 0)
209                                 currente = dictionaries [0].GetEnumerator ();
210                 }
211         }
212 }