| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="BooleanExtensions.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | |
| | | 9 | | #pragma warning disable IDE0130 // Namespace does not match folder structure |
| | | 10 | | namespace MyNet.Primitives; |
| | | 11 | | #pragma warning restore IDE0130 // Namespace does not match folder structure |
| | | 12 | | |
| | | 13 | | public static class BooleanExtensions |
| | | 14 | | { |
| | | 15 | | extension(bool value) |
| | | 16 | | { |
| | | 17 | | /// <summary> |
| | | 18 | | /// Executes the specified action if the boolean is true. If the boolean is false, the action is not executed. |
| | | 19 | | /// </summary> |
| | | 20 | | /// <param name="ifTrue">The action to execute if the boolean is true.</param> |
| | | 21 | | /// <exception cref="ArgumentNullException">Thrown if the <paramref name="ifTrue"/> action is null.</exception> |
| | | 22 | | public void IfTrue(Action ifTrue) |
| | | 23 | | { |
| | 12 | 24 | | ArgumentNullException.ThrowIfNull(ifTrue); |
| | | 25 | | |
| | 9 | 26 | | if (value) |
| | 6 | 27 | | ifTrue(); |
| | 9 | 28 | | } |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// Executes the specified action if the boolean is false. If the boolean is true, the action is not executed. |
| | | 32 | | /// </summary> |
| | | 33 | | /// <param name="ifFalse">The action to execute if the boolean is false.</param> |
| | | 34 | | /// <exception cref="ArgumentNullException">Thrown if the <paramref name="ifFalse"/> action is null.</exception> |
| | | 35 | | public void IfFalse(Action ifFalse) |
| | | 36 | | { |
| | 51 | 37 | | ArgumentNullException.ThrowIfNull(ifFalse); |
| | | 38 | | |
| | 48 | 39 | | if (!value) |
| | 45 | 40 | | ifFalse(); |
| | 48 | 41 | | } |
| | | 42 | | } |
| | | 43 | | |
| | | 44 | | extension(bool? value) |
| | | 45 | | { |
| | | 46 | | /// <summary> |
| | | 47 | | /// Returns true if the nullable boolean has a value of true; otherwise, false. This method returns false if the |
| | | 48 | | /// </summary> |
| | 18 | 49 | | public bool IsTrue() => value == true; |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Returns true if the nullable boolean has a value of false; otherwise, false. This method returns false if th |
| | | 53 | | /// </summary> |
| | 18 | 54 | | public bool IsFalse() => value != true; |
| | | 55 | | |
| | | 56 | | /// <summary> |
| | | 57 | | /// Executes the specified action if the nullable boolean has a value of true. If the nullable boolean is null o |
| | | 58 | | /// </summary> |
| | | 59 | | /// <param name="ifTrue">The action to execute if the nullable boolean is true.</param> |
| | | 60 | | /// <exception cref="ArgumentNullException">Thrown if the <paramref name="ifTrue"/> action is null.</exception> |
| | | 61 | | public void IfTrue(Action ifTrue) |
| | | 62 | | { |
| | 12 | 63 | | ArgumentNullException.ThrowIfNull(ifTrue); |
| | | 64 | | |
| | 9 | 65 | | if (value.IsTrue()) |
| | 3 | 66 | | ifTrue(); |
| | 9 | 67 | | } |
| | | 68 | | |
| | | 69 | | /// <summary> |
| | | 70 | | /// Executes the specified action if the nullable boolean has a value of false. If the nullable boolean is null |
| | | 71 | | /// </summary> |
| | | 72 | | /// <param name="ifFalse">The action to execute if the nullable boolean is false.</param> |
| | | 73 | | /// <exception cref="ArgumentNullException">Thrown if the <paramref name="ifFalse"/> action is null.</exception> |
| | | 74 | | public void IfFalse(Action ifFalse) |
| | | 75 | | { |
| | 12 | 76 | | ArgumentNullException.ThrowIfNull(ifFalse); |
| | | 77 | | |
| | 9 | 78 | | if (value.IsFalse()) |
| | 6 | 79 | | ifFalse(); |
| | 9 | 80 | | } |
| | | 81 | | } |
| | | 82 | | } |
| | | 83 | | |