using System;
namespace DTT.Utils.Extensions
{
///
/// Provides generic extension methods.
///
public static class GenericExtensions
{
///
/// Checks a value by throwing an exception if it is equal to a faulty one.
///
/// The type of value.
/// The value to check.
/// The value that should be excepted.
/// The exception to throw if the catch failed.
/// The caught value.
public static T ThrowIf(this T value, T faultyValue, Exception onCatch = null)
{
if (value.Equals(faultyValue))
throw onCatch ?? new InvalidOperationException($"Value {value} was faulty.");
return value;
}
///
/// Replaces a value with a default value if it is equal to a faulty one.
///
/// The type of value.
/// The value to check.
/// The value that should be excepted.
/// The default value to use as replacement.
/// The caught value.
public static T ReplaceIf(this T value, T faultyValue, T defaultValue = default(T)) => value.Equals(faultyValue) ? defaultValue : value;
}
}