~ubuntu-branches/ubuntu/trusty/smuxi/trusty-proposed

« back to all changes in this revision

Viewing changes to lib/Newtonsoft.Json/Src/Newtonsoft.Json/Utilities/ListWrapper.cs

  • Committer: Package Import Robot
  • Author(s): Mirco Bauer
  • Date: 2013-05-25 22:11:31 UTC
  • mfrom: (1.2.12)
  • Revision ID: package-import@ubuntu.com-20130525221131-nd2mc0kzubuwyx20
Tags: 0.8.11-1
* [22d13d5] Imported Upstream version 0.8.11
* [6d2b95a] Refreshed patches
* [89eb66e] Added ServiceStack libraries to smuxi-engine package
* [848ab10] Enable Campfire engine
* [c6dbdc7] Always build db4o for predictable build result
* [13ec489] Exclude OS X specific libraries from dh_clideps

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
    public ListWrapper(IList list)
48
 
    {
49
 
      ValidationUtils.ArgumentNotNull(list, "list");
50
 
 
51
 
      if (list is IList<T>)
52
 
        _genericList = (IList<T>) list;
53
 
      else
54
 
        _list = list;
55
 
    }
56
 
 
57
 
    public ListWrapper(IList<T> list)
58
 
    {
59
 
      ValidationUtils.ArgumentNotNull(list, "list");
60
 
 
61
 
      _genericList = list;
62
 
    }
63
 
 
64
 
    public int IndexOf(T item)
65
 
    {
66
 
      if (_genericList != null)
67
 
        return _genericList.IndexOf(item);
68
 
      else
69
 
        return _list.IndexOf(item);
70
 
    }
71
 
 
72
 
    public void Insert(int index, T item)
73
 
    {
74
 
      if (_genericList != null)
75
 
        _genericList.Insert(index, item);
76
 
      else
77
 
        _list.Insert(index, item);
78
 
    }
79
 
 
80
 
    public void RemoveAt(int index)
81
 
    {
82
 
      if (_genericList != null)
83
 
        _genericList.RemoveAt(index);
84
 
      else
85
 
        _list.RemoveAt(index);
86
 
    }
87
 
 
88
 
    public T this[int index]
89
 
    {
90
 
      get
91
 
      {
92
 
        if (_genericList != null)
93
 
          return _genericList[index];
94
 
        else
95
 
          return (T)_list[index];
96
 
      }
97
 
      set
98
 
      {
99
 
        if (_genericList != null)
100
 
          _genericList[index] = value;
101
 
        else
102
 
          _list[index] = value;
103
 
      }
104
 
    }
105
 
 
106
 
    public void Add(T item)
107
 
    {
108
 
      if (_genericList != null)
109
 
        _genericList.Add(item);
110
 
      else
111
 
        _list.Add(item);
112
 
    }
113
 
 
114
 
    public void Clear()
115
 
    {
116
 
      if (_genericList != null)
117
 
        _genericList.Clear();
118
 
      else
119
 
        _list.Clear();
120
 
    }
121
 
 
122
 
    public bool Contains(T item)
123
 
    {
124
 
      if (_genericList != null)
125
 
        return _genericList.Contains(item);
126
 
      else
127
 
        return _list.Contains(item);
128
 
    }
129
 
 
130
 
    public void CopyTo(T[] array, int arrayIndex)
131
 
    {
132
 
      if (_genericList != null)
133
 
        _genericList.CopyTo(array, arrayIndex);
134
 
      else
135
 
        _list.CopyTo(array, arrayIndex);
136
 
    }
137
 
 
138
 
    public int Count
139
 
    {
140
 
      get
141
 
      {
142
 
        if (_genericList != null)
143
 
          return _genericList.Count;
144
 
        else
145
 
          return _list.Count;
146
 
      }
147
 
    }
148
 
 
149
 
    public bool IsReadOnly
150
 
    {
151
 
      get
152
 
      {
153
 
        if (_genericList != null)
154
 
          return _genericList.IsReadOnly;
155
 
        else
156
 
          return _list.IsReadOnly;
157
 
      }
158
 
    }
159
 
 
160
 
    public bool Remove(T item)
