93 lines
4.3 KiB
C#
93 lines
4.3 KiB
C#
using System.Collections;
|
|
|
|
namespace adas_core.Domain.Utils;
|
|
|
|
/// <summary>
|
|
/// Provides static utility methods for working with collections.
|
|
/// </summary>
|
|
public static class CollectionsUtils
|
|
{
|
|
/// <summary>
|
|
/// Determines whether the specified list is null or contains no elements.
|
|
/// </summary>
|
|
/// <param name="value">The list to evaluate.</param>
|
|
/// <returns><see langword="true"/> if the list is null or empty; otherwise, <see langword="false"/>.</returns>
|
|
public static bool IsNullOrEmpty<T>(this List<T>? value)
|
|
{
|
|
return value == null || !value.Any();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Determines whether the specified list is <see langword="null"/> or contains no elements.
|
|
/// </summary>
|
|
/// <param name="value">The list to evaluate.</param>
|
|
/// <returns><see langword="true"/> if the list is <see langword="null"/> or empty; otherwise, <see langword="false"/>.</returns>
|
|
public static bool IsEmptyOrNull<T>(List<T>? value)
|
|
{
|
|
return value == null || !value.Any();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Determines whether the specified list contains at least one element, returning <c>false</c> when the list is <c>null</c> or empty.
|
|
/// </summary>
|
|
/// <param name="value">The list to evaluate. May be <c>null</c>.</param>
|
|
/// <returns><c>true</c> if <paramref name="value"/> is not <c>null</c> and contains one or more elements; otherwise, <c>false</c>.</returns>
|
|
public static bool HasElements<T>(List<T>? value)
|
|
{
|
|
return value != null && value.Any();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Determines whether the specified list is neither null nor empty by negating the result of <see cref="IsEmptyOrNull{T}(List{T})"/>.
|
|
/// </summary>
|
|
/// <param name="value">The list to evaluate.</param>
|
|
/// <returns><see langword="true"/> if the list is not null and contains at least one element; otherwise, <see langword="false"/>.</returns>
|
|
public static bool IsNotEmpty<T>(List<T> value)
|
|
{
|
|
return !IsEmptyOrNull(value);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns the provided list as-is when it is not null, or an empty list when it is null, ensuring a non-null result for safe iteration.
|
|
/// </summary>
|
|
/// <param name="value">The list to evaluate; may be <see langword="null"/>.</param>
|
|
/// <returns>The original <paramref name="value"/> if it is not null; otherwise, an empty <see cref="List{T}"/>.</returns>
|
|
public static List<T> EmptyIfNull<T>(List<T>? value)
|
|
{
|
|
return value ?? [];
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns <c>null</c> when the specified list is null or contains no elements; otherwise, returns the list as-is.
|
|
/// </summary>
|
|
/// <param name="value">The list to evaluate.</param>
|
|
/// <returns>The original <paramref name="value"/> when it is not null and not empty; otherwise, <c>null</c>.</returns>
|
|
public static List<T>? NullIfEmpty<T>(List<T> value)
|
|
{
|
|
return IsEmptyOrNull(value) ? null : value;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Converts the specified collection into a strongly typed array of the given element type.
|
|
/// </summary>
|
|
/// <param name="collection">The source collection whose elements are copied into the new array.</param>
|
|
/// <param name="type">The element <see cref="Type"/> of the array to create.</param>
|
|
/// <returns>A new <see cref="Array"/> of the specified type containing the elements copied from the collection.</returns>
|
|
public static Array ToArray(ICollection collection, Type type)
|
|
{
|
|
var result = Array.CreateInstance(type, collection.Count);
|
|
collection.CopyTo(result, 0);
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns the provided default list when the input list is null or empty; otherwise returns the input list unchanged.
|
|
/// </summary>
|
|
/// <param name="list">The list to evaluate for a null or empty state.</param>
|
|
/// <param name="defaultList">The fallback list returned when <paramref name="list"/> is null or empty.</param>
|
|
/// <returns>The original <paramref name="list"/> when it contains items; otherwise <paramref name="defaultList"/>.</returns>
|
|
public static List<string> IfEmptyOrNull(List<string> list, List<string> defaultList)
|
|
{
|
|
return IsEmptyOrNull(list) ? defaultList : list;
|
|
}
|
|
} |