-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathConditions.cs
More file actions
110 lines (99 loc) · 4.01 KB
/
Conditions.cs
File metadata and controls
110 lines (99 loc) · 4.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
namespace Menees
{
#region Using Directives
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Linq.Expressions;
using System.Runtime.CompilerServices;
using System.Text;
using Menees.Diagnostics;
#endregion
/// <summary>
/// Provides several methods for checking preconditions and postconditions
/// (i.e., design-by-contract).
/// </summary>
public static class Conditions
{
#region Public Methods
/// <summary>
/// Requires that the argument's state is valid (i.e., true).
/// </summary>
/// <param name="argState">The argument's state.</param>
/// <param name="explanation">The name of the arg to put in the exception.</param>
/// <exception cref="ArgumentException">If <paramref name="argState"/> is false.</exception>
public static void RequireArgument([DoesNotReturnIf(false)] bool argState, string explanation)
{
RequireArgument(argState, explanation, null);
}
/// <summary>
/// Requires that the named argument's state is valid (i.e., true).
/// </summary>
/// <param name="argState">The argument's state.</param>
/// <param name="explanation">The explanation to put in the exception.</param>
/// <param name="argName">The name of the arg to put in the exception.</param>
/// <exception cref="ArgumentException">If <paramref name="argState"/> is false.</exception>
public static void RequireArgument([DoesNotReturnIf(false)] bool argState, string explanation, string? argName)
{
if (!argState)
{
throw Exceptions.NewArgumentException(explanation, argName);
}
}
/// <summary>
/// Makes sure a collection is non-null and non-empty.
/// </summary>
/// <typeparam name="T">The type of item in the collection.</typeparam>
/// <param name="arg">The collection to check.</param>
/// <param name="argName">The name of the argument to put in the exception.</param>
public static IEnumerable<T> RequireCollection<T>([NotNull] IEnumerable<T>? arg, [CallerArgumentExpression(nameof(arg))] string? argName = null)
{
RequireArgument(!CollectionUtility.IsNullOrEmpty(arg), "The collection must be non-empty.", argName);
return arg;
}
/// <summary>
/// Requires that a reference is non-null.
/// </summary>
/// <param name="reference">The reference to check.</param>
/// <param name="argName">The arg name to put in the exception.</param>
/// <exception cref="ArgumentNullException">If <paramref name="reference"/> is null.</exception>
[return: NotNull]
public static T RequireReference<T>([NotNull] T? reference, [CallerArgumentExpression(nameof(reference))] string? argName = null)
{
// Note: This method doesn't use a "where T : class" constraint because we need to allow generic methods
// and types to call Conditions.RequireReference even if they aren't constrained to just classes.
if (reference == null)
{
throw Exceptions.NewArgumentNullException(argName!);
}
return reference;
}
/// <summary>
/// Requires that the given state is valid (i.e., true).
/// </summary>
/// <param name="state">The state to check.</param>
/// <param name="explanation">The explanation to put in the exception.</param>
/// <exception cref="InvalidOperationException">If <paramref name="state"/> is false.</exception>
public static void RequireState([DoesNotReturnIf(false)] bool state, string explanation)
{
if (!state)
{
throw Exceptions.NewInvalidOperationException(explanation);
}
}
/// <summary>
/// Requires that a string is non-null and non-empty.
/// </summary>
/// <param name="arg">The string to check.</param>
/// <param name="argName">The name of the arg to put in the exception.</param>
/// <exception cref="ArgumentException">If <paramref name="arg"/> is null or empty.</exception>
public static string RequireString([NotNull] string? arg, [CallerArgumentExpression(nameof(arg))] string? argName = null)
{
RequireArgument(arg.IsNotEmpty(), "The string must be non-empty.", argName);
return arg;
}
#endregion
}
}