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