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