// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。 // // 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。 // // 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任! using System.Diagnostics.CodeAnalysis; namespace Admin.NET.Core; /// /// 集合扩展方法 /// public static class CollectionExtensions { /// /// 检查给定的集合对象是否为空或者没有任何项 /// /// 集合元素类型 /// 要检查的集合 /// 如果集合为null或空则返回true,否则返回false public static bool IsNullOrEmpty(this ICollection? source) { return source is not { Count: > 0 }; } /// /// 如果条件成立,添加项 /// /// 集合元素类型 /// 要操作的集合 /// 要添加的值 /// 条件标志,为true时添加项 public static void AddIf(this ICollection source, T value, bool flag) { _ = NotNull(source, nameof(source)); if (flag) { source.Add(value); } } /// /// 如果条件成立,添加项 /// /// 集合元素类型 /// 要操作的集合 /// 要添加的值 /// 条件函数,返回true时添加项 public static void AddIf(this ICollection source, T value, Func func) { _ = NotNull(source, nameof(source)); if (func()) { source.Add(value); } } /// /// 如果给定的集合对象不为空,则添加一个项 /// /// 集合元素类型 /// 要操作的集合 /// 要添加的值(如果不为null) public static void AddIfNotNull(this ICollection source, T value) { _ = NotNull(source, nameof(source)); if (value is not null) { source.Add(value); } } /// /// 如果集合中尚未包含该项,则将其添加到集合中 /// /// 集合中项的类型 /// 集合对象 /// 要检查并添加的项 /// 如果添加了项,则返回真(True);如果没有添加(即项已存在)则返回假(False) public static bool AddIfNotContains(this ICollection source, T item) { _ = NotNull(source, nameof(source)); if (source.Contains(item)) { return false; } source.Add(item); return true; } /// /// 向集合中添加尚未包含的项 /// /// 集合中项的类型 /// 集合对象 /// 要检查并添加的项的集合 /// 返回添加的项的集合 public static IEnumerable AddIfNotContains(this ICollection source, IEnumerable items) { _ = NotNull(source, nameof(source)); var enumerable = items as T[] ?? [.. items]; _ = NotNull(enumerable, nameof(items)); List addedItems = []; foreach (var item in enumerable) { if (source.Contains(item)) { continue; } source.Add(item); addedItems.Add(item); } return addedItems; } /// /// 如果集合中尚未包含满足给定谓词条件的项,则将项添加到集合中 /// /// 集合中项的类型 /// 集合对象 /// 决定项是否已存在于集合中的条件 /// 返回项的工厂函数 /// 如果添加了项,则返回真(True);如果没有添加(即项已存在)则返回假(False) public static bool AddIfNotContains(this ICollection source, Func predicate, Func itemFactory) { _ = NotNull(source, nameof(source)); if (source.Any(predicate)) { return false; } source.Add(itemFactory()); return true; } /// /// 移除集合中所有满足给定谓词条件的项 /// /// 集合中项的类型 /// 集合对象 /// 用于移除项的条件 /// 被移除项的列表 public static IList RemoveAllWhere(this ICollection source, Func predicate) { _ = NotNull(source, nameof(source)); var items = source.Where(predicate).ToList(); foreach (var item in items) { _ = source.Remove(item); } return items; } /// /// 从集合中移除所有指定的项 /// /// 集合中项的类型 /// 集合对象 /// 要移除的项的集合 public static void RemoveAll(this ICollection source, IEnumerable items) { _ = NotNull(source, nameof(source)); var enumerable = items as T[] ?? [.. items]; _ = NotNull(enumerable, nameof(items)); foreach (var item in enumerable) { _ = source.Remove(item); } } /// /// 数据不为空判断 /// /// /// /// /// /// public static T NotNull([NotNull] T? value, string parameterName) { return value is null ? throw new ArgumentNullException(parameterName) : value; } }