using System.Collections;
namespace XFEExtension.NetCore.AutoConfig;
///
/// 支持线程安全访问和合并自动保存的配置文件列表
///
/// 列表泛型
public class ProfileList : ICollection, IEnumerable, IEnumerable, IList, IReadOnlyCollection, IReadOnlyList, ICollection, IList, IProfileOwnedCollection
{
private readonly List _innerList;
private readonly object _syncRoot = new();
private XFEProfile? _currentProfile;
///
/// 创建空列表
///
public ProfileList() => _innerList = [];
///
/// 使用已有列表创建
///
/// 已有列表
public ProfileList(List innerList) => _innerList = innerList;
///
public TValue this[int index]
{
get
{
lock (_syncRoot)
return _innerList[index];
}
set
{
lock (_syncRoot)
{
_innerList[index] = value;
RequestSave();
}
}
}
object? IList.this[int index]
{
get
{
lock (_syncRoot)
return ((IList)_innerList)[index];
}
set
{
lock (_syncRoot)
{
((IList)_innerList)[index] = value;
RequestSave();
}
}
}
///
/// 当前配置文件实例
///
public XFEProfile? CurrentProfile
{
get
{
lock (_syncRoot)
return _currentProfile;
}
set
{
lock (_syncRoot)
_currentProfile = value;
}
}
///
public int Count
{
get
{
lock (_syncRoot)
return _innerList.Count;
}
}
///
public bool IsReadOnly => ((ICollection)_innerList).IsReadOnly;
///
public bool IsSynchronized => true;
///
public object SyncRoot => _syncRoot;
///
public bool IsFixedSize => ((IList)_innerList).IsFixedSize;
///
public void Add(TValue item)
{
lock (_syncRoot)
{
_innerList.Add(item);
RequestSave();
}
}
///
public int Add(object? value)
{
lock (_syncRoot)
{
var result = ((IList)_innerList).Add(value);
RequestSave();
return result;
}
}
///
public void AddRange(IEnumerable collection)
{
var items = collection.ToArray();
if (items.Length == 0)
return;
lock (_syncRoot)
{
_innerList.AddRange(items);
RequestSave();
}
}
///
public void AddRange(ReadOnlySpan source)
{
if (source.IsEmpty)
return;
lock (_syncRoot)
{
_innerList.AddRange(source);
RequestSave();
}
}
///
public void Clear()
{
lock (_syncRoot)
{
if (_innerList.Count == 0)
return;
_innerList.Clear();
RequestSave();
}
}
///
public bool Contains(TValue item)
{
lock (_syncRoot)
return _innerList.Contains(item);
}
///
public bool Contains(object? value)
{
lock (_syncRoot)
return ((IList)_innerList).Contains(value);
}
///
public void CopyTo(TValue[] array, int arrayIndex)
{
lock (_syncRoot)
_innerList.CopyTo(array, arrayIndex);
}
///
public void CopyTo(Array array, int index)
{
lock (_syncRoot)
((ICollection)_innerList).CopyTo(array, index);
}
///
public IEnumerator GetEnumerator()
{
lock (_syncRoot)
return ((IEnumerable)_innerList.ToArray()).GetEnumerator();
}
///
public int IndexOf(TValue item)
{
lock (_syncRoot)
return _innerList.IndexOf(item);
}
///
public int IndexOf(object? value)
{
lock (_syncRoot)
return ((IList)_innerList).IndexOf(value);
}
///
public void Insert(int index, TValue item)
{
lock (_syncRoot)
{
_innerList.Insert(index, item);
RequestSave();
}
}
///
public void Insert(int index, object? value)
{
lock (_syncRoot)
{
((IList)_innerList).Insert(index, value);
RequestSave();
}
}
///
public bool Remove(TValue item)
{
lock (_syncRoot)
{
var result = _innerList.Remove(item);
if (result)
RequestSave();
return result;
}
}
///
public void Remove(object? value)
{
lock (_syncRoot)
{
var count = _innerList.Count;
((IList)_innerList).Remove(value);
if (_innerList.Count != count)
RequestSave();
}
}
///
public void RemoveAt(int index)
{
lock (_syncRoot)
{
_innerList.RemoveAt(index);
RequestSave();
}
}
IEnumerator IEnumerable.GetEnumerator()
{
lock (_syncRoot)
return _innerList.ToArray().GetEnumerator();
}
private void RequestSave() => _currentProfile?.InstanceRequestSaveProfile();
}