161
 
    {
162
 
      if (_genericList != null)
163
 
      {
164
 
        return _genericList.Remove(item);
165
 
      }
166
 
      else
167
 
      {
168
 
        bool contains = _list.Contains(item);
169
 
 
170
 
        if (contains)
171
 
          _list.Remove(item);
172
 
 
173
 
        return contains;
174
 
      }
175
 
    }
176
 
 
177
 
    public IEnumerator<T> GetEnumerator()
178
 
    {
179
 
      if (_genericList != null)
180
 
        return _genericList.GetEnumerator();
181
 
 
182
 
      return _list.Cast<T>().GetEnumerator();
183
 
    }
184
 
 
185
 
    IEnumerator IEnumerable.GetEnumerator()
186
 
    {
187
 
      if (_genericList != null)
188
 
        return _genericList.GetEnumerator();
189
 
      else
190
 
        return _list.GetEnumerator();
191
 
    }
192
 
 
193
 
    int IList.Add(object value)
194
 
    {
195
 
      VerifyValueType(value);
196
 
      Add((T)value);
197
 
 
198
 
      return (Count - 1);
199
 
    }
200
 
 
201
 
    bool IList.Contains(object value)
202
 
    {
203
 
      if (IsCompatibleObject(value))
204
 
        return Contains((T)value);
205
 
 
206
 
      return false;
207
 
    }
208
 
 
209
 
    int IList.IndexOf(object value)
210
 
    {
211
 
      if (IsCompatibleObject(value))
212
 
        return IndexOf((T)value);
213
 
 
214
 
      return -1;
215
 
    }
216
 
 
217
 
    void IList.Insert(int index, object value)
218
 
    {
219
 
      VerifyValueType(value);
220
 
      Insert(index, (T)value);
221
 
    }
222
 
 
223
 
    bool IList.IsFixedSize
224
 
    {
225
 
      get { return false; }
226
 
    }
227
 
 
228
 
    void IList.Remove(object value)
229
 
    {
230
 
      if (IsCompatibleObject(value))
231
 
        Remove((T)value);
232
 
    }
233
 
 
234
 
    object IList.this[int index]
235
 
    {
236
 
      get { return this[index]; }
237
 
      set
238
 
      {
239
 
        VerifyValueType(value);
240
 
        this[index] = (T)value;
241
 
      }
242
 
    }
243
 
 
244
 
    void ICollection.CopyTo(Array array, int arrayIndex)
245
 
    {
246
 
      CopyTo((T[])array, arrayIndex);
247
 
    }
248
 
 
249
 
    bool ICollection.IsSynchronized
250
 
    {
251
 
      get { return false; }
252
 
    }
253
 
 
254
 
    object ICollection.SyncRoot
255
 
    {
256
 
      get
257
 
      {
258
 
        if (_syncRoot == null)
259
 
          Interlocked.CompareExchange(ref _syncRoot, new object(), null);
260
 
 
261
 
        return _syncRoot;
262
 
      }
263
 
    }
264
 
 
265
 
    private static void VerifyValueType(object value)
266
 
    {
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");
269
 
    }
270
 
 
271
 
    private static bool IsCompatibleObject(object value)
272
 
    {
273
 
      if (!(value is T) && (value != null || (typeof(T).IsValueType && !ReflectionUtils.IsNullableType(typeof(T)))))
274
 
        return false;
275
 
 
276
 
      return true;
277
 
    }
278
 
 
279
 
    public object UnderlyingList
280
 
    {
281
 
      get
282
 
      {
283
 
        if (_genericList != null)
284
 
          return _genericList;
285
 
        else
286
 
          return _list;
287
 
      }
288
 
    }
289
 
  }
 
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.Collections;
 
27
using System.Collections.Generic;
 
28
 
 
29
namespace Newtonsoft.Json.Utilities
 
