using System.Collections; namespace Connected.Expressions.Collections; internal sealed class DeferredList : IDeferredList, ICollection, IEnumerable, IList, ICollection, IEnumerable, IDeferLoadable { private readonly IEnumerable _source; public DeferredList(IEnumerable source) { _source = source; } private IEnumerable Source => _source; private List Values { get; set; } public void Load() { if (!IsLoaded) Values = new List(Source); } public bool IsLoaded => Values is not null; #region IList Members public int IndexOf(T item) { Load(); return Values.IndexOf(item); } public void Insert(int index, T item) { Load(); Values.Insert(index, item); } public void RemoveAt(int index) { Load(); Values.RemoveAt(index); } public T this[int index] { get { Load(); return Values[index]; } set { Load(); Values[index] = value; } } #endregion #region ICollection Members public void Add(T item) { Load(); Values.Add(item); } public void Clear() { Load(); Values.Clear(); } public bool Contains(T item) { Load(); return Values.Contains(item); } public void CopyTo(T[] array, int arrayIndex) { Load(); Values.CopyTo(array, arrayIndex); } public int Count { get { Load(); return Values.Count; } } public bool IsReadOnly => false; public bool Remove(T item) { Load(); return Values.Remove(item); } #endregion #region IEnumerable Members public IEnumerator GetEnumerator() { Load(); return Values.GetEnumerator(); } #endregion #region IEnumerable Members IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } #endregion #region IList Members public int Add(object? value) { Load(); return ((IList)Values).Add(value); } public bool Contains(object? value) { Load(); return ((IList)Values).Contains(value); } public int IndexOf(object? value) { Load(); return ((IList)Values).IndexOf(value); } public void Insert(int index, object? value) { Load(); ((IList)Values).Insert(index, value); } public bool IsFixedSize => false; public void Remove(object value) { Load(); ((IList)Values).Remove(value); } object IList.this[int index] { get { Load(); return ((IList)Values)[index]; } set { Load(); ((IList)Values)[index] = value; } } #endregion #region ICollection Members public void CopyTo(Array array, int index) { Load(); ((IList)Values).CopyTo(array, index); } public bool IsSynchronized => false; public object? SyncRoot => default; #endregion }