2024-04-22 14:17:08 +01:00
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2024-04-22 14:18:41 +01:00
|
|
|
using System.Diagnostics.CodeAnalysis;
|
2024-04-22 14:17:08 +01:00
|
|
|
using System.Linq;
|
2024-04-22 14:18:41 +01:00
|
|
|
using System.Runtime.CompilerServices;
|
2024-04-22 14:17:08 +01:00
|
|
|
|
2025-10-22 09:17:13 +01:00
|
|
|
namespace SharpCompress;
|
2024-04-22 14:17:08 +01:00
|
|
|
|
2025-01-15 04:46:22 +03:00
|
|
|
internal static class NotNullExtensions
|
2024-04-22 14:17:08 +01:00
|
|
|
{
|
2024-04-23 15:08:32 +01:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2025-10-22 09:19:30 +01:00
|
|
|
public static IEnumerable<T> Empty<T>(this IEnumerable<T>? source) => source ?? [];
|
2024-04-23 15:08:50 +01:00
|
|
|
|
2024-04-23 15:08:32 +01:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2024-04-22 14:17:08 +01:00
|
|
|
public static IEnumerable<T> Empty<T>(this T? source)
|
|
|
|
|
{
|
|
|
|
|
if (source is null)
|
|
|
|
|
{
|
2025-10-22 09:17:13 +01:00
|
|
|
return [];
|
2024-04-22 14:17:08 +01:00
|
|
|
}
|
|
|
|
|
return source.AsEnumerable();
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-15 18:05:14 +00:00
|
|
|
#if LEGACY_DOTNET
|
2024-04-23 15:08:50 +01:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2024-04-22 14:17:08 +01:00
|
|
|
public static T NotNull<T>(this T? obj, string? message = null)
|
|
|
|
|
where T : class
|
|
|
|
|
{
|
|
|
|
|
if (obj is null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(message ?? "Value is null");
|
|
|
|
|
}
|
|
|
|
|
return obj;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-23 15:08:50 +01:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2024-04-22 14:17:08 +01:00
|
|
|
public static T NotNull<T>(this T? obj, string? message = null)
|
|
|
|
|
where T : struct
|
|
|
|
|
{
|
|
|
|
|
if (obj is null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(message ?? "Value is null");
|
|
|
|
|
}
|
|
|
|
|
return obj.Value;
|
|
|
|
|
}
|
|
|
|
|
#else
|
|
|
|
|
|
2024-04-23 15:08:32 +01:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2024-04-22 14:17:08 +01:00
|
|
|
public static T NotNull<T>(
|
|
|
|
|
[NotNull] this T? obj,
|
|
|
|
|
[CallerArgumentExpression(nameof(obj))] string? paramName = null
|
|
|
|
|
)
|
|
|
|
|
where T : class
|
|
|
|
|
{
|
|
|
|
|
ArgumentNullException.ThrowIfNull(obj, paramName);
|
|
|
|
|
return obj;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-23 15:08:32 +01:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2024-04-22 14:17:08 +01:00
|
|
|
public static T NotNull<T>(
|
|
|
|
|
[NotNull] this T? obj,
|
|
|
|
|
[CallerArgumentExpression(nameof(obj))] string? paramName = null
|
|
|
|
|
)
|
|
|
|
|
where T : struct
|
|
|
|
|
{
|
2026-02-11 11:04:25 +00:00
|
|
|
if (!obj.HasValue)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(paramName);
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-22 14:17:08 +01:00
|
|
|
return obj.Value;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2025-10-22 09:17:13 +01:00
|
|
|
|
2025-10-23 11:43:21 +01:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2025-10-22 09:17:13 +01:00
|
|
|
public static string NotNullOrEmpty(this string obj, string name)
|
|
|
|
|
{
|
|
|
|
|
obj.NotNull(name);
|
|
|
|
|
if (obj.Length == 0)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("String is empty.", name);
|
|
|
|
|
}
|
|
|
|
|
return obj;
|
|
|
|
|
}
|
2024-04-22 14:17:08 +01:00
|
|
|
}
|