2024-10-22 09:28:58 +08:00

125 lines
4.5 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using JetBrains.Annotations;
namespace JiShe.CollectBus.Common.Extensions
{
/// <summary>
/// Extension methods for Collections.
/// </summary>
public static class CollectionExtensions
{
/// <summary>
/// Checks whatever given collection object is null or has no item.
/// </summary>
[Description("检查给定的集合对象是否为null或没有项目")]
public static bool IsNullOrEmpty<T>([CanBeNull] this ICollection<T> source)
{
return source == null || source.Count <= 0;
}
/// <summary>
/// Adds an item to the collection if it's not already in the collection.
/// </summary>
/// <param name="source">Collection</param>
/// <param name="item">Item to check and add</param>
/// <typeparam name="T">Type of the items in the collection</typeparam>
/// <returns>Returns True if added, returns False if not.</returns>
[Description("项不在集合中则将其添加到集合中")]
public static bool AddIfNotContains<T>([NotNull] this ICollection<T> source, T item)
{
if (source == null)
{
throw new ArgumentNullException("source");
}
if (source.Contains(item))
{
return false;
}
source.Add(item);
return true;
}
/// <summary>
/// Adds items to the collection which are not already in the collection.
/// </summary>
/// <param name="source">The collection</param>
/// <param name="items">Item to check and add</param>
/// <typeparam name="T">Type of the items in the collection</typeparam>
/// <returns>Returns the added items.</returns>
public static IEnumerable<T> AddIfNotContains<T>([NotNull] this ICollection<T> source, IEnumerable<T> items)
{
var addedItems = new List<T>();
foreach (var item in items)
{
if (source.Contains(item))
{
continue;
}
source.Add(item);
addedItems.Add(item);
}
return addedItems;
}
/// <summary>
/// Adds an item to the collection if it's not already in the collection based on the given <paramref name="predicate"/>.
/// </summary>
/// <param name="source">The collection</param>
/// <param name="predicate">The condition to decide if the item is already in the collection</param>
/// <param name="itemFactory">A factory that returns the item</param>
/// <typeparam name="T">Type of the items in the collection</typeparam>
/// <returns>Returns True if added, returns False if not.</returns>
public static bool AddIfNotContains<T>([NotNull] this ICollection<T> source, [NotNull] Func<T, bool> predicate, [NotNull] Func<T> itemFactory)
{
if (source.Any(predicate))
{
return false;
}
source.Add(itemFactory());
return true;
}
/// <summary>
/// Removes all items from the collection those satisfy the given <paramref name="predicate"/>.
/// </summary>
/// <typeparam name="T">Type of the items in the collection</typeparam>
/// <param name="source">The collection</param>
/// <param name="predicate">The condition to remove the items</param>
/// <returns>List of removed items</returns>
[Description("从集合中移除满足给定条件的所有项")]
public static IList<T> RemoveAll<T>([NotNull] this ICollection<T> source, Func<T, bool> predicate)
{
var items = source.Where(predicate).ToList();
foreach (var item in items)
{
source.Remove(item);
}
return items;
}
/// <summary>
/// Removes all items from the collection those satisfy the given <paramref name="predicate"/>.
/// </summary>
/// <typeparam name="T">Type of the items in the collection</typeparam>
/// <param name="source">The collection</param>
/// <param name="items">Items to be removed from the list</param>
public static void RemoveAll<T>([NotNull] this ICollection<T> source, IEnumerable<T> items)
{
foreach (var item in items)
{
source.Remove(item);
}
}
}
}