Merge pull request #4540 from kumpera/android-changes-part1
[mono.git] / mcs / class / System.ServiceModel / System.ServiceModel.Dispatcher / MessageFilterTable.cs
1 //
2 // System.ServiceModel.MessageFilterTable.cs
3 //
4 // Author: Duncan Mak (duncan@novell.com)
5 //
6 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining
9 // a copy of this software and associated documentation files (the
10 // "Software"), to deal in the Software without restriction, including
11 // without limitation the rights to use, copy, modify, merge, publish,
12 // distribute, sublicense, and/or sell copies of the Software, and to
13 // permit persons to whom the Software is furnished to do so, subject to
14 // the following conditions:
15 // 
16 // The above copyright notice and this permission notice shall be
17 // included in all copies or substantial portions of the Software.
18 // 
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 //
27
28 using System;
29 using System.Collections;
30 using System.Collections.Generic;
31 using System.Runtime.Serialization;
32 using System.ServiceModel;
33 using System.ServiceModel.Channels;
34
35 namespace System.ServiceModel.Dispatcher
36 {
37         [DataContract]
38         public class MessageFilterTable<TFilterData> : IMessageFilterTable<TFilterData>, IDictionary<MessageFilter,TFilterData>,
39                 ICollection<KeyValuePair<MessageFilter, TFilterData>>, IEnumerable<KeyValuePair<MessageFilter,TFilterData>>, IEnumerable
40         {
41
42                 int default_priority;
43                 Dictionary<MessageFilter, TFilterData> table;
44                 Dictionary<MessageFilter, int> priority_list;
45                 
46                 public MessageFilterTable ()
47                         : this (0) {} // 0 is the default
48
49                 public MessageFilterTable (int defaultPriority)
50                 {
51                         this.default_priority = defaultPriority;
52                         table = new Dictionary<MessageFilter, TFilterData> ();
53                         priority_list = new Dictionary<MessageFilter, int> ();
54                 }
55
56                 public void Add (KeyValuePair<MessageFilter, TFilterData> item)
57                 {
58                         this.Add (item.Key, item.Value, default_priority);
59                 }
60
61                 public void Add (MessageFilter filter, TFilterData data)
62                 {
63                         this.Add (filter, data, default_priority);
64                 }
65
66                 public void Add (MessageFilter filter, TFilterData data, int priority)
67                 {
68                         table.Add (filter, data);
69                         priority_list.Add (filter, priority);
70                 }
71
72                 public void Clear ()
73                 {
74                         table.Clear ();
75                         priority_list.Clear ();
76                 }
77
78                 public bool Contains (KeyValuePair<MessageFilter, TFilterData> item)
79                 {
80                         return ContainsKey (item.Key);
81                 }
82
83                 public bool ContainsKey (MessageFilter filter)
84                 {
85                         return table.ContainsKey (filter);
86                 }
87
88                 public void CopyTo (KeyValuePair<MessageFilter, TFilterData> [] array, int arrayIndex)
89                 {
90                         ((ICollection) table).CopyTo (array, arrayIndex);
91                 }
92
93                 protected virtual IMessageFilterTable<TFilterData> CreateFilterTable (MessageFilter filter)
94                 {
95                         return filter.CreateFilterTable<TFilterData> ();
96                 }
97
98                 public IEnumerator<KeyValuePair<MessageFilter, TFilterData>> GetEnumerator ()
99                 {
100                         return table.GetEnumerator ();
101                 }
102
103                 public int GetPriority (MessageFilter filter)
104                 {
105                         return priority_list [filter];
106                 }
107
108                 public bool GetMatchingFilter (Message message, out MessageFilter filter)
109                 {
110                         if (message == null)
111                                 throw new ArgumentNullException ("message is null");
112                         filter = null;
113
114                         int count = 0;
115                         foreach (MessageFilter f in table.Keys) {
116                                 if (!f.Match (message))
117                                         continue;
118
119                                 if (count > 1)
120                                         throw new MultipleFilterMatchesException (
121                                             "More than one filter matches.");
122
123                                 count++;
124                                 filter = f;
125                         }
126                         return count == 1;
127                 }
128
129                 public bool GetMatchingFilter (MessageBuffer buffer, out MessageFilter filter)
130                 {
131                         return GetMatchingFilter (buffer.CreateMessage (), out filter);
132                 }
133
134                 [MonoTODO ("Consider priority")]                
135                 public bool GetMatchingFilters (Message message, ICollection<MessageFilter> results)
136                 {
137                         if (message == null)
138                                 throw new ArgumentNullException ("message is null.");
139
140                         int count = 0;
141                         foreach (MessageFilter f in table.Keys)
142                                 if (f.Match (message)) {
143                                         results.Add (f);
144                                         count++;
145                                 }
146                         return count != 0;
147                 }
148
149                 public bool GetMatchingFilters (MessageBuffer buffer, ICollection<MessageFilter> results)
150                 {
151                         return GetMatchingFilters (buffer.CreateMessage (), results);
152                 }
153
154                 [MonoTODO ("Consider priority")]
155                 public bool GetMatchingValue (Message message, out TFilterData data)
156                 {
157                         if (message == null)
158                                 throw new ArgumentNullException ("message is null");
159                         data = default (TFilterData);
160
161                         int count = 0;
162                         foreach (MessageFilter f in table.Keys) {
163                                 if (!f.Match (message))
164                                         continue;
165
166                                 if (count > 1)
167                                         throw new MultipleFilterMatchesException (
168                                             "More than one filter matches.");
169
170                                 count++;
171                                 data = table [f];
172                         }
173                         return count == 1;
174                 }
175
176                 public bool GetMatchingValue (MessageBuffer buffer, out TFilterData data)
177                 {
178                         return GetMatchingValue (buffer.CreateMessage (), out data);
179                 }
180
181                 public bool GetMatchingValues (Message message, ICollection<TFilterData> results)
182                 {
183                         if (message == null)
184                                 throw new ArgumentNullException ("message is null.");
185
186                         int count = 0;
187                         foreach (MessageFilter f in table.Keys)
188                                 if (f.Match (message)) {
189                                         results.Add (table [f]);
190                                         count++;
191                                 }
192                         return count != 0;
193                 }
194
195                 public bool GetMatchingValues (MessageBuffer buffer, ICollection<TFilterData> results)
196                 {
197                         return GetMatchingValues (buffer.CreateMessage (), results);
198                 }
199
200                 public bool Remove (MessageFilter filter)
201                 {
202                         if (filter == null)
203                                 throw new ArgumentNullException ("filter is null.");
204
205                         if (!table.ContainsKey (filter))
206                                 return false;
207                         
208                         table.Remove (filter);
209                         priority_list.Remove (filter);
210                         return true;
211                 }
212
213                 public bool Remove (KeyValuePair<MessageFilter, TFilterData> item)
214                 {
215                         return Remove (item.Key);
216                 }
217
218                 IEnumerator IEnumerable.GetEnumerator ()
219                 {
220                         return (IEnumerator) table.GetEnumerator ();
221                 }
222
223                 public bool TryGetValue (MessageFilter filter, out TFilterData data)
224                 {
225                         return table.TryGetValue (filter, out data);
226                 }
227
228                 public int Count { get { return table.Count; } }
229
230                 [DataMember]
231                 public int DefaultPriority {
232                         get { return default_priority; }
233                         set { default_priority = value; }
234                 }
235
236                 public bool IsReadOnly { get { return false; } }
237
238                 public TFilterData this [MessageFilter filter] {
239                         get { return table [filter]; }
240                         set { table [filter] = value; }
241                 }
242
243                 public ICollection<MessageFilter> Keys {
244                         get { return table.Keys; }
245                 }
246
247                 public ICollection<TFilterData> Values {
248                         get { return table.Values; }
249                 }
250         }
251 }