using System; using System.Collections; using System.Collections.Generic; using System.Linq; namespace Unity.VisualScripting { public partial class EnsureThat { public void HasItems(T value) where T : class, ICollection { if (!Ensure.IsActive) { return; } IsNotNull(value); if (value.Count < 1) { throw new ArgumentException(ExceptionMessages.Collections_HasItemsFailed, paramName); } } public void HasItems(ICollection value) { if (!Ensure.IsActive) { return; } IsNotNull(value); if (value.Count < 1) { throw new ArgumentException(ExceptionMessages.Collections_HasItemsFailed, paramName); } } public void HasItems(T[] value) { if (!Ensure.IsActive) { return; } IsNotNull(value); if (value.Length < 1) { throw new ArgumentException(ExceptionMessages.Collections_HasItemsFailed, paramName); } } public void HasNoNullItem(T value) where T : class, IEnumerable { if (!Ensure.IsActive) { return; } IsNotNull(value); foreach (var item in value) { if (item == null) { throw new ArgumentException(ExceptionMessages.Collections_HasNoNullItemFailed, paramName); } } } public void HasItems(IList value) => HasItems(value as ICollection); public void HasItems(IDictionary value) { if (!Ensure.IsActive) { return; } IsNotNull(value); if (value.Count < 1) { throw new ArgumentException(ExceptionMessages.Collections_HasItemsFailed, paramName); } } public void SizeIs(T[] value, int expected) { if (!Ensure.IsActive) { return; } if (value.Length != expected) { throw new ArgumentException(ExceptionMessages.Collections_SizeIs_Failed.Inject(expected, value.Length), paramName); } } public void SizeIs(T[] value, long expected) { if (!Ensure.IsActive) { return; } if (value.Length != expected) { throw new ArgumentException(ExceptionMessages.Collections_SizeIs_Failed.Inject(expected, value.Length), paramName); } } public void SizeIs(T value, int expected) where T : ICollection { if (!Ensure.IsActive) { return; } if (value.Count != expected) { throw new ArgumentException(ExceptionMessages.Collections_SizeIs_Failed.Inject(expected, value.Count), paramName); } } public void SizeIs(T value, long expected) where T : ICollection { if (!Ensure.IsActive) { return; } if (value.Count != expected) { throw new ArgumentException(ExceptionMessages.Collections_SizeIs_Failed.Inject(expected, value.Count), paramName); } } public void SizeIs(ICollection value, int expected) { if (!Ensure.IsActive) { return; } if (value.Count != expected) { throw new ArgumentException(ExceptionMessages.Collections_SizeIs_Failed.Inject(expected, value.Count), paramName); } } public void SizeIs(ICollection value, long expected) { if (!Ensure.IsActive) { return; } if (value.Count != expected) { throw new ArgumentException(ExceptionMessages.Collections_SizeIs_Failed.Inject(expected, value.Count), paramName); } } public void SizeIs(IList value, int expected) => SizeIs(value as ICollection, expected); public void SizeIs(IList value, long expected) => SizeIs(value as ICollection, expected); public void SizeIs(IDictionary value, int expected) { if (!Ensure.IsActive) { return; } if (value.Count != expected) { throw new ArgumentException(ExceptionMessages.Collections_SizeIs_Failed.Inject(expected, value.Count), paramName); } } public void SizeIs(IDictionary value, long expected) { if (!Ensure.IsActive) { return; } if (value.Count != expected) { throw new ArgumentException(ExceptionMessages.Collections_SizeIs_Failed.Inject(expected, value.Count), paramName); } } public void IsKeyOf(IDictionary value, TKey expectedKey, string keyLabel = null) { if (!Ensure.IsActive) { return; } if (!value.ContainsKey(expectedKey)) { throw new ArgumentException(ExceptionMessages.Collections_ContainsKey_Failed.Inject(expectedKey, keyLabel ?? paramName.Prettify()), paramName); } } public void Any(IList value, Func predicate) => Any(value as ICollection, predicate); public void Any(ICollection value, Func predicate) { if (!Ensure.IsActive) { return; } if (!value.Any(predicate)) { throw new ArgumentException(ExceptionMessages.Collections_Any_Failed, paramName); } } public void Any(T[] value, Func predicate) { if (!Ensure.IsActive) { return; } if (!value.Any(predicate)) { throw new ArgumentException(ExceptionMessages.Collections_Any_Failed, paramName); } } } }