2010-04-06 Jb Evain <jbevain@novell.com>
[mcs.git] / class / System.ServiceModel / System.ServiceModel.Dispatcher / MessageFilterTable.cs
blobb0f4021300292bd10deb855efdae037b40924406
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.
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;
35 namespace System.ServiceModel.Dispatcher
37 [DataContract]
38 public class MessageFilterTable<TFilterData> : IMessageFilterTable<TFilterData>, IDictionary<MessageFilter,TFilterData>,
39 ICollection<KeyValuePair<MessageFilter, TFilterData>>, IEnumerable<KeyValuePair<MessageFilter,TFilterData>>, IEnumerable
42 int default_priority;
43 Dictionary<MessageFilter, TFilterData> table;
44 Dictionary<MessageFilter, int> priority_list;
46 public MessageFilterTable ()
47 : this (0) {} // 0 is the default
49 public MessageFilterTable (int default_priority)
51 this.default_priority = default_priority;
52 table = new Dictionary<MessageFilter, TFilterData> ();
53 priority_list = new Dictionary<MessageFilter, int> ();
56 public void Add (KeyValuePair<MessageFilter, TFilterData> item)
58 this.Add (item.Key, item.Value, default_priority);
61 public void Add (MessageFilter filter, TFilterData data)
63 this.Add (filter, data, default_priority);
66 public void Add (MessageFilter filter, TFilterData data, int priority)
68 table.Add (filter, data);
69 priority_list.Add (filter, priority);
72 public void Clear ()
74 table.Clear ();
75 priority_list.Clear ();
78 public bool Contains (KeyValuePair<MessageFilter, TFilterData> item)
80 return ContainsKey (item.Key);
83 public bool ContainsKey (MessageFilter filter)
85 return table.ContainsKey (filter);
88 public void CopyTo (KeyValuePair<MessageFilter, TFilterData> [] array, int index)
90 ((ICollection) table).CopyTo (array, index);
93 protected virtual IMessageFilterTable<TFilterData> CreateFilterTable (MessageFilter filter)
95 return filter.CreateFilterTable<TFilterData> ();
98 public IEnumerator<KeyValuePair<MessageFilter, TFilterData>> GetEnumerator ()
100 return table.GetEnumerator ();
103 public int GetPriority (MessageFilter filter)
105 return priority_list [filter];
108 public bool GetMatchingFilter (Message message, out MessageFilter result)
110 if (message == null)
111 throw new ArgumentNullException ("message is null");
112 result = null;
114 int count = 0;
115 foreach (MessageFilter f in table.Keys) {
116 if (!f.Match (message))
117 continue;
119 if (count > 1)
120 throw new MultipleFilterMatchesException (
121 "More than one filter matches.");
123 count++;
124 result = f;
126 return count == 1;
129 public bool GetMatchingFilter (MessageBuffer buffer, out MessageFilter result)
131 return GetMatchingFilter (buffer.CreateMessage (), out result);
134 [MonoTODO ("Consider priority")]
135 public bool GetMatchingFilters (Message message, ICollection<MessageFilter> results)
137 if (message == null)
138 throw new ArgumentNullException ("message is null.");
140 int count = 0;
141 foreach (MessageFilter f in table.Keys)
142 if (f.Match (message)) {
143 results.Add (f);
144 count++;
146 return count != 0;
149 public bool GetMatchingFilters (MessageBuffer buffer, ICollection<MessageFilter> results)
151 return GetMatchingFilters (buffer.CreateMessage (), results);
154 [MonoTODO ("Consider priority")]
155 public bool GetMatchingValue (Message message, out TFilterData data)
157 if (message == null)
158 throw new ArgumentNullException ("message is null");
159 data = default (TFilterData);
161 int count = 0;
162 foreach (MessageFilter f in table.Keys) {
163 if (!f.Match (message))
164 continue;
166 if (count > 1)
167 throw new MultipleFilterMatchesException (
168 "More than one filter matches.");
170 count++;
171 data = table [f];
173 return count == 1;
176 public bool GetMatchingValue (MessageBuffer buffer, out TFilterData data)
178 return GetMatchingValue (buffer.CreateMessage (), out data);
181 public bool GetMatchingValues (Message message, ICollection<TFilterData> results)
183 if (message == null)
184 throw new ArgumentNullException ("message is null.");
186 int count = 0;
187 foreach (MessageFilter f in table.Keys)
188 if (f.Match (message)) {
189 results.Add (table [f]);
190 count++;
192 return count != 0;
195 public bool GetMatchingValues (MessageBuffer buffer, ICollection<TFilterData> results)
197 return GetMatchingValues (buffer.CreateMessage (), results);
200 public bool Remove (MessageFilter filter)
202 if (filter == null)
203 throw new ArgumentNullException ("filter is null.");
205 if (!table.ContainsKey (filter))
206 return false;
208 table.Remove (filter);
209 priority_list.Remove (filter);
210 return true;
213 public bool Remove (KeyValuePair<MessageFilter, TFilterData> item)
215 return Remove (item.Key);
218 IEnumerator IEnumerable.GetEnumerator ()
220 return (IEnumerator) table.GetEnumerator ();
223 public bool TryGetValue (MessageFilter filter, out TFilterData data)
225 return table.TryGetValue (filter, out data);
228 public int Count { get { return table.Count; } }
230 [DataMember]
231 public int DefaultPriority {
232 get { return default_priority; }
233 set { default_priority = value; }
236 public bool IsReadOnly { get { return false; } }
238 public TFilterData this [MessageFilter filter] {
239 get { return table [filter]; }
240 set { table [filter] = value; }
243 public ICollection<MessageFilter> Keys {
244 get { return table.Keys; }
247 public ICollection<TFilterData> Values {
248 get { return table.Values; }