| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Diagnostics.CodeAnalysis; |
| | | 4 | | using System.Runtime.CompilerServices; |
| | | 5 | | |
| | | 6 | | namespace SwiftCollections; |
| | | 7 | | |
| | | 8 | | public static class SwiftThrowHelper |
| | | 9 | | { |
| | | 10 | | #nullable enable |
| | | 11 | | |
| | | 12 | | /// <inheritdoc cref="ArgumentNullException"/> |
| | | 13 | | [MethodImpl(MethodImplOptions.NoInlining)] |
| | | 14 | | public static void ThrowIfNull([NotNull] object? argument, string? paramName = null) |
| | 472874 | 15 | | { |
| | 472874 | 16 | | if (argument is null) |
| | 27 | 17 | | ThrowArgumentNullException(paramName); |
| | 472847 | 18 | | } |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// Throws an exception if the specified value is null and nulls are not allowed for TValue. |
| | | 22 | | /// </summary> |
| | | 23 | | /// <param name="value">The value to check.</param> |
| | | 24 | | /// <param name="defaultValue">A default value of type TValue used to determine if nulls are illegal.</param> |
| | | 25 | | /// <exception cref="ArgumentNullException">The value is null and TValue is a value type.</exception> |
| | | 26 | | [MethodImpl(MethodImplOptions.NoInlining)] |
| | | 27 | | public static void ThrowIfNullAndNullsAreIllegal<TValue>(object value, TValue? defaultValue) |
| | 6 | 28 | | { |
| | 6 | 29 | | if (value == null && !(defaultValue == null)) |
| | 0 | 30 | | ThrowArgumentNullException(nameof(value)); |
| | 6 | 31 | | } |
| | | 32 | | |
| | | 33 | | [DoesNotReturn] |
| | | 34 | | [MethodImpl(MethodImplOptions.NoInlining)] |
| | | 35 | | private static void ThrowArgumentNullException(string? paramName) => |
| | 27 | 36 | | throw new ArgumentNullException(paramName); |
| | | 37 | | |
| | | 38 | | /// <inheritdoc cref="ArgumentOutOfRangeException"/> |
| | | 39 | | [MethodImpl(MethodImplOptions.NoInlining)] |
| | | 40 | | public static void ThrowIfNegative(int value, string? paramName = null) |
| | 78 | 41 | | { |
| | 78 | 42 | | if (value < 0) |
| | 0 | 43 | | ThrowArgumentOutOfRangeException(value, paramName); |
| | 78 | 44 | | } |
| | | 45 | | |
| | | 46 | | /// <inheritdoc cref="ArgumentOutOfRangeException"/> |
| | | 47 | | [MethodImpl(MethodImplOptions.NoInlining)] |
| | | 48 | | public static void ThrowIfNegativeOrZero(int value, string? paramName = null) |
| | 95 | 49 | | { |
| | 95 | 50 | | if (value < 0 || value == 0) |
| | 2 | 51 | | ThrowArgumentOutOfRangeException(value, paramName); |
| | 93 | 52 | | } |
| | | 53 | | |
| | | 54 | | [DoesNotReturn] |
| | | 55 | | [MethodImpl(MethodImplOptions.NoInlining)] |
| | | 56 | | private static void ThrowArgumentOutOfRangeException(int value, string? paramName) => |
| | 2 | 57 | | throw new ArgumentOutOfRangeException(paramName, $"{paramName} must be greater than zero. Value: {value}"); |
| | | 58 | | |
| | | 59 | | /// <inheritdoc cref="ObjectDisposedException"/> |
| | | 60 | | [MethodImpl(MethodImplOptions.NoInlining)] |
| | | 61 | | public static void ThrowIfDisposed([DoesNotReturnIf(true)] bool condition, string? objectName = null) |
| | 195 | 62 | | { |
| | 195 | 63 | | if (condition) |
| | 11 | 64 | | ThrowObjectDisposedException(objectName); |
| | 184 | 65 | | } |
| | | 66 | | |
| | | 67 | | [DoesNotReturn] |
| | | 68 | | [MethodImpl(MethodImplOptions.NoInlining)] |
| | | 69 | | private static void ThrowObjectDisposedException(string? objectName) => |
| | 11 | 70 | | throw new ObjectDisposedException(objectName); |
| | | 71 | | |
| | | 72 | | /// <inheritdoc cref="KeyNotFoundException"/> |
| | | 73 | | [MethodImpl(MethodImplOptions.NoInlining)] |
| | | 74 | | public static void ThrowIfKeyInvalid(int index, object? key = null) |
| | 4162 | 75 | | { |
| | 4162 | 76 | | if (index < 0) |
| | 3 | 77 | | ThrowKeyNotFoundException(index, key); |
| | 4159 | 78 | | } |
| | | 79 | | |
| | | 80 | | [DoesNotReturn] |
| | | 81 | | [MethodImpl(MethodImplOptions.NoInlining)] |
| | | 82 | | private static void ThrowKeyNotFoundException(int index, object? key) => |
| | 3 | 83 | | throw new KeyNotFoundException($"Key not found: {key}"); |
| | | 84 | | |
| | | 85 | | /// <inheritdoc cref="IndexOutOfRangeException"/> |
| | | 86 | | [MethodImpl(MethodImplOptions.NoInlining)] |
| | | 87 | | public static void ThrowIfIndexInvalid(int index, int count) |
| | 205 | 88 | | { |
| | 205 | 89 | | if ((uint)index >= (uint)count) |
| | 1 | 90 | | ThrowIndexOutOfRangeException(index); |
| | 204 | 91 | | } |
| | | 92 | | |
| | | 93 | | [DoesNotReturn] |
| | | 94 | | [MethodImpl(MethodImplOptions.NoInlining)] |
| | | 95 | | private static void ThrowIndexOutOfRangeException(int value) => |
| | 1 | 96 | | throw new IndexOutOfRangeException($"Index out of range: {value}"); |
| | | 97 | | } |