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;
28
using System.Collections.Generic;
29
using System.Threading;
30
using Newtonsoft.Json.Utilities;
32
using System.Globalization;
34
namespace Newtonsoft.Json.Utilities
36
internal interface IWrappedList : IList
38
object UnderlyingList { get; }
41
internal class ListWrapper<T> : IList<T>, IWrappedList
43
private readonly IList _list;
44
private readonly IList<T> _genericList;
45
private object _syncRoot;
47
public ListWrapper(IList list)
49
ValidationUtils.ArgumentNotNull(list, "list");
52
_genericList = (IList<T>) list;
57
public ListWrapper(IList<T> list)
59
ValidationUtils.ArgumentNotNull(list, "list");
64
public int IndexOf(T item)
66
if (_genericList != null)
67
return _genericList.IndexOf(item);
69
return _list.IndexOf(item);
72
public void Insert(int index, T item)
74
if (_genericList != null)
75
_genericList.Insert(index, item);
77
_list.Insert(index, item);
80
public void RemoveAt(int index)
82
if (_genericList != null)
83
_genericList.RemoveAt(index);
85
_list.RemoveAt(index);
88
public T this[int index]
92
if (_genericList != null)
93
return _genericList[index];
95
return (T)_list[index];
99
if (_genericList != null)
100
_genericList[index] = value;
102
_list[index] = value;
106
public void Add(T item)
108
if (_genericList != null)
109
_genericList.Add(item);
116
if (_genericList != null)
117
_genericList.Clear();
122
public bool Contains(T item)
124
if (_genericList != null)
125
return _genericList.Contains(item);
127
return _list.Contains(item);
130
public void CopyTo(T[] array, int arrayIndex)
132
if (_genericList != null)
133
_genericList.CopyTo(array, arrayIndex);
135
_list.CopyTo(array, arrayIndex);
142
if (_genericList != null)
143
return _genericList.Count;
149
public bool IsReadOnly
153
if (_genericList != null)
154
return _genericList.IsReadOnly;
156
return _list.IsReadOnly;
160
public bool Remove(T item)
162
if (_genericList != null)
164
return _genericList.Remove(item);
168
bool contains = _list.Contains(item);
177
public IEnumerator<T> GetEnumerator()
179
if (_genericList != null)
180
return _genericList.GetEnumerator();
182
return _list.Cast<T>().GetEnumerator();
185
IEnumerator IEnumerable.GetEnumerator()
187
if (_genericList != null)
188
return _genericList.GetEnumerator();
190
return _list.GetEnumerator();
193
int IList.Add(object value)
195
VerifyValueType(value);
201
bool IList.Contains(object value)
203
if (IsCompatibleObject(value))
204
return Contains((T)value);
209
int IList.IndexOf(object value)
211
if (IsCompatibleObject(value))
212
return IndexOf((T)value);
217
void IList.Insert(int index, object value)
219
VerifyValueType(value);
220
Insert(index, (T)value);
223
bool IList.IsFixedSize
225
get { return false; }
228
void IList.Remove(object value)
230
if (IsCompatibleObject(value))
234
object IList.this[int index]
236
get { return this[index]; }
239
VerifyValueType(value);
240
this[index] = (T)value;
244
void ICollection.CopyTo(Array array, int arrayIndex)
246
CopyTo((T[])array, arrayIndex);
249
bool ICollection.IsSynchronized
251
get { return false; }
254
object ICollection.SyncRoot
258
if (_syncRoot == null)
259
Interlocked.CompareExchange(ref _syncRoot, new object(), null);
265
private static void VerifyValueType(object value)
267
if (!IsCompatibleObject(value))
268
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");
271
private static bool IsCompatibleObject(object value)
273
if (!(value is T) && (value != null || (typeof(T).IsValueType && !ReflectionUtils.IsNullableType(typeof(T)))))
279
public object UnderlyingList
283
if (_genericList != null)
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.
26
using System.Collections;
27
using System.Collections.Generic;
29
namespace Newtonsoft.Json.Utilities
31
internal interface IWrappedList : IList
33
object UnderlyingList { get; }
36
internal class ListWrapper<T> : CollectionWrapper<T>, IList<T>, IWrappedList
38
private readonly IList<T> _genericList;
40
public ListWrapper(IList list)
43
ValidationUtils.ArgumentNotNull(list, "list");
46
_genericList = (IList<T>) list;
49
public ListWrapper(IList<T> list)
52
ValidationUtils.ArgumentNotNull(list, "list");
57
public int IndexOf(T item)
59
if (_genericList != null)
60
return _genericList.IndexOf(item);
62
return ((IList)this).IndexOf(item);
65
public void Insert(int index, T item)
67
if (_genericList != null)
68
_genericList.Insert(index, item);
70
((IList)this).Insert(index, item);
73
public void RemoveAt(int index)
75
if (_genericList != null)
76
_genericList.RemoveAt(index);
78
((IList)this).RemoveAt(index);
81
public T this[int index]
85
if (_genericList != null)
86
return _genericList[index];
88
return (T)((IList)this)[index];
92
if (_genericList != null)
93
_genericList[index] = value;
95
((IList)this)[index] = value;
99
public override void Add(T item)
101
if (_genericList != null)
102
_genericList.Add(item);
107
public override void Clear()
109
if (_genericList != null)
110
_genericList.Clear();
115
public override bool Contains(T item)
117
if (_genericList != null)
118
return _genericList.Contains(item);
120
return base.Contains(item);
123
public override void CopyTo(T[] array, int arrayIndex)
125
if (_genericList != null)
126
_genericList.CopyTo(array, arrayIndex);
128
base.CopyTo(array, arrayIndex);
131
public override int Count
135
if (_genericList != null)
136
return _genericList.Count;
142
public override bool IsReadOnly
146
if (_genericList != null)
147
return _genericList.IsReadOnly;
149
return base.IsReadOnly;
153
public override bool Remove(T item)
155
if (_genericList != null)
157
return _genericList.Remove(item);
161
bool contains = base.Contains(item);
170
public override IEnumerator<T> GetEnumerator()
172
if (_genericList != null)
173
return _genericList.GetEnumerator();
175
return base.GetEnumerator();
178
public object UnderlyingList
182
if (_genericList != null)
185
return UnderlyingCollection;
b'\\ No newline at end of file'