using System; using System.Threading; using System.Collections; using System.Collections.Generic; namespace Cobilas.Collections.Generic { /// Provides the base class for a read-only generic Long collection. /// The type of elements in the collection. [Serializable] public class ReadOnlyLongCollection : ILongList, ILongList, IReadOnlyLongList, ICloneable { private T[] readOnlyList; [NonSerialized] private object _syncRoot; /// public long Count => ArrayManipulation.ArrayLongLength(readOnlyList); bool ILongList.IsReadOnly => true; bool ILongList.IsFixedSize => true; bool ILongCollection.IsReadOnly => true; bool ILongCollection.IsSynchronized => false; object ILongCollection.SyncRoot { get { if (_syncRoot is null) { if (readOnlyList is ICollection collection) _syncRoot = collection.SyncRoot; else Interlocked.CompareExchange(ref _syncRoot, new object(), null); } return _syncRoot; } } /// public T this[long index] => readOnlyList[index]; object ILongList.this[long index] { get => readOnlyList[index]; set => throw new NotImplementedException(); } T ILongList.this[long index] { get => readOnlyList[index]; set => throw new NotImplementedException(); } /// Creates a new instance of the object. public ReadOnlyLongCollection(IEnumerable enumerable) { foreach (T item in enumerable) ArrayManipulation.Add(item, ref readOnlyList); } /// public IEnumerator GetEnumerator() => new ArrayToIEnumerator(readOnlyList); /// public object Clone() => new ReadOnlyLongCollection(readOnlyList); IEnumerator IEnumerable.GetEnumerator() => new ArrayToIEnumerator(readOnlyList); void ILongList.Insert(long index, T item) => throw new NotImplementedException(); void ILongList.RemoveAt(long index) => throw new NotImplementedException(); void ILongCollection.Add(T item) => throw new NotImplementedException(); long ILongList.Add(object value) => throw new NotImplementedException(); bool ILongCollection.Remove(T item) => throw new NotImplementedException(); void ILongList.Insert(long index, object value) => throw new NotImplementedException(); void ILongList.Remove(object value) => throw new NotImplementedException(); void ILongList.RemoveAt(long index) => throw new NotImplementedException(); void ILongCollection.CopyTo(T[] array, long arrayIndex) => ArrayManipulation.CopyTo(readOnlyList, arrayIndex, array, arrayIndex, Count); void ILongCollection.CopyTo(Array array, long index) => ArrayManipulation.CopyTo(readOnlyList, index, array, index, Count); void ILongCollection.Clear() => ArrayManipulation.ClearArraySafe(ref readOnlyList); void ILongList.Clear() => ArrayManipulation.ClearArraySafe(ref readOnlyList); bool ILongCollection.Contains(T item) => ArrayManipulation.Exists(readOnlyList, (E) => EqualityComparer.Default.Equals(E, item)); bool ILongList.Contains(object value) => ArrayManipulation.Exists(readOnlyList, (E) => (object)E == value); long ILongList.IndexOf(T item) => ArrayManipulation.LongIndexOf(item, readOnlyList); long ILongList.IndexOf(object value) => ArrayManipulation.LongIndexOf(value, readOnlyList); } }