Json.NET
Code Coverage Statistics for Source File

Newtonsoft.Json\Utilities\DictionaryWrapper.cs

Symbol Coverage: 17.09% (27 of 158)

Branch Coverage: 14.29% (11 of 77)

Cyclomatic Complexity Avg: 1.90 Max:4

Code Lines: 151


L V Source
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
 20444
    public DictionaryWrapper(IDictionary dictionary)
22
    {
23
 20444
      ValidationUtils.ArgumentNotNull(dictionary, "dictionary");
24

  
25
 20444
      _dictionary = dictionary;
26
 20444
    }
27

  
28
 3
    public DictionaryWrapper(IDictionary<TKey, TValue> dictionary)
29
    {
30
 3
      ValidationUtils.ArgumentNotNull(dictionary, "dictionary");
31

  
32
 3
      _genericDictionary = dictionary;
33
 3
    }
34

  
35
    public void Add(TKey key, TValue value)
36
    {
37
 0
      if (_genericDictionary != null)
38
 0
        _genericDictionary.Add(key, value);
39
      else
40
 0
        _dictionary.Add(key, value);
41
 0
    }
42

  
43
    public bool ContainsKey(TKey key)
44
    {
45
 0
      if (_genericDictionary != null)
46
 0
        return _genericDictionary.ContainsKey(key);
47
      else
48
 0
        return _dictionary.Contains(key);
49
 0
    }
50

  
51
    public ICollection<TKey> Keys
52
    {
53
      get
54
      {
55
 0
        if (_genericDictionary != null)
56
 0
          return _genericDictionary.Keys;
57
        else
58
 0
          return _dictionary.Keys.Cast<TKey>().ToList();
59
 0
      }
60
    }
61

  
62
    public bool Remove(TKey key)
63
    {
64
 0
      if (_genericDictionary != null)
65
      {
66
 0
        return _genericDictionary.Remove(key);
67
      }
68
      else
69
      {
70
 0
        if (_dictionary.Contains(key))
71
        {
72
 0
          _dictionary.Remove(key);
73
 0
          return true;
74
        }
75
        else
76
        {
77
 0
          return false;
78
        }
79
      }
80
 0
    }
81

  
82
    public bool TryGetValue(TKey key, out TValue value)
83
    {
84
 0
      if (_genericDictionary != null)
85
      {
86
 0
        return _genericDictionary.TryGetValue(key, out value);
87
      }
88
      else
89
      {
90
 0
        if (!_dictionary.Contains(key))
91
        {
92
 0
          value = default(TValue);
93
 0
          return false;
94
        }
95
        else
96
        {
97
 0
          value = (TValue)_dictionary[key];
98
 0
          return true;
99
        }
100
      }
101
 0
    }
102

  
103
    public ICollection<TValue> Values
104
    {
105
      get
106
      {
107
 0
        if (_genericDictionary != null)
108
 0
          return _genericDictionary.Values;
109
        else
110
 0
          return _dictionary.Values.Cast<TValue>().ToList();
111
 0
      }
112
    }
113

  
114
    public TValue this[TKey key]
115
    {
116
      get
117
      {
118
 0
        if (_genericDictionary != null)
119
 0
          return _genericDictionary[key];
120
        else
121
 0
          return (TValue)_dictionary[key];
122
 0
      }
123
      set
124
      {
125
 0
        if (_genericDictionary != null)
126
 0
          _genericDictionary[key] = value;
127
        else
128
 0
          _dictionary[key] = value;
129
 0
      }
130
    }
131

  
132
    public void Add(KeyValuePair<TKey, TValue> item)
133
    {
134
 0
      if (_genericDictionary != null)
135
 0
        _genericDictionary.Add(item);
136
      else
137
 0
        ((IList)_dictionary).Add(item);
138
 0
    }
139

  
140
    public void Clear()
141
    {
142
 0
      if (_genericDictionary != null)
143
 0
        _genericDictionary.Clear();
144
      else
145
 0
        _dictionary.Clear();
146
 0
    }
147

  
148
    public bool Contains(KeyValuePair<TKey, TValue> item)
149
    {
150
 0
      if (_genericDictionary != null)
151
 0
        return _genericDictionary.Contains(item);
152
      else
153
 0
        return ((IList)_dictionary).Contains(item);
154
 0
    }
155

  
156
    public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
157
    {
158
 0
      if (_genericDictionary != null)
159
      {
160
 0
        _genericDictionary.CopyTo(array, arrayIndex);
161
      }
162
      else
163
      {
164
 0
        foreach (DictionaryEntry item in _dictionary)
165
        {
166
 0
          array[arrayIndex++] = new KeyValuePair<TKey, TValue>((TKey)item.Key, (TValue)item.Value);
167
        }
168
      }
169
 0
    }
170

  
171
    public int Count
172
    {
173
      get
174
      {
175
 0
        if (_genericDictionary != null)
176
 0
          return _genericDictionary.Count;
177
        else
178
 0
          return _dictionary.Count;
179
 0
      }
180
    }
181

  
182
    public bool IsReadOnly
183
    {
184
      get
185
      {
186
 0
        if (_genericDictionary != null)
187
 0
          return _genericDictionary.IsReadOnly;
188
        else
189
 0
          return _dictionary.IsReadOnly;
190
 0
      }
191
    }
192

  
193
    public bool Remove(KeyValuePair<TKey, TValue> item)
