2 using System.Collections.Generic;
5 using System.Collections;
6 using System.Threading;
8 namespace Newtonsoft.Json.Utilities
10 internal interface IWrappedDictionary : IDictionary
12 object UnderlyingDictionary { get; }
15 internal class DictionaryWrapper<TKey, TValue> : IDictionary<TKey, TValue>, IWrappedDictionary
17 private readonly IDictionary _dictionary;
18 private readonly IDictionary<TKey, TValue> _genericDictionary;
19 private object _syncRoot;
21 public DictionaryWrapper(IDictionary dictionary)
23 ValidationUtils.ArgumentNotNull(dictionary, "dictionary");
25 _dictionary = dictionary;
28 public DictionaryWrapper(IDictionary<TKey, TValue> dictionary)
30 ValidationUtils.ArgumentNotNull(dictionary, "dictionary");
32 _genericDictionary = dictionary;
35 public void Add(TKey key, TValue value)
37 if (_genericDictionary != null)
38 _genericDictionary.Add(key, value);
40 _dictionary.Add(key, value);
43 public bool ContainsKey(TKey key)
45 if (_genericDictionary != null)
46 return _genericDictionary.ContainsKey(key);
48 return _dictionary.Contains(key);
51 public ICollection<TKey> Keys
55 if (_genericDictionary != null)
56 return _genericDictionary.Keys;
58 return _dictionary.Keys.Cast<TKey>().ToList();
62 public bool Remove(TKey key)
64 if (_genericDictionary != null)
66 return _genericDictionary.Remove(key);
70 if (_dictionary.Contains(key))
72 _dictionary.Remove(key);
82 public bool TryGetValue(TKey key, out TValue value)
84 if (_genericDictionary != null)
86 return _genericDictionary.TryGetValue(key, out value);
90 if (!_dictionary.Contains(key))
92 value = default(TValue);
97 value = (TValue)_dictionary[key];
103 public ICollection<TValue> Values
107 if (_genericDictionary != null)
108 return _genericDictionary.Values;
110 return _dictionary.Values.Cast<TValue>().ToList();
114 public TValue this[TKey key]
118 if (_genericDictionary != null)
119 return _genericDictionary[key];
121 return (TValue)_dictionary[key];
125 if (_genericDictionary != null)
126 _genericDictionary[key] = value;
128 _dictionary[key] = value;
132 public void Add(KeyValuePair<TKey, TValue> item)
134 if (_genericDictionary != null)
135 _genericDictionary.Add(item);
137 ((IList)_dictionary).Add(item);
142 if (_genericDictionary != null)
143 _genericDictionary.Clear();
148 public bool Contains(KeyValuePair<TKey, TValue> item)
150 if (_genericDictionary != null)
151 return _genericDictionary.Contains(item);
153 return ((IList)_dictionary).Contains(item);
156 public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
158 if (_genericDictionary != null)
160 _genericDictionary.CopyTo(array, arrayIndex);
164 foreach (DictionaryEntry item in _dictionary)
166 array[arrayIndex++] = new KeyValuePair<TKey, TValue>((TKey)item.Key, (TValue)item.Value);
175 if (_genericDictionary != null)
176 return _genericDictionary.Count;
178 return _dictionary.Count;
182 public bool IsReadOnly
186 if (_genericDictionary != null)
187 return _genericDictionary.IsReadOnly;
189 return _dictionary.IsReadOnly;
193 public bool Remove(KeyValuePair<TKey, TValue> item)
195 if (_genericDictionary != null)
197 return _genericDictionary.Remove(item);
201 if (_dictionary.Contains(item.Key))
203 object value = _dictionary[item.Key];
205 if (object.Equals(value, item.Value))
207 _dictionary.Remove(item.Key);
222 public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
224 if (_genericDictionary != null)
225 return _genericDictionary.GetEnumerator();
227 return _dictionary.Cast<DictionaryEntry>().Select(de => new KeyValuePair<TKey, TValue>((TKey)de.Key, (TValue)de.Value)).GetEnumerator();
230 IEnumerator IEnumerable.GetEnumerator()
232 return GetEnumerator();
235 void IDictionary.Add(object key, object value)
237 if (_genericDictionary != null)
238 _genericDictionary.Add((TKey)key, (TValue)value);
240 _dictionary.Add(key, value);
243 bool IDictionary.Contains(object key)
245 if (_genericDictionary != null)
246 return _genericDictionary.ContainsKey((TKey)key);
248 return _dictionary.Contains(key);
251 private struct DictionaryEnumerator<TEnumeratorKey, TEnumeratorValue> : IDictionaryEnumerator
253 private readonly IEnumerator<KeyValuePair<TEnumeratorKey, TEnumeratorValue>> _e;
255 public DictionaryEnumerator(IEnumerator<KeyValuePair<TEnumeratorKey, TEnumeratorValue>> e)
257 ValidationUtils.ArgumentNotNull(e, "e");
261 public DictionaryEntry Entry
263 get { return (DictionaryEntry)Current; }
268 get { return Entry.Key; }
273 get { return Entry.Value; }
276 public object Current
278 get { return new DictionaryEntry(_e.Current.Key, _e.Current.Value); }
281 public bool MoveNext()
283 return _e.MoveNext();
292 IDictionaryEnumerator IDictionary.GetEnumerator()
294 if (_genericDictionary != null)
295 return new DictionaryEnumerator<TKey, TValue>(_genericDictionary.GetEnumerator());
297 return _dictionary.GetEnumerator();
300 bool IDictionary.IsFixedSize
304 if (_genericDictionary != null)
307 return _dictionary.IsFixedSize;
311 ICollection IDictionary.Keys
315 if (_genericDictionary != null)
316 return _genericDictionary.Keys.ToList();
318 return _dictionary.Keys;
322 public void Remove(object key)
324 if (_genericDictionary != null)
325 _genericDictionary.Remove((TKey)key);
327 _dictionary.Remove(key);
330 ICollection IDictionary.Values
334 if (_genericDictionary != null)
335 return _genericDictionary.Values.ToList();
337 return _dictionary.Values;
341 object IDictionary.this[object key]
345 if (_genericDictionary != null)
346 return _genericDictionary[(TKey)key];
348 return _dictionary[key];
352 if (_genericDictionary != null)
353 _genericDictionary[(TKey)key] = (TValue)value;
355 _dictionary[key] = value;
359 void ICollection.CopyTo(Array array, int index)
361 if (_genericDictionary != null)
362 _genericDictionary.CopyTo((KeyValuePair<TKey, TValue>[])array, index);
364 _dictionary.CopyTo(array, index);
367 bool ICollection.IsSynchronized
371 if (_genericDictionary != null)
374 return _dictionary.IsSynchronized;
378 object ICollection.SyncRoot
382 if (_syncRoot == null)
383 Interlocked.CompareExchange(ref _syncRoot, new object(), null);
389 public object UnderlyingDictionary
393 if (_genericDictionary != null)
394 return _genericDictionary;