maui-linux/Xamarin.Forms.Core/SynchronizedList.cs

130 строки
1.9 KiB
C#
Исходник Обычный вид История

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)
{
lock (_list)
2016-03-22 23:02:25 +03:00
{
_list.Add(item);
_snapshot = null;
}
}
public void Clear()
{
lock (_list)
2016-03-22 23:02:25 +03:00
{
_list.Clear();
_snapshot = null;
}
}
public bool Contains(T item)
{
lock (_list)
2016-03-22 23:02:25 +03:00
return _list.Contains(item);
}
public void CopyTo(T[] array, int arrayIndex)
{
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)
{
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)
{
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)
{
lock (_list)
2016-03-22 23:02:25 +03:00
return _list.IndexOf(item);
}
public void Insert(int index, T item)
{
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];
lock (_list)
2016-03-22 23:02:25 +03:00
return _list[index];
}
set
{
lock (_list)
2016-03-22 23:02:25 +03:00
{
_list[index] = value;
_snapshot = null;
}
}
}
public void RemoveAt(int index)
{
lock (_list)
2016-03-22 23:02:25 +03:00
{
_list.RemoveAt(index);
_snapshot = null;
}
}
}
}