Skip to content

Commit

Permalink
Add comments to code.
Browse files Browse the repository at this point in the history
  • Loading branch information
olivegamestudio committed Aug 28, 2024
1 parent 8741aa1 commit b88f988
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions Utility.Guard/Guard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,18 @@

namespace Utility;

/// <summary>
/// Provides guard clauses for validating method arguments.
/// </summary>
public static class Guard
{
/// <summary>
/// Throws an <see cref="InvalidOperationException"/> if the specified item is null.
/// </summary>
/// <typeparam name="T">The type of the item.</typeparam>
/// <param name="item">The item to check for null.</param>
/// <returns>The original item if it is not null.</returns>
/// <exception cref="InvalidOperationException">Thrown if the item is null.</exception>
public static T ThrowIfNull<T>(T item)
{
if (item is null)
Expand All @@ -14,6 +24,13 @@ public static T ThrowIfNull<T>(T item)
return item;
}

/// <summary>
/// Throws an <see cref="InvalidOperationException"/> if the specified item is not null.
/// </summary>
/// <typeparam name="T">The type of the item.</typeparam>
/// <param name="item">The item to check for not null.</param>
/// <returns>The original item if it is null.</returns>
/// <exception cref="InvalidOperationException">Thrown if the item is not null.</exception>
public static T ThrowIfNotNull<T>(T item)
{
if (item is not null)
Expand All @@ -24,6 +41,14 @@ public static T ThrowIfNotNull<T>(T item)
return item;
}

/// <summary>
/// Throws an <see cref="InvalidOperationException"/> if the specified integer is within the specified range.
/// </summary>
/// <param name="item">The integer to check.</param>
/// <param name="start">The start of the range.</param>
/// <param name="end">The end of the range.</param>
/// <returns>The original integer if it is not within the range.</returns>
/// <exception cref="InvalidOperationException">Thrown if the integer is within the range.</exception>
public static int ThrowIfIntegerNotInRange(int item, int start, int end)
{
if (item >= start && item <= end)
Expand All @@ -34,6 +59,12 @@ public static int ThrowIfIntegerNotInRange(int item, int start, int end)
return item;
}

/// <summary>
/// Throws an <see cref="InvalidOperationException"/> if the specified string is null or empty.
/// </summary>
/// <param name="value">The string to check.</param>
/// <returns>The original string if it is not null or empty.</returns>
/// <exception cref="InvalidOperationException">Thrown if the string is null or empty.</exception>
public static string ThrowIfStringNullOrEmpty(string value)
{
if (string.IsNullOrEmpty(value))
Expand Down

0 comments on commit b88f988

Please sign in to comment.