2016-03-22 23:02:25 +03:00
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Collections.ObjectModel;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
|
|
namespace Xamarin.Forms
|
|
|
|
|
{
|
|
|
|
|
internal class SynchronizedList<T> : IList<T>, IReadOnlyList<T>
|
|
|
|
|
{
|
|
|
|
|
readonly List<T> _list = new List<T>();
|
|
|
|
|
ReadOnlyCollection<T> _snapshot;
|
|
|
|
|
|
|
|
|
|
public void Add(T item)
|
|
|
|
|
{
|
2016-03-29 22:41:41 +03:00
|
|
|
|
lock (_list)
|
2016-03-22 23:02:25 +03:00
|
|
|
|
{
|
|
|
|
|
_list.Add(item);
|
|
|
|
|
_snapshot = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Clear()
|
|
|
|
|
{
|
2016-03-29 22:41:41 +03:00
|
|
|
|
lock (_list)
|
2016-03-22 23:02:25 +03:00
|
|
|
|
{
|
|
|
|
|
_list.Clear();
|
|
|
|
|
_snapshot = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Contains(T item)
|
|
|
|
|
{
|
2016-03-29 22:41:41 +03:00
|
|
|
|
lock (_list)
|
2016-03-22 23:02:25 +03:00
|
|
|
|
return _list.Contains(item);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void CopyTo(T[] array, int arrayIndex)
|
|
|
|
|
{
|
2016-03-29 22:41:41 +03:00
|
|
|
|
lock (_list)
|
2016-03-22 23:02:25 +03:00
|
|
|
|
_list.CopyTo(array, arrayIndex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int Count
|
|
|
|
|
{
|
|
|
|
|
get { return _list.Count; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ICollection<T>.IsReadOnly
|
|
|
|
|
{
|
|
|
|
|
get { return false; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Remove(T item)
|
|
|
|
|
{
|
2016-03-29 22:41:41 +03:00
|
|
|
|
lock (_list)
|
2016-03-22 23:02:25 +03:00
|
|
|
|
{
|
|
|
|
|
if (_list.Remove(item))
|
|
|
|
|
{
|
|
|
|
|
_snapshot = null;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IEnumerator IEnumerable.GetEnumerator()
|
|
|
|
|
{
|
|
|
|
|
return GetEnumerator();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IEnumerator<T> GetEnumerator()
|
|
|
|
|
{
|
|
|
|
|
ReadOnlyCollection<T> snap = _snapshot;
|
|
|
|
|
if (snap == null)
|
|
|
|
|
{
|
2016-03-29 22:41:41 +03:00
|
|
|
|
lock (_list)
|
2016-03-22 23:02:25 +03:00
|
|
|
|
_snapshot = snap = new ReadOnlyCollection<T>(_list.ToList());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return snap.GetEnumerator();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int IndexOf(T item)
|
|
|
|
|
{
|
2016-03-29 22:41:41 +03:00
|
|
|
|
lock (_list)
|
2016-03-22 23:02:25 +03:00
|
|
|
|
return _list.IndexOf(item);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Insert(int index, T item)
|
|
|
|
|
{
|
2016-03-29 22:41:41 +03:00
|
|
|
|
lock (_list)
|
2016-03-22 23:02:25 +03:00
|
|
|
|
{
|
|
|
|
|
_list.Insert(index, item);
|
|
|
|
|
_snapshot = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public T this[int index]
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
ReadOnlyCollection<T> snap = _snapshot;
|
|
|
|
|
if (snap != null)
|
|
|
|
|
return snap[index];
|
|
|
|
|
|
2016-03-29 22:41:41 +03:00
|
|
|
|
lock (_list)
|
2016-03-22 23:02:25 +03:00
|
|
|
|
return _list[index];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
set
|
|
|
|
|
{
|
2016-03-29 22:41:41 +03:00
|
|
|
|
lock (_list)
|
2016-03-22 23:02:25 +03:00
|
|
|
|
{
|
|
|
|
|
_list[index] = value;
|
|
|
|
|
_snapshot = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void RemoveAt(int index)
|
|
|
|
|
{
|
2016-03-29 22:41:41 +03:00
|
|
|
|
lock (_list)
|
2016-03-22 23:02:25 +03:00
|
|
|
|
{
|
|
|
|
|
_list.RemoveAt(index);
|
|
|
|
|
_snapshot = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|