50 lines
1.2 KiB
C#
50 lines
1.2 KiB
C#
#nullable enable
|
|
namespace Spark.Core.Collections {
|
|
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
|
|
public static class NonNullCollectionWrapper {
|
|
public static NonNullCollectionWrapper<T> Create<T>(ICollection<T?> src) =>
|
|
new(src);
|
|
}
|
|
|
|
public class NonNullCollectionWrapper<T> : ICollection<T> {
|
|
readonly ICollection<T?> src;
|
|
|
|
public NonNullCollectionWrapper(ICollection<T?> src) =>
|
|
this.src = src;
|
|
|
|
public int Count => src.Count;
|
|
|
|
public bool IsReadOnly => src.IsReadOnly;
|
|
|
|
public void Add(T item) =>
|
|
src.Add(item);
|
|
|
|
public void Clear() =>
|
|
src.Clear();
|
|
|
|
public bool Contains(T item) =>
|
|
src.Contains(item);
|
|
|
|
public void CopyTo(T[] array, int arrayIndex) =>
|
|
src.CopyTo(array, arrayIndex);
|
|
|
|
public IEnumerator<T> GetEnumerator() {
|
|
foreach (var v in src) {
|
|
if (v is null) {
|
|
throw new InvalidOperationException();
|
|
}
|
|
yield return v;
|
|
}
|
|
}
|
|
|
|
public bool Remove(T item) =>
|
|
src.Remove(item);
|
|
|
|
IEnumerator IEnumerable.GetEnumerator() =>
|
|
GetEnumerator();
|
|
}
|
|
}
|