Json.NET
Code Coverage Statistics for Source File

Newtonsoft.Json\Utilities\ListWrapper.cs

Symbol Coverage: 7.08% (8 of 113)

Branch Coverage: 8.33% (5 of 60)

Cyclomatic Complexity Avg: 1.87 Max:5

Code Lines: 110


L V Source
1
#region License
2
// Copyright (c) 2007 James Newton-King
3
//
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
11
// conditions:
12
//
13
// The above copyright notice and this permission notice shall be
14
// included in all copies or substantial portions of the Software.
15
//
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.
24
#endregion
25

  
26
using System;
27
using System.Collections;
28
using System.Collections.Generic;
29
using System.Threading;
30
using Newtonsoft.Json.Utilities;
31
using System.Linq;
32
using System.Globalization;
33

  
34
namespace Newtonsoft.Json.Utilities
35
{
36
  internal interface IWrappedList : IList
37
  {
38
    object UnderlyingList { get; }
39
  }
40

  
41
  internal class ListWrapper<T> : IList<T>, IWrappedList
42
  {
43
    private readonly IList _list;
44
    private readonly IList<T> _genericList;
45
    private object _syncRoot;
46

  
47
 693
    public ListWrapper(IList list)
48
    {
49
 693
      ValidationUtils.ArgumentNotNull(list, "list");
50

  
51
 693
      if (list is IList<T>)
52
 0
        _genericList = (IList<T>) list;
53
      else
54
 693
        _list = list;
55
 693
    }
56

  
57
 0
    public ListWrapper(IList<T> list)
58
    {
59
 0
      ValidationUtils.ArgumentNotNull(list, "list");
60

  
61
 0
      _genericList = list;
62
 0
    }
63

  
64
    public int IndexOf(T item)
65
    {
66
 0
      if (_genericList != null)
67
 0
        return _genericList.IndexOf(item);
68
      else
69
 0
        return _list.IndexOf(item);
70
 0
    }
71

  
72
    public void Insert(int index, T item)
73
    {
74
 0
      if (_genericList != null)
75
 0
        _genericList.Insert(index, item);
76
      else
77
 0
        _list.Insert(index, item);
78
 0
    }
79

  
80
    public void RemoveAt(int index)
81
    {
82
 0
      if (_genericList != null)
83
 0
        _genericList.RemoveAt(index);
84
      else
85
 0
        _list.RemoveAt(index);
86
 0
    }
87

  
88
    public T this[int index]
89
    {
90
      get
91
      {
92
 0
        if (_genericList != null)
93
 0
          return _genericList[index];
94
        else
95
 0
          return (T)_list[index];
96
 0
      }
97
      set
98
      {
99
 0
        if (_genericList != null)
100
 0
          _genericList[index] = value;
101
        else
102
 0
          _list[index] = value;
103
 0
      }
104
    }
105

  
106
    public void Add(T item)
107
    {
108
 1737
      if (_genericList != null)
109
 0
        _genericList.Add(item);
110
      else
111
 1737
        _list.Add(item);
112
 1737
    }
113

  
114
    public void Clear()
115
    {
116
 0
      if (_genericList != null)
117
 0
        _genericList.Clear();
118
      else
119
 0
        _list.Clear();
120
 0
    }
121

  
122
    public bool Contains(T item)
123
    {
124
 0
      if (_genericList != null)
125
 0
        return _genericList.Contains(item);
126
      else
127
 0
        return _list.Contains(item);
128
 0
    }
129

  
130
    public void CopyTo(T[] array, int arrayIndex)
131
    {
132
 0
      if (_genericList != null)
133
 0
        _genericList.CopyTo(array, arrayIndex);
134
      else
135
 0
        _list.CopyTo(array, arrayIndex);
136
 0
    }
137

  
138
    public int Count
139
    {
140
      get
141
      {
142
 0
        if (_genericList != null)
143
 0
          return _genericList.Count;
144
        else
145
 0
          return _list.Count;
146
 0
      }
147
    }
148

  
149
    public bool IsReadOnly
150
    {
151
      get
152
      {
153
 0
        if (_genericList != null)
154
 0
          return _genericList.IsReadOnly;
155
        else
156
 0
          return _list.IsReadOnly;
157
 0
      }
158
    }
159

  
160
    public bool Remove(T item)
161
    {
162
 0
      if (_genericList != null)
163
      {
164
 0
        return _genericList.Remove(item);
165
      }
166
      else
167
      {
168
 0
        bool contains = _list.Contains(item);
169

  
170
 0
        if (contains)
171
 0
          _list.Remove(item);
172

  
173
 0
        return contains;
174
      }
175
 0
    }
176

  
177
    public IEnumerator<T> GetEnumerator()
178
    {
179
 0
      if (_genericList != null)
180
 0
        return _genericList.GetEnumerator();
181

  
182
 0
      return _list.Cast<T>().GetEnumerator();
183
 0
    }
184

  
185
    IEnumerator IEnumerable.GetEnumerator()
186
    {
187
 0
      if (_genericList != null)
188
 0
        return _genericList.GetEnumerator();
189
      else
190
 0
        return _list.GetEnumerator();
191
 0
    }
192

  
193
    int IList.Add(object value)
194
    {
195
 0
      VerifyValueType(value);
196
 0
      Add((T)value);
197

  
198
 0
      return (Count - 1);
199
 0
    }
200

  
201
    bool IList.Contains(object value)
202
    {
203
 0
      if (IsCompatibleObject(value))
204
 0
        return Contains((T)value);
205

  
206
 0
      return false;
207
 0
    }
208

  
209
    int IList.IndexOf(object value)
210
    {
211
 0
      if (IsCompatibleObject(value))
212
 0
        return IndexOf((T)value);
213

  
214
 0
      return -1;
215
 0
    }
216

  
217
    void IList.Insert(int index, object value)
218
    {
219
 0
      VerifyValueType(value);
220
 0
      Insert(index, (T)value);
221
 0
    }
222

  
223
    bool IList.IsFixedSize
224
    {
225
 0
      get { return false; }
226
    }
227

  
228
    void IList.Remove(object value)
229
    {
230
 0
      if (IsCompatibleObject(value))
231
 0
        Remove((T)value);
232
 0
    }
233

  
234
    object IList.this[int index]
235
    {
236
 0
      get { return this[index]; }
237
      set
238
      {
239
 0
        VerifyValueType(value);
240
 0
        this[index] = (T)value;
241
 0
      }
242
    }
243

  
244
    void ICollection.CopyTo(Array array, int arrayIndex)
245
    {
246
 0
      CopyTo((T[])array, arrayIndex);
247
 0
    }
248

  
249
    bool ICollection.IsSynchronized
250
    {
251
 0
      get { return false; }
252
    }
253

  
254
    object ICollection.SyncRoot
255
    {
256
      get
257
      {
258
 0
        if (_syncRoot == null)
259
 0
          Interlocked.CompareExchange(ref _syncRoot, new object(), null);
260

  
261
 0
        return _syncRoot;
262
 0
      }
263
    }
264

  
265
    private static void VerifyValueType(object value)
266
    {
267
 0
      if (!IsCompatibleObject(value))
268
 0
        throw new ArgumentException("The value '{0}' is not of type '{1}' and cannot be used in this generic collection.".FormatWith(CultureInfo.InvariantCulture, value, typeof(T)), "value");
269
 0
    }
270

  
271
    private static bool IsCompatibleObject(object value)
272
    {
273
 0
      if (!(value is T) && (value != null || (typeof(T).IsValueType && !ReflectionUtils.IsNullableType(typeof(T)))))
274
 0
        return false;
275

  
276
 0
      return true;
277
 0
    }
278

  
279
    public object UnderlyingList
280
    {
281
      get
282
      {
283
 0
        if (_genericList != null)
284
 0
          return _genericList;
285
        else
286
 0
          return _list;
287
 0
      }
288
    }
289
  }
290
}