1+ // Licensed to the .NET Foundation under one or more agreements.
2+ // The .NET Foundation licenses this file to you under the MIT license.
3+
4+ #nullable enable
5+
16using System . Diagnostics . CodeAnalysis ;
7+ using System . Runtime . CompilerServices ;
28using JetBrains . Annotations ;
39
410namespace Microsoft . EntityFrameworkCore . Utilities ;
@@ -8,13 +14,13 @@ internal static class Check
814{
915 [ ContractAnnotation ( "value:null => halt" ) ]
1016 [ return : NotNull ]
11- public static T NotNull < T > ( [ NoEnumeration ] [ AllowNull ] [ NotNull ] T value , [ InvokerParameterName ] string parameterName )
17+ public static T NotNull < T > (
18+ [ NoEnumeration , AllowNull , NotNull ] T value ,
19+ [ InvokerParameterName , CallerArgumentExpression ( nameof ( value ) ) ] string parameterName = "" )
1220 {
1321 if ( value is null )
1422 {
15- NotEmpty ( parameterName , nameof ( parameterName ) ) ;
16-
17- throw new ArgumentNullException ( parameterName ) ;
23+ ThrowArgumentNull ( parameterName ) ;
1824 }
1925
2026 return value ;
@@ -23,108 +29,123 @@ public static T NotNull<T>([NoEnumeration] [AllowNull] [NotNull] T value, [Invok
2329 [ ContractAnnotation ( "value:null => halt" ) ]
2430 public static IReadOnlyList < T > NotEmpty < T > (
2531 [ NotNull ] IReadOnlyList < T > ? value ,
26- [ InvokerParameterName ] string parameterName )
32+ [ InvokerParameterName , CallerArgumentExpression ( nameof ( value ) ) ] string parameterName = "" )
2733 {
2834 NotNull ( value , parameterName ) ;
2935
3036 if ( value . Count == 0 )
3137 {
32- NotEmpty ( parameterName , nameof ( parameterName ) ) ;
33-
34- throw new ArgumentException ( AbstractionsStrings . CollectionArgumentIsEmpty ( parameterName ) ) ;
38+ ThrowNotEmpty ( parameterName ) ;
3539 }
3640
3741 return value ;
3842 }
3943
4044 [ ContractAnnotation ( "value:null => halt" ) ]
41- public static string NotEmpty ( [ NotNull ] string ? value , [ InvokerParameterName ] string parameterName )
45+ public static string NotEmpty (
46+ [ NotNull ] string ? value ,
47+ [ InvokerParameterName , CallerArgumentExpression ( nameof ( value ) ) ] string parameterName = "" )
4248 {
43- if ( value is null )
44- {
45- NotEmpty ( parameterName , nameof ( parameterName ) ) ;
46- throw new ArgumentNullException ( parameterName ) ;
47- }
49+ NotNull ( value , parameterName ) ;
4850
49- if ( value . Trim ( ) . Length == 0 )
51+ if ( value . AsSpan ( ) . Trim ( ) . Length == 0 )
5052 {
51- NotEmpty ( parameterName , nameof ( parameterName ) ) ;
52- throw new ArgumentException ( AbstractionsStrings . ArgumentIsEmpty ( parameterName ) ) ;
53+ ThrowStringArgumentEmpty ( parameterName ) ;
5354 }
5455
5556 return value ;
5657 }
5758
58- public static string ? NullButNotEmpty ( string ? value , [ InvokerParameterName ] string parameterName )
59+ public static IReadOnlyCollection < T > ? NullButNotEmpty < T > (
60+ IReadOnlyCollection < T > ? value ,
61+ [ InvokerParameterName , CallerArgumentExpression ( nameof ( value ) ) ] string parameterName = "" )
5962 {
60- if ( value is not null && value . Length == 0 )
63+ if ( value is not null && value . Count == 0 )
6164 {
62- NotEmpty ( parameterName , nameof ( parameterName ) ) ;
63-
64- throw new ArgumentException ( AbstractionsStrings . ArgumentIsEmpty ( parameterName ) ) ;
65+ ThrowStringArgumentEmpty ( parameterName ) ;
6566 }
6667
6768 return value ;
6869 }
6970
70- public static IReadOnlyCollection < T > ? NullButNotEmpty < T > (
71- IReadOnlyCollection < T > ? value ,
72- [ InvokerParameterName ] string parameterName )
71+ public static string ? NullButNotEmpty (
72+ string ? value ,
73+ [ InvokerParameterName , CallerArgumentExpression ( nameof ( value ) ) ] string parameterName = "" )
7374 {
74- if ( value is { Count : 0 } )
75+ if ( value is not null && value . Length == 0 )
7576 {
76- NotEmpty ( parameterName , nameof ( parameterName ) ) ;
77-
78- throw new ArgumentException ( AbstractionsStrings . ArgumentIsEmpty ( parameterName ) ) ;
77+ ThrowStringArgumentEmpty ( parameterName ) ;
7978 }
8079
8180 return value ;
8281 }
8382
8483 public static IReadOnlyList < T > HasNoNulls < T > (
8584 [ NotNull ] IReadOnlyList < T > ? value ,
86- [ InvokerParameterName ] string parameterName )
85+ [ InvokerParameterName , CallerArgumentExpression ( nameof ( value ) ) ] string parameterName = "" )
8786 where T : class
8887 {
8988 NotNull ( value , parameterName ) ;
9089
91- if ( value . Any ( e => e is null ) )
90+ for ( var i = 0 ; i < value . Count ; i ++ )
9291 {
93- NotEmpty ( parameterName , nameof ( parameterName ) ) ;
94-
95- throw new ArgumentException ( parameterName ) ;
92+ if ( value [ i ] is null )
93+ {
94+ ThrowArgumentException ( parameterName , parameterName ) ;
95+ }
9696 }
9797
9898 return value ;
9999 }
100100
101101 public static IReadOnlyList < string > HasNoEmptyElements (
102102 [ NotNull ] IReadOnlyList < string > ? value ,
103- [ InvokerParameterName ] string parameterName )
103+ [ InvokerParameterName , CallerArgumentExpression ( nameof ( value ) ) ] string parameterName = "" )
104104 {
105105 NotNull ( value , parameterName ) ;
106106
107- if ( value . Any ( s => string . IsNullOrWhiteSpace ( s ) ) )
107+ for ( var i = 0 ; i < value . Count ; i ++ )
108108 {
109- NotEmpty ( parameterName , nameof ( parameterName ) ) ;
110-
111- throw new ArgumentException ( AbstractionsStrings . CollectionArgumentHasEmptyElements ( parameterName ) ) ;
109+ if ( string . IsNullOrWhiteSpace ( value [ i ] ) )
110+ {
111+ ThrowCollectionHasEmptyElements ( parameterName ) ;
112+ }
112113 }
113114
114115 return value ;
115116 }
116117
117118 [ Conditional ( "DEBUG" ) ]
118- public static void DebugAssert ( [ DoesNotReturnIf ( false ) ] bool condition , string message )
119+ public static void DebugAssert ( [ DoesNotReturnIf ( false ) ] bool condition , [ CallerArgumentExpression ( nameof ( condition ) ) ] string message = "" )
119120 {
120121 if ( ! condition )
121122 {
122- throw new Exception ( $ "Check.DebugAssert failed: { message } ") ;
123+ throw new UnreachableException ( $ "Check.DebugAssert failed: { message } ") ;
123124 }
124125 }
125126
126127 [ Conditional ( "DEBUG" ) ]
127128 [ DoesNotReturn ]
128129 public static void DebugFail ( string message )
129- => throw new Exception ( $ "Check.DebugFail failed: { message } ") ;
130+ => throw new UnreachableException ( $ "Check.DebugFail failed: { message } ") ;
131+
132+ [ DoesNotReturn ]
133+ private static void ThrowArgumentNull ( string parameterName )
134+ => throw new ArgumentNullException ( parameterName ) ;
135+
136+ [ DoesNotReturn ]
137+ private static void ThrowNotEmpty ( string parameterName )
138+ => throw new ArgumentException ( AbstractionsStrings . CollectionArgumentIsEmpty , parameterName ) ;
139+
140+ [ DoesNotReturn ]
141+ private static void ThrowStringArgumentEmpty ( string parameterName )
142+ => throw new ArgumentException ( AbstractionsStrings . ArgumentIsEmpty , parameterName ) ;
143+
144+ [ DoesNotReturn ]
145+ private static void ThrowCollectionHasEmptyElements ( string parameterName )
146+ => throw new ArgumentException ( AbstractionsStrings . CollectionArgumentHasEmptyElements , parameterName ) ;
147+
148+ [ DoesNotReturn ]
149+ private static void ThrowArgumentException ( string message , string parameterName )
150+ => throw new ArgumentException ( message , parameterName ) ;
130151}
0 commit comments