194
    {
195
 0
      if (_genericDictionary != null)
196
      {
197
 0
        return _genericDictionary.Remove(item);
198
      }
199
      else
200
      {
201
 0
        if (_dictionary.Contains(item.Key))
202
        {
203
 0
          object value = _dictionary[item.Key];
204

  
205
 0
          if (object.Equals(value, item.Value))
206
          {
207
 0
            _dictionary.Remove(item.Key);
208
 0
            return true;
209
          }
210
          else
211
          {
212
 0
            return false;
213
          }
214
        }
215
        else
216
        {
217
 0
          return true;
218
        }
219
      }
220
 0
    }
221

  
222
    public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
223
    {
224
 0
      if (_genericDictionary != null)
225
 0
        return _genericDictionary.GetEnumerator();
226
      else
227
 0
        return _dictionary.Cast<DictionaryEntry>().Select(de => new KeyValuePair<TKey, TValue>((TKey)de.Key, (TValue)de.Value)).GetEnumerator();
228
 0
    }
229

  
230
    IEnumerator IEnumerable.GetEnumerator()
231
    {
232
 0
      return GetEnumerator();
233
 0
    }
234

  
235
    void IDictionary.Add(object key, object value)
236
    {
237
 0
      if (_genericDictionary != null)
238
 0
        _genericDictionary.Add((TKey)key, (TValue)value);
239
      else
240
 0
        _dictionary.Add(key, value);
241
 0
    }
242

  
243
    bool IDictionary.Contains(object key)
244
    {
245
 0
      if (_genericDictionary != null)
246
 0
        return _genericDictionary.ContainsKey((TKey)key);
247
      else
248
 0
        return _dictionary.Contains(key);
249
 0
    }
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
 1
        ValidationUtils.ArgumentNotNull(e, "e");
258
 1
        _e = e;
259
 1
      }
260

  
261
      public DictionaryEntry Entry
262
      {
263
 0
        get { return (DictionaryEntry)Current; }
264
      }
265

  
266
      public object Key
267
      {
268
 0
        get { return Entry.Key; }
269
      }
270

  
271
      public object Value
272
      {
273
 0
        get { return Entry.Value; }
274
      }
275

  
276
      public object Current
277
      {
278
 1
        get { return new DictionaryEntry(_e.Current.Key, _e.Current.Value); }
279
      }
280

  
281
      public bool MoveNext()
282
      {
283
 2
        return _e.MoveNext();
284
 2
      }
285

  
286
      public void Reset()
287
      {
288
 0
        _e.Reset();
289
 0
      }
290
    }
291

  
292
    IDictionaryEnumerator IDictionary.GetEnumerator()
293
    {
294
 10426
      if (_genericDictionary != null)
295
 1
        return new DictionaryEnumerator<TKey, TValue>(_genericDictionary.GetEnumerator());
296
      else
297
 10425
        return _dictionary.GetEnumerator();
298
 10426
    }
299

  
300
    bool IDictionary.IsFixedSize
301
    {
302
      get
303
      {
304
 0
        if (_genericDictionary != null)
305
 0
          return false;
306
        else
307
 0
          return _dictionary.IsFixedSize;
308
 0
      }
309
    }
310

  
311
    ICollection IDictionary.Keys
312
    {
313
      get
314
      {
315
 0
        if (_genericDictionary != null)
316
 0
          return _genericDictionary.Keys.ToList();
317
        else
318
 0
          return _dictionary.Keys;
319
 0
      }
320
    }
321

  
322
    public void Remove(object key)
323
    {
324
 0
      if (_genericDictionary != null)
325
 0
        _genericDictionary.Remove((TKey)key);
326
      else
327
 0
        _dictionary.Remove(key);
328
 0
    }
329

  
330
    ICollection IDictionary.Values
331
    {
332
      get
333
      {
334
 0
        if (_genericDictionary != null)
335
 0
          return _genericDictionary.Values.ToList();
336
        else
337
 0
          return _dictionary.Values;
338
 0
      }
339
    }
340

  
341
    object IDictionary.this[object key]
342
    {
343
      get
344
      {
345
 0
        if (_genericDictionary != null)
346
 0
          return _genericDictionary[(TKey)key];
347
        else
348
 0
          return _dictionary[key];
349
 0
      }
350
      set
351
      {
352
 30043
        if (_genericDictionary != null)
353
 2
          _genericDictionary[(TKey)key] = (TValue)value;
354
        else
355
 30041
          _dictionary[key] = value;
356
 30043
      }
357
    }
358

  
359
    void ICollection.CopyTo(Array array, int index)
360
    {
361
 0
      if (_genericDictionary != null)
362
 0
        _genericDictionary.CopyTo((KeyValuePair<TKey, TValue>[])array, index);
363
      else
364
 0
        _dictionary.CopyTo(array, index);
365
 0
    }
366

  
367
    bool ICollection.IsSynchronized
368
    {
369
      get
370
      {
371
 0
        if (_genericDictionary != null)
372
 0
          return false;
373
        else
374
 0
          return _dictionary.IsSynchronized;
375
 0
      }
376
    }
377

  
378
    object ICollection.SyncRoot
379
    {
380
      get
381
      {
382
 0
        if (_syncRoot == null)
383
 0
          Interlocked.CompareExchange(ref _syncRoot, new object(), null);
384

  
385
 0
        return _syncRoot;
386
 0
      }
387
    }
388

  
389
    public object UnderlyingDictionary
390
    {
391
      get
392
      {
393
 71356
        if (_genericDictionary != null)
394
 10
          return _genericDictionary;
395
        else
396
 71346
          return _dictionary;
397
 71356
      }
398
    }
399
  }
400
}