1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . Diagnostics ;
4+ using System . Runtime . CompilerServices ;
5+
6+ using JetBrains . Annotations ;
7+
8+ using static CodeJam . PlatformDependent ;
9+
10+ namespace CodeJam
11+ {
12+ /// <summary>
13+ /// <see cref="ArgumentAssertion{T}"/> extension methods.
14+ /// </summary>
15+ [ PublicAPI ]
16+ public static class ArgumentAssertionExtensions
17+ {
18+ /// <summary>
19+ /// Ensures that <paramref name="arg"/> != <c>null</c>
20+ /// </summary>
21+ /// <typeparam name="T">Type of the value. Auto-inferred in most cases</typeparam>
22+ /// <param name="arg">The argument.</param>
23+ [ DebuggerHidden , MethodImpl ( AggressiveInlining ) ]
24+ [ AssertionMethod ]
25+ public static ArgumentAssertion < T > NotNull < T > ( [ NoEnumeration ] this ArgumentAssertion < T > arg ) where T : class
26+ {
27+ Code . NotNull ( arg . Argument , arg . ArgumentName ) ;
28+ return arg ;
29+ }
30+
31+ /// <summary>
32+ /// Ensures that <paramref name="arg"/> != <c>null</c>
33+ /// </summary>
34+ /// <typeparam name="T">Type of the value. Auto-inferred in most cases</typeparam>
35+ /// <param name="arg">The argument.</param>
36+ [ DebuggerHidden , MethodImpl ( AggressiveInlining ) ]
37+ [ AssertionMethod ]
38+ public static ArgumentAssertion < T ? > NotNull < T > ( [ NoEnumeration ] this ArgumentAssertion < T ? > arg ) where T : struct
39+ {
40+ Code . NotNull ( arg . Argument , arg . ArgumentName ) ;
41+ return arg ;
42+ }
43+
44+ /// <summary>Ensures that all items in <paramref name="arg"/> != <c>null</c></summary>
45+ /// <typeparam name="T">Type of the value. Auto-inferred in most cases</typeparam>
46+ /// <param name="arg">The argument.</param>
47+ [ DebuggerHidden , MethodImpl ( AggressiveInlining ) ]
48+ [ AssertionMethod ]
49+ public static ArgumentAssertion < IEnumerable < T > > ItemNotNull < T > ( this ArgumentAssertion < IEnumerable < T > > arg ) where T : class
50+ {
51+ Code . ItemNotNull ( arg . Argument , arg . ArgumentName ) ;
52+ return arg ;
53+ }
54+
55+ /// <summary>
56+ /// Ensures that supplied enumerable is not empty.
57+ /// </summary>
58+ /// <typeparam name="T">Type of item.</typeparam>
59+ /// <param name="arg">The argument.</param>
60+ [ DebuggerHidden , MethodImpl ( AggressiveInlining ) ]
61+ [ AssertionMethod ]
62+ public static ArgumentAssertion < IEnumerable < T > > NotEmpty < T > ( this ArgumentAssertion < IEnumerable < T > > arg )
63+ {
64+ Code . NotEmpty ( arg . Argument , arg . ArgumentName ) ;
65+ return arg ;
66+ }
67+
68+ /// <summary>
69+ /// Ensures that supplied collection is not empty.
70+ /// </summary>
71+ /// <typeparam name="T">Type of item.</typeparam>
72+ /// <param name="arg">The argument.</param>
73+ [ DebuggerHidden , MethodImpl ( AggressiveInlining ) ]
74+ [ AssertionMethod ]
75+ public static ArgumentAssertion < ICollection < T > > NotEmpty < T > ( this ArgumentAssertion < ICollection < T > > arg )
76+ {
77+ Code . NotEmpty ( arg . Argument , arg . ArgumentName ) ;
78+ return arg ;
79+ }
80+
81+ /// <summary>
82+ /// Ensures that supplied array is not empty.
83+ /// </summary>
84+ /// <typeparam name="T">Type of item.</typeparam>
85+ /// <param name="arg">The argument.</param>
86+ [ DebuggerHidden , MethodImpl ( AggressiveInlining ) ]
87+ [ AssertionMethod ]
88+ public static ArgumentAssertion < T [ ] > NotEmpty < T > ( this ArgumentAssertion < T [ ] > arg )
89+ {
90+ Code . NotEmpty ( arg . Argument , arg . ArgumentName ) ;
91+ return arg ;
92+ }
93+
94+ /// <summary>Ensures that <paramref name="arg"/> is not null nor empty</summary>
95+ /// <param name="arg">The argument.</param>
96+ [ DebuggerHidden , MethodImpl ( AggressiveInlining ) ]
97+ [ AssertionMethod ]
98+ public static ArgumentAssertion < string > NotNullNorEmpty ( this ArgumentAssertion < string > arg )
99+ {
100+ Code . NotNullNorEmpty ( arg . Argument , arg . ArgumentName ) ;
101+ return arg ;
102+ }
103+
104+ /// <summary>Ensures that <paramref name="arg"/> is not null nor white space</summary>
105+ /// <param name="arg">The argument.</param>
106+ [ DebuggerHidden , MethodImpl ( AggressiveInlining ) ]
107+ [ AssertionMethod ]
108+ public static ArgumentAssertion < string > NotNullNorWhiteSpace ( this ArgumentAssertion < string > arg )
109+ {
110+ Code . NotNullNorWhiteSpace ( arg . Argument , arg . ArgumentName ) ;
111+ return arg ;
112+ }
113+
114+ /// <summary>Assertion for the argument value</summary>
115+ /// <param name="arg">The argument.</param>
116+ /// <param name="condition">The condition to check</param>
117+ /// <param name="message">The message.</param>
118+ [ DebuggerHidden , MethodImpl ( AggressiveInlining ) ]
119+ [ AssertionMethod ]
120+ public static ArgumentAssertion < T > Assert < T > (
121+ this ArgumentAssertion < T > arg ,
122+ [ AssertionCondition ( AssertionConditionType . IS_TRUE ) ] bool condition ,
123+ [ NotNull ] string message )
124+ {
125+ Code . AssertArgument ( condition , arg . ArgumentName , message ) ;
126+ return arg ;
127+ }
128+
129+ /// <summary>Assertion for the argument value</summary>
130+ /// <param name="arg">Argument.</param>
131+ /// <param name="condition">The condition to check</param>
132+ /// <param name="messageFormat">The message format.</param>
133+ /// <param name="args">Format string arguments.</param>
134+ [ DebuggerHidden , MethodImpl ( AggressiveInlining ) ]
135+ [ AssertionMethod , StringFormatMethod ( "messageFormat" ) ]
136+ public static ArgumentAssertion < T > Assert < T > (
137+ this ArgumentAssertion < T > arg ,
138+ [ AssertionCondition ( AssertionConditionType . IS_TRUE ) ] bool condition ,
139+ [ NotNull ] string messageFormat ,
140+ [ CanBeNull ] params object [ ] args )
141+ {
142+ Code . AssertArgument ( condition , arg . ArgumentName , messageFormat , args ) ;
143+ return arg ;
144+ }
145+ }
146+ }
0 commit comments