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