Files
adas-core/adas-core.Domain/Utils/CollectionsUtils.cs
T

102 lines
4.7 KiB
C#

using System.Collections;
namespace adas_core.Domain.Utils;
/// <summary>
/// Provides static utility methods for working with collections.
/// </summary>
/// <!-- aidoc:v1 sig=50818c2 -->
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>
/// <!-- aidoc:v1 sig=dfb5373 body=1cd2a38 -->
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>
/// <!-- aidoc:v1 sig=5038079 body=1cd2a38 -->
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>
/// <!-- aidoc:v1 sig=2c3b8ff body=0269891 -->
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>
/// <!-- aidoc:v1 sig=61945f3 body=2b7a733 -->
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>
/// <!-- aidoc:v1 sig=4b33799 body=5556777 -->
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>
/// <!-- aidoc:v1 sig=a05f7fc body=74812a2 -->
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>
/// <!-- aidoc:v1 sig=98732cd body=d08ef3a -->
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>
/// <!-- aidoc:v1 sig=c865748 body=df53ea7 -->
public static List<string> IfEmptyOrNull(List<string> list, List<string> defaultList)
{
return IsEmptyOrNull(list) ? defaultList : list;
}
}