Version
[pithos-ms-client] / trunk / Libraries / Json40r2 / Source / Src / Newtonsoft.Json / Utilities / DictionaryWrapper.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Collections;
6 using System.Threading;
7
8 namespace Newtonsoft.Json.Utilities
9 {
10   internal interface IWrappedDictionary : IDictionary
11   {
12     object UnderlyingDictionary { get; }
13   }
14
15   internal class DictionaryWrapper<TKey, TValue> : IDictionary<TKey, TValue>, IWrappedDictionary
16   {
17     private readonly IDictionary _dictionary;
18     private readonly IDictionary<TKey, TValue> _genericDictionary;
19     private object _syncRoot;
20
21     public DictionaryWrapper(IDictionary dictionary)
22     {
23       ValidationUtils.ArgumentNotNull(dictionary, "dictionary");
24
25       _dictionary = dictionary;
26     }
27
28     public DictionaryWrapper(IDictionary<TKey, TValue> dictionary)
29     {
30       ValidationUtils.ArgumentNotNull(dictionary, "dictionary");
31
32       _genericDictionary = dictionary;
33     }
34
35     public void Add(TKey key, TValue value)
36     {
37       if (_genericDictionary != null)
38         _genericDictionary.Add(key, value);
39       else
40         _dictionary.Add(key, value);
41     }
42
43     public bool ContainsKey(TKey key)
44     {
45       if (_genericDictionary != null)
46         return _genericDictionary.ContainsKey(key);
47       else
48         return _dictionary.Contains(key);
49     }
50
51     public ICollection<TKey> Keys
52     {
53       get
54       {
55         if (_genericDictionary != null)
56           return _genericDictionary.Keys;
57         else
58           return _dictionary.Keys.Cast<TKey>().ToList();
59       }
60     }
61
62     public bool Remove(TKey key)
63     {
64       if (_genericDictionary != null)
65       {
66         return _genericDictionary.Remove(key);
67       }
68       else
69       {
70         if (_dictionary.Contains(key))
71         {
72           _dictionary.Remove(key);
73           return true;
74         }
75         else
76         {
77           return false;
78         }
79       }
80     }
81
82     public bool TryGetValue(TKey key, out TValue value)
83     {
84       if (_genericDictionary != null)
85       {
86         return _genericDictionary.TryGetValue(key, out value);
87       }
88       else
89       {
90         if (!_dictionary.Contains(key))
91         {
92           value = default(TValue);
93           return false;
94         }
95         else
96         {
97           value = (TValue)_dictionary[key];
98           return true;
99         }
100       }
101     }
102
103     public ICollection<TValue> Values
104     {
105       get
106       {
107         if (_genericDictionary != null)
108           return _genericDictionary.Values;
109         else
110           return _dictionary.Values.Cast<TValue>().ToList();
111       }
112     }
113
114     public TValue this[TKey key]
115     {
116       get
117       {
118         if (_genericDictionary != null)
119           return _genericDictionary[key];
120         else
121           return (TValue)_dictionary[key];
122       }
123       set
124       {
125         if (_genericDictionary != null)
126           _genericDictionary[key] = value;
127         else
128           _dictionary[key] = value;
129       }
130     }
131
132     public void Add(KeyValuePair<TKey, TValue> item)
133     {
134       if (_genericDictionary != null)
135         _genericDictionary.Add(item);
136       else
137         ((IList)_dictionary).Add(item);
138     }
139
140     public void Clear()
141     {
142       if (_genericDictionary != null)
143         _genericDictionary.Clear();
144       else
145         _dictionary.Clear();
146     }
147
148     public bool Contains(KeyValuePair<TKey, TValue> item)
149     {
150       if (_genericDictionary != null)
151         return _genericDictionary.Contains(item);
152       else
153         return ((IList)_dictionary).Contains(item);
154     }
155
156     public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
157     {
158       if (_genericDictionary != null)
159       {
160         _genericDictionary.CopyTo(array, arrayIndex);
161       }
162       else
163       {
164         foreach (DictionaryEntry item in _dictionary)
165         {
166           array[arrayIndex++] = new KeyValuePair<TKey, TValue>((TKey)item.Key, (TValue)item.Value);
167         }
168       }
169     }
170
171     public int Count
172     {
173       get
174       {
175         if (_genericDictionary != null)
176           return _genericDictionary.Count;
177         else
178           return _dictionary.Count;
179       }
180     }
181
182     public bool IsReadOnly
183     {
184       get
185       {
186         if (_genericDictionary != null)
187           return _genericDictionary.IsReadOnly;
188         else
189           return _dictionary.IsReadOnly;
190       }
191     }
192
193     public bool Remove(KeyValuePair<TKey, TValue> item)
194     {
195       if (_genericDictionary != null)
196       {
197         return _genericDictionary.Remove(item);
198       }
199       else
200       {
201         if (_dictionary.Contains(item.Key))
202         {
203           object value = _dictionary[item.Key];
204
205           if (object.Equals(value, item.Value))
206           {
207             _dictionary.Remove(item.Key);
208             return true;
209           }
210           else
211           {
212             return false;
213           }
214         }
215         else
216         {
217           return true;
218         }
219       }
220     }
221
222     public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
223     {
224       if (_genericDictionary != null)
225         return _genericDictionary.GetEnumerator();
226       else
227         return _dictionary.Cast<DictionaryEntry>().Select(de => new KeyValuePair<TKey, TValue>((TKey)de.Key, (TValue)de.Value)).GetEnumerator();
228     }
229
230     IEnumerator IEnumerable.GetEnumerator()
231     {
232       return GetEnumerator();
233     }
234
235     void IDictionary.Add(object key, object value)
236     {
237       if (_genericDictionary != null)
238         _genericDictionary.Add((TKey)key, (TValue)value);
239       else
240         _dictionary.Add(key, value);
241     }
242
243     bool IDictionary.Contains(object key)
244     {
245       if (_genericDictionary != null)
246         return _genericDictionary.ContainsKey((TKey)key);
247       else
248         return _dictionary.Contains(key);
249     }
250
251     private struct DictionaryEnumerator<TEnumeratorKey, TEnumeratorValue> : IDictionaryEnumerator
252     {
253       private readonly IEnumerator<KeyValuePair<TEnumeratorKey, TEnumeratorValue>> _e;
254
255       public DictionaryEnumerator(IEnumerator<KeyValuePair<TEnumeratorKey, TEnumeratorValue>> e)
256       {
257         ValidationUtils.ArgumentNotNull(e, "e");
258         _e = e;
259       }
260
261       public DictionaryEntry Entry
262       {
263         get { return (DictionaryEntry)Current; }
264       }
265
266       public object Key
267       {
268         get { return Entry.Key; }
269       }
270
271       public object Value
272       {
273         get { return Entry.Value; }
274       }
275
276       public object Current
277       {
278         get { return new DictionaryEntry(_e.Current.Key, _e.Current.Value); }
279       }
280
281       public bool MoveNext()
282       {
283         return _e.MoveNext();
284       }
285
286       public void Reset()
287       {
288         _e.Reset();
289       }
290     }
291
292     IDictionaryEnumerator IDictionary.GetEnumerator()
293     {
294       if (_genericDictionary != null)
295         return new DictionaryEnumerator<TKey, TValue>(_genericDictionary.GetEnumerator());
296       else
297         return _dictionary.GetEnumerator();
298     }
299
300     bool IDictionary.IsFixedSize
301     {
302       get
303       {
304         if (_genericDictionary != null)
305           return false;
306         else
307           return _dictionary.IsFixedSize;
308       }
309     }
310
311     ICollection IDictionary.Keys
312     {
313       get
314       {
315         if (_genericDictionary != null)
316           return _genericDictionary.Keys.ToList();
317         else
318           return _dictionary.Keys;
319       }
320     }
321
322     public void Remove(object key)
323     {
324       if (_genericDictionary != null)
325         _genericDictionary.Remove((TKey)key);
326       else
327         _dictionary.Remove(key);
328     }
329
330     ICollection IDictionary.Values
331     {
332       get
333       {
334         if (_genericDictionary != null)
335           return _genericDictionary.Values.ToList();
336         else
337           return _dictionary.Values;
338       }
339     }
340
341     object IDictionary.this[object key]
342     {
343       get
344       {
345         if (_genericDictionary != null)
346           return _genericDictionary[(TKey)key];
347         else
348           return _dictionary[key];
349       }
350       set
351       {
352         if (_genericDictionary != null)
353           _genericDictionary[(TKey)key] = (TValue)value;
354         else
355           _dictionary[key] = value;
356       }
357     }
358
359     void ICollection.CopyTo(Array array, int index)
360     {
361       if (_genericDictionary != null)
362         _genericDictionary.CopyTo((KeyValuePair<TKey, TValue>[])array, index);
363       else
364         _dictionary.CopyTo(array, index);
365     }
366
367     bool ICollection.IsSynchronized
368     {
369       get
370       {
371         if (_genericDictionary != null)
372           return false;
373         else
374           return _dictionary.IsSynchronized;
375       }
376     }
377
378     object ICollection.SyncRoot
379     {
380       get
381       {
382         if (_syncRoot == null)
383           Interlocked.CompareExchange(ref _syncRoot, new object(), null);
384
385         return _syncRoot;
386       }
387     }
388
389     public object UnderlyingDictionary
390     {
391       get
392       {
393         if (_genericDictionary != null)
394           return _genericDictionary;
395         else
396           return _dictionary;
397       }
398     }
399   }
400 }