30
{
 
31
  internal interface IWrappedList : IList
 
32
  {
 
33
    object UnderlyingList { get; }
 
34
  }
 
35
 
 
36
  internal class ListWrapper<T> : CollectionWrapper<T>, IList<T>, IWrappedList
 
37
  {
 
38
    private readonly IList<T> _genericList;
 
39
 
 
40
    public ListWrapper(IList list)
 
41
      : base(list)
 
42
    {
 
43
      ValidationUtils.ArgumentNotNull(list, "list");
 
44
 
 
45
      if (list is IList<T>)
 
46
        _genericList = (IList<T>) list;
 
47
    }
 
48
 
 
49
    public ListWrapper(IList<T> list)
 
50
      : base(list)
 
51
    {
 
52
      ValidationUtils.ArgumentNotNull(list, "list");
 
53
 
 
54
      _genericList = list;
 
55
    }
 
56
 
 
57
    public int IndexOf(T item)
 
58
    {
 
59
      if (_genericList != null)
 
60
        return _genericList.IndexOf(item);
 
61
      else
 
62
        return ((IList)this).IndexOf(item);
 
63
    }
 
64
 
 
65
    public void Insert(int index, T item)
 
66
    {
 
67
      if (_genericList != null)
 
68
        _genericList.Insert(index, item);
 
69
      else
 
70
        ((IList)this).Insert(index, item);
 
71
    }
 
72
 
 
73
    public void RemoveAt(int index)
 
74
    {
 
75
      if (_genericList != null)
 
76
        _genericList.RemoveAt(index);
 
77
      else
 
78
        ((IList)this).RemoveAt(index);
 
79
    }
 
80
 
 
81
    public T this[int index]
 
82
    {
 
83
      get
 
84
      {
 
85
        if (_genericList != null)
 
86
          return _genericList[index];
 
87
        else
 
88
          return (T)((IList)this)[index];
 
89
      }
 
90
      set
 
91
      {
 
92
        if (_genericList != null)
 
93
          _genericList[index] = value;
 
94
        else
 
95
          ((IList)this)[index] = value;
 
96
      }
 
97
    }
 
98
 
 
99
    public override void Add(T item)
 
100
    {
 
101
      if (_genericList != null)
 
102
        _genericList.Add(item);
 
103
      else
 
104
        base.Add(item);
 
105
    }
 
106
 
 
107
    public override void Clear()
 
108
    {
 
109
      if (_genericList != null)
 
110
        _genericList.Clear();
 
111
      else
 
112
        base.Clear();
 
113
    }
 
114
 
 
115
    public override bool Contains(T item)
 
116
    {
 
117
      if (_genericList != null)
 
118
        return _genericList.Contains(item);
 
119
      else
 
120
        return base.Contains(item);
 
121
    }
 
122
 
 
123
    public override void CopyTo(T[] array, int arrayIndex)
 
124
    {
 
125
      if (_genericList != null)
 
126
        _genericList.CopyTo(array, arrayIndex);
 
127
      else
 
128
        base.CopyTo(array, arrayIndex);
 
129
    }
 
130
 
 
131
    public override int Count
 
132
    {
 
133
      get
 
134
      {
 
135
        if (_genericList != null)
 
136
          return _genericList.Count;
 
137
        else
 
138
          return base.Count;
 
139
      }
 
140
    }
 
141
 
 
142
    public override bool IsReadOnly
 
143
    {
 
144
      get
 
145
      {
 
146
        if (_genericList != null)
 
147
          return _genericList.IsReadOnly;
 
148
        else
 
149
          return base.IsReadOnly;
 
150
      }
 
151
    }
 
152
 
 
153
    public override bool Remove(T item)
 
154
    {
 
155
      if (_genericList != null)
 
156
      {
 
157
        return _genericList.Remove(item);
 
158
      }
 
159
      else
 
160
      {
 
161
        bool contains = base.Contains(item);
 
162
 
 
163
        if (contains)
 
164
          base.Remove(item);
 
165
 
 
166
        return contains;
 
167
      }
 
168
    }
 
169
 
 
170
    public override IEnumerator<T> GetEnumerator()
 
171
    {
 
172
      if (_genericList != null)
 
173
        return _genericList.GetEnumerator();
 
174
 
 
175
      return base.GetEnumerator();
 
176
    }
 
177
 
 
178
    public object UnderlyingList
 
179
    {
 
180
      get
 
181
      {
 
182
        if (_genericList != null)
 
183
          return _genericList;
 
184
        else
 
185
          return UnderlyingCollection;
 
186
      }
 
187
    }
 
188
  }
290
189
}
 
 
b'\\ No newline at end of file'