Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mcs / class / System.ServiceModel / System.ServiceModel.Channels / MessageProperties.cs
1 //
2 // MessageProperties.cs
3 //
4 // Author:
5 //      Atsushi Enomoto <atsushi@ximian.com>
6 //
7 // Copyright (C) 2005 Novell, Inc.  http://www.novell.com
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28 using System;
29 using System.Collections;
30 using System.Collections.Generic;
31 using System.Collections.ObjectModel;
32 using System.ServiceModel.Channels;
33 using System.ServiceModel.Security;
34
35 using Pair = System.Collections.Generic.KeyValuePair<string, object>;
36
37 namespace System.ServiceModel.Channels
38 {
39         public sealed class MessageProperties : IDictionary<string, object>, 
40                 ICollection<Pair>, IEnumerable<Pair>, IEnumerable, IDisposable
41         {
42                 List<Pair> list;
43
44                 public MessageProperties ()
45                 {
46                         list = new List<Pair> ();
47                 }
48
49                 public MessageProperties (MessageProperties properties)
50                 {
51                         list = new List<Pair> ();
52                         CopyProperties (properties);
53                 }
54
55                 public bool AllowOutputBatching {
56                         get {
57                                 var obj = this ["AllowOutputBatching"];
58                                 return obj != null ? (bool) obj : false;
59                         }
60                         set { this ["AllowOutputBatching"] = value; }
61                 }
62
63                 public int Count {
64                         get { return list.Count; }
65                 }
66
67                 public MessageEncoder Encoder {
68                         get { return (MessageEncoder) this ["Encoder"]; }
69                         set { this ["Encoder"] = value; }
70                 }
71
72                 public bool IsFixedSize {
73                         get { return false; }
74                 }
75
76                 public bool IsReadOnly {
77                         get { return false; }
78                 }
79
80                 public ICollection<string> Keys {
81                         get { return new ParameterKeyCollection (list); }
82                 }
83
84                 public object this [string name] {
85                         get {
86                                 for (int i = 0; i < list.Count; i++)
87                                         if (list [i].Key == name)
88                                                 return list [i].Value;
89                                 return null;
90                         }
91                         set {
92                                 for (int i = 0; i < list.Count; i++)
93                                         if (list [i].Key == name) {
94                                                 list [i] = new Pair (name, value);
95                                                 return;
96                                         }
97                                 list.Add (new Pair (name, value));
98                         }
99                 }
100
101 #if !MOBILE
102                 public SecurityMessageProperty Security {
103                         get { return (SecurityMessageProperty) this ["Security"]; }
104                         set { this ["Security"] = value; }
105                 }
106 #endif
107
108                 public ICollection<object> Values {
109                         get { return new ParameterValueCollection (list); }
110                 }
111
112                 public Uri Via {
113                         get { return (Uri) this ["Via"]; }
114                         set { this ["Via"] = value; }
115                 }
116
117                 public void Add (string name, object property)
118                 {
119                         list.Add (new Pair (name, property));
120                 }
121
122                 public void Clear ()
123                 {
124                         list.Clear ();
125                 }
126
127                 public bool ContainsKey (string name)
128                 {
129                         for (int i = 0; i < list.Count; i++)
130                                 if (list [i].Key == name)
131                                         return true;
132                         return false;
133                 }
134
135                 public void CopyProperties (MessageProperties properties)
136                 {
137                         list.AddRange (properties.list);
138                 }
139
140                 public void Dispose ()
141                 {
142                 }
143
144                 public bool Remove (string name)
145                 {
146                         for (int i = 0; i < list.Count; i++)
147                                 if (list [i].Key == name) {
148                                         list.RemoveAt (i);
149                                         return true;
150                                 }
151                         return false;
152                 }
153
154                 public bool TryGetValue (string name, out object value)
155                 {
156                         for (int i = 0; i < list.Count; i++)
157                                 if (list [i].Key == name) {
158                                         value = list [i].Value;
159                                         return true;
160                                 }
161                         value = null;
162                         return false;
163                 }
164
165                 void ICollection<Pair>.Add (Pair pair)
166                 {
167                         list.Add (pair);
168                 }
169
170                 bool ICollection<Pair>.Contains (Pair pair)
171                 {
172                         return list.Contains (pair);
173                 }
174
175                 void ICollection<Pair>.CopyTo (Pair [] array, int index)
176                 {
177                         list.CopyTo (array, index);
178                 }
179
180                 bool ICollection<Pair>.Remove (Pair pair)
181                 {
182                         return list.Remove (pair);
183                 }
184
185                 IEnumerator<Pair> IEnumerable<Pair>.GetEnumerator ()
186                 {
187                         return list.GetEnumerator ();
188                 }
189
190                 IEnumerator IEnumerable.GetEnumerator ()
191                 {
192                         return (IEnumerator) ((IEnumerable<Pair>) this).GetEnumerator ();
193                 }
194
195                 class ParameterKeyCollection : ICollection<string>
196                 {
197                         List<Pair> source;
198
199                         public ParameterKeyCollection (List<Pair> source)
200                         {
201                                 this.source = source;
202                         }
203
204                         public int Count {
205                                 get { return source.Count; }
206                         }
207
208                         public bool IsReadOnly {
209                                 get { return true; }
210                         }
211
212                         public void Add (string item)
213                         {
214                                 throw new InvalidOperationException ();
215                         }
216
217                         public void Clear ()
218                         {
219                                 throw new InvalidOperationException ();
220                         }
221
222                         public bool Contains (string item)
223                         {
224                                 for (int i = 0; i < source.Count; i++)
225                                         if (source [i].Key == item)
226                                                 return true;
227                                 return false;
228                         }
229
230                         public void CopyTo (string [] array, int index)
231                         {
232                                 for (int i = 0; i < source.Count; i++)
233                                         array [index + i] = source [i].Key;
234                         }
235
236                         public IEnumerator<string> GetEnumerator ()
237                         {
238                                 foreach (Pair p in source)
239                                         yield return p.Key;
240                         }
241
242                         IEnumerator IEnumerable.GetEnumerator ()
243                         {
244                                 foreach (Pair p in source)
245                                         yield return p.Key;
246                         }
247
248                         public bool Remove (string item)
249                         {
250                                 throw new InvalidOperationException ();
251                         }
252                 }
253
254                 class ParameterValueCollection : ICollection<object>
255                 {
256                         List<Pair> source;
257
258                         public ParameterValueCollection (List<Pair> source)
259                         {
260                                 this.source = source;
261                         }
262
263                         public int Count {
264                                 get { return source.Count; }
265                         }
266
267                         public bool IsReadOnly {
268                                 get { return true; }
269                         }
270
271                         public void Add (object item)
272                         {
273                                 throw new InvalidOperationException ();
274                         }
275
276                         public void Clear ()
277                         {
278                                 throw new InvalidOperationException ();
279                         }
280
281                         public bool Contains (object item)
282                         {
283                                 for (int i = 0; i < source.Count; i++)
284                                         if (source [i].Value == item)
285                                                 return true;
286                                 return false;
287                         }
288
289                         public void CopyTo (object [] array, int index)
290                         {
291                                 for (int i = 0; i < source.Count; i++)
292                                         array [index + i] = source [i].Value;
293                         }
294
295                         public IEnumerator<object> GetEnumerator ()
296                         {
297                                 foreach (Pair p in source)
298                                         yield return p.Value;
299                         }
300
301                         IEnumerator IEnumerable.GetEnumerator ()
302                         {
303                                 foreach (Pair p in source)
304                                         yield return p.Key;
305                         }
306
307                         public bool Remove (object item)
308                         {
309                                 throw new InvalidOperationException ();
310                         }
311                 }
312         }
313 }