2008-07-13 Nestor Salceda <nestor.salceda@gmail.com>
[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 #if NET_2_0
37         [System.Runtime.InteropServices.ComVisible (true)]
38 #endif
39         internal class AggregateDictionary: IDictionary
40         {
41                 IDictionary[] dictionaries;
42                 ArrayList _values;
43                 ArrayList _keys;
44                 
45                 public AggregateDictionary (IDictionary[] dics)
46                 {
47                         dictionaries = dics;
48                 }
49                 
50                 public bool IsFixedSize 
51                 { 
52                         get { return true; }
53                 }               
54                 
55                 public bool IsReadOnly 
56                 { 
57                         get { return true; }
58                 } 
59                 
60                 public object this [object key]
61                 { 
62                         get 
63                         {
64                                 foreach (IDictionary dic in dictionaries)
65                                         if (dic.Contains (key)) return dic [key];
66                                 return null;
67                         }
68                         
69                         set
70                         {
71                                 throw new NotSupportedException ();
72                         }
73                 }
74                 
75                 public ICollection Keys 
76                 { 
77                         get
78                         {
79                                 if (_keys != null) return _keys;
80                                 
81                                 _keys = new ArrayList ();
82                                 foreach (IDictionary dic in dictionaries)
83                                         _keys.AddRange (dic.Keys);
84                                 return _keys;
85                         }
86                 } 
87                 
88                 public ICollection Values 
89                 { 
90                         get
91                         {
92                                 if (_values != null) return _values;
93                                 
94                                 _values = new ArrayList ();
95                                 foreach (IDictionary dic in dictionaries)
96                                         _values.AddRange (dic.Values);
97                                 return _values;
98                         }
99                 }
100                 
101                 public void Add (object key, object value)
102                 {
103                         throw new NotSupportedException ();
104                 }
105                 
106                 public void Clear ()
107                 {
108                         throw new NotSupportedException ();
109                 }
110                 
111                 public bool Contains (object ob)
112                 {
113                         foreach (IDictionary dic in dictionaries)
114                                 if (dic.Contains (ob)) return true;
115                         return false;
116                 }
117                 
118                 public IDictionaryEnumerator GetEnumerator ()
119                 {
120                         return new AggregateEnumerator (dictionaries);
121                 }
122                 
123                 IEnumerator IEnumerable.GetEnumerator ()
124                 {
125                         return new AggregateEnumerator (dictionaries);
126                 }
127                 
128                 public void Remove (object ob)
129                 {
130                         throw new NotSupportedException ();
131                 }
132                 
133                 public void CopyTo (Array array, int index)
134                 {
135                         foreach (object ob in this)
136                                 array.SetValue (ob, index++);
137                 }
138                 
139                 public int Count 
140                 { 
141                         get
142                         {
143                                 int c = 0;
144                                 foreach (IDictionary dic in dictionaries)
145                                         c += dic.Count;
146                                 return c;
147                         }
148                 }
149                 
150                 public bool IsSynchronized 
151                 { 
152                         get { return false; }
153                 }
154                 
155                 public object SyncRoot 
156                 { 
157                         get { return this; }
158                 } 
159         }
160         
161         internal class AggregateEnumerator: IDictionaryEnumerator
162         {
163                 IDictionary[] dictionaries;
164                 int pos = 0;
165                 IDictionaryEnumerator currente;
166                 
167                 public AggregateEnumerator (IDictionary[] dics)
168                 {
169                         dictionaries = dics;
170                         Reset ();
171                 }
172                 
173                 public DictionaryEntry Entry 
174                 { 
175                         get { return currente.Entry; }
176                 }
177                 
178                 public object Key 
179                 {
180                         get { return currente.Key; }
181                 }
182                 
183                 public object Value 
184                 { 
185                         get { return currente.Value; }
186                 }
187                 
188                 public object Current 
189                 { 
190                         get { return currente.Current; }
191                 }
192                 
193                 public bool MoveNext ()
194                 {
195                         if (pos >= dictionaries.Length) return false;
196
197                         if (!currente.MoveNext()) 
198                         {
199                                 pos++;
200                                 if (pos >= dictionaries.Length) return false;
201                                 currente = dictionaries [pos].GetEnumerator ();
202                                 return MoveNext ();
203                         }
204                         
205                         return true;
206                 }
207                 
208                 public void Reset ()
209                 {
210                         pos = 0;
211                         if (dictionaries.Length > 0)
212                                 currente = dictionaries [0].GetEnumerator ();
213                 }
214         }
215 }