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;
2
// Copyright (c) 2007 James Newton-King
4
// Permission is hereby granted, free of charge, to any person
5
// obtaining a copy of this software and associated documentation
6
// files (the "Software"), to deal in the Software without
7
// restriction, including without limitation the rights to use,
8
// copy, modify, merge, publish, distribute, sublicense, and/or sell
9
// copies of the Software, and to permit persons to whom the
10
// Software is furnished to do so, subject to the following
13
// The above copyright notice and this permission notice shall be
14
// included in all copies or substantial portions of the Software.
16
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23
// OTHER DEALINGS IN THE SOFTWARE.
27
using System.Collections.Generic;
28
using System.Collections;
29
using System.Threading;
31
using Newtonsoft.Json.Utilities.LinqBridge;
36
namespace Newtonsoft.Json.Utilities
38
internal interface IWrappedDictionary
41
object UnderlyingDictionary { get; }
44
internal class DictionaryWrapper<TKey, TValue> : IDictionary<TKey, TValue>, IWrappedDictionary
46
private readonly IDictionary _dictionary;
47
private readonly IDictionary<TKey, TValue> _genericDictionary;
48
private object _syncRoot;
50
public DictionaryWrapper(IDictionary dictionary)
52
ValidationUtils.ArgumentNotNull(dictionary, "dictionary");
54
_dictionary = dictionary;
57
public DictionaryWrapper(IDictionary<TKey, TValue> dictionary)
59
ValidationUtils.ArgumentNotNull(dictionary, "dictionary");
61
_genericDictionary = dictionary;
64
public void Add(TKey key, TValue value)
66
if (_dictionary != null)
67
_dictionary.Add(key, value);
69
_genericDictionary.Add(key, value);
72
public bool ContainsKey(TKey key)
74
if (_dictionary != null)
75
return _dictionary.Contains(key);
77
return _genericDictionary.ContainsKey(key);
80
public ICollection<TKey> Keys
84
if (_dictionary != null)
85
return _dictionary.Keys.Cast<TKey>().ToList();
87
return _genericDictionary.Keys;
91
public bool Remove(TKey key)
93
if (_dictionary != null)
95
if (_dictionary.Contains(key))
97
_dictionary.Remove(key);
106
return _genericDictionary.Remove(key);
109
public bool TryGetValue(TKey key, out TValue value)
111
if (_dictionary != null)
113
if (!_dictionary.Contains(key))
115
value = default(TValue);
120
value = (TValue)_dictionary[key];
125
return _genericDictionary.TryGetValue(key, out value);
128
public ICollection<TValue> Values
132
if (_dictionary != null)
133
return _dictionary.Values.Cast<TValue>().ToList();
135
return _genericDictionary.Values;
139
public TValue this[TKey key]
143
if (_dictionary != null)
144
return (TValue)_dictionary[key];
145
return _genericDictionary[key];
149
if (_dictionary != null)
150
_dictionary[key] = value;
152
_genericDictionary[key] = value;
156
public void Add(KeyValuePair<TKey, TValue> item)
158
if (_dictionary != null)
159
((IList)_dictionary).Add(item);
161
_genericDictionary.Add(item);
166
if (_dictionary != null)
169
_genericDictionary.Clear();
172
public bool Contains(KeyValuePair<TKey, TValue> item)
174
if (_dictionary != null)
175
return ((IList)_dictionary).Contains(item);
177
return _genericDictionary.Contains(item);
180
public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
182
if (_dictionary != null)
184
foreach (DictionaryEntry item in _dictionary)
186
array[arrayIndex++] = new KeyValuePair<TKey, TValue>((TKey)item.Key, (TValue)item.Value);
191
_genericDictionary.CopyTo(array, arrayIndex);
199
if (_dictionary != null)
200
return _dictionary.Count;
202
return _genericDictionary.Count;
206
public bool IsReadOnly
210
if (_dictionary != null)
211
return _dictionary.IsReadOnly;
213
return _genericDictionary.IsReadOnly;
217
public bool Remove(KeyValuePair<TKey, TValue> item)
219
if (_dictionary != null)
221
if (_dictionary.Contains(item.Key))
223
object value = _dictionary[item.Key];
225
if (object.Equals(value, item.Value))
227
_dictionary.Remove(item.Key);
242
return _genericDictionary.Remove(item);
246
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
248
if (_dictionary != null)
249
return _dictionary.Cast<DictionaryEntry>().Select(de => new KeyValuePair<TKey, TValue>((TKey)de.Key, (TValue)de.Value)).GetEnumerator();
251
return _genericDictionary.GetEnumerator();
254
IEnumerator IEnumerable.GetEnumerator()
256
return GetEnumerator();
259
void IDictionary.Add(object key, object value)
261
if (_dictionary != null)
262
_dictionary.Add(key, value);
264
_genericDictionary.Add((TKey)key, (TValue)value);
267
object IDictionary.this[object key]
271
if (_dictionary != null)
272
return _dictionary[key];
274
return _genericDictionary[(TKey)key];
278
if (_dictionary != null)
279
_dictionary[key] = value;
281
_genericDictionary[(TKey)key] = (TValue)value;
285
private struct DictionaryEnumerator<TEnumeratorKey, TEnumeratorValue> : IDictionaryEnumerator
287
private readonly IEnumerator<KeyValuePair<TEnumeratorKey, TEnumeratorValue>> _e;
289
public DictionaryEnumerator(IEnumerator<KeyValuePair<TEnumeratorKey, TEnumeratorValue>> e)
291
ValidationUtils.ArgumentNotNull(e, "e");
295
public DictionaryEntry Entry
297
get { return (DictionaryEntry)Current; }
302
get { return Entry.Key; }
307
get { return Entry.Value; }
310
public object Current
312
get { return new DictionaryEntry(_e.Current.Key, _e.Current.Value); }
315
public bool MoveNext()
317
return _e.MoveNext();
326
IDictionaryEnumerator IDictionary.GetEnumerator()
328
if (_dictionary != null)
329
return _dictionary.GetEnumerator();
331
return new DictionaryEnumerator<TKey, TValue>(_genericDictionary.GetEnumerator());
334
bool IDictionary.Contains(object key)
336
if (_genericDictionary != null)
337
return _genericDictionary.ContainsKey((TKey)key);
339
return _dictionary.Contains(key);
342
bool IDictionary.IsFixedSize
346
if (_genericDictionary != null)
349
return _dictionary.IsFixedSize;
353
ICollection IDictionary.Keys
357
if (_genericDictionary != null)
358
return _genericDictionary.Keys.ToList();
360
return _dictionary.Keys;
364
public void Remove(object key)
366
if (_dictionary != null)
367
_dictionary.Remove(key);
369
_genericDictionary.Remove((TKey)key);
372
ICollection IDictionary.Values
376
if (_genericDictionary != null)
377
return _genericDictionary.Values.ToList();
379
return _dictionary.Values;
383
void ICollection.CopyTo(Array array, int index)
385
if (_dictionary != null)
386
_dictionary.CopyTo(array, index);
388
_genericDictionary.CopyTo((KeyValuePair<TKey, TValue>[])array, index);
391
bool ICollection.IsSynchronized
395
if (_dictionary != null)
396
return _dictionary.IsSynchronized;
402
object ICollection.SyncRoot
406
if (_syncRoot == null)
407
Interlocked.CompareExchange(ref _syncRoot, new object(), null);
413
public object UnderlyingDictionary
417
if (_dictionary != null)
420
return _genericDictionary;
b'\\ No newline at end of file'