/// <summary> /// <para>Throws <see cref="ObjectIsNullContractViolationException"/> if any inspected value is null</para> /// <para>Consider using <see cref="NotNullOrDefault{TArgument}"/> instead as it works for value types as well and is only marginally slower.</para> /// </summary> public static Inspected <TValue> NotNull <TValue>(this Inspected <TValue> me) where TValue : class { return(me.Inspect( inspected => !ReferenceEquals(inspected, null), badValue => new ObjectIsNullContractViolationException(badValue))); }
///<summary> /// <para>Throws <see cref="ObjectIsNullContractViolationException"/> if any expected value is null.</para> /// <para>Throws <see cref="StringIsEmptyContractViolationException"/> if any inspected value is an empty string.</para> /// <para>Throws <see cref="StringIsWhitespaceContractViolationException"/> if any inspected value is a string containing only whitespace.</para> /// </summary> public static Inspected <String> NotNullEmptyOrWhiteSpace(this Inspected <String> me) { me.NotNullOrEmpty(); //We want the proper exceptions return(me.Inspect( inspected => inspected.Trim() != String.Empty, badValue => new StringIsWhitespaceContractViolationException(badValue))); }
///<summary> /// <para>Throws <see cref="ObjectIsNullContractViolationException"/> if any inspected value is null.</para> /// <para>Throws a <see cref="EnumerableIsEmptyContractViolationException"/> if any inspected value is an empty sequence.</para> /// </summary> public static Inspected <TValue> NotNullOrEmptyEnumerable <TValue>(this Inspected <TValue> me) where TValue : IEnumerable { me.Inspect( inspected => !ReferenceEquals(inspected, null), badValue => new ObjectIsNullContractViolationException(badValue)); return(me.Inspect( inspected => inspected.Cast <object>().Any(), badValue => new EnumerableIsEmptyContractViolationException(badValue))); }
/// <summary> /// <para>Throws <see cref="ObjectIsNullContractViolationException"/> if any inspected value is null</para> /// <para>Throws <see cref="ObjectIsDefaultContractViolationException"/> if any inspected value is default(TValue). Such as 0 for integer, Guid.Empty for Guid, new SomeStruct().</para> /// </summary> public static Inspected <TValue> NotNullOrDefault <TValue>(this Inspected <TValue> me) { me.Inspect( inspected => !ReferenceEquals(inspected, null), badValue => new ObjectIsNullContractViolationException(badValue)); return(me.Inspect( inspected => { if (!inspected.GetType().IsValueType) { return true; } var valueTypeDefault = Activator.CreateInstance(inspected.GetType()); return !Equals(inspected, valueTypeDefault); }, badValue => new ObjectIsDefaultContractViolationException(badValue))); }
///<summary>Throws a <see cref="GuidIsEmptyContractViolationException"/> if any inspected value is Guid.Empty</summary> public static Inspected <Guid> NotEmpty(this Inspected <Guid> me) { return(me.Inspect( inspected => inspected != Guid.Empty, badValue => new GuidIsEmptyContractViolationException(badValue))); }
///<summary> /// <para>Throws <see cref="ObjectIsNullContractViolationException"/> if any expected value is null.</para> /// <para>Throws <see cref="StringIsEmptyContractViolationException"/> if any inspected value is an empty string.</para> /// </summary> public static Inspected <string> NotNullOrEmpty(this Inspected <string> me) { me.NotNull(); //We want the proper exceptions return(me.Inspect(inspected => inspected != String.Empty, badValue => new StringIsEmptyContractViolationException(badValue))); }