Class Conditional


  • public final class Conditional
    extends Object
    Basic entity of Conditional library, that replaces if-else statements and ternary operators with a featured fluent interface.
    1. Conditional can be instantiated via a conditional(boolean) method. That method accepts a boolean argument (true/false), which becomes an immutable value described by a constructed Conditional. Described value determines behavior of a given instance of Conditional.
    2. To a given instance of Conditional multiple actions can be submitted in a fluent manner.
    3. Every submitted action is bound either to a true or false boolean value, depending on the used submission method.
    4. Submitted actions are supposed to be executed in a void manner or to be executed and return a value in the result of that execution. To achieve that, usage of Conditional is finalized via an execute(...) or get(...) methods respectively, that trigger actions bound to a value described by a given instance of Conditional.
    5. Conditional is lazy, which means that respective submitted actions will be triggered when and only when an execute(...) or get(...) methods are called, hence mere submission of an action doesn't suffice to trigger that action.
    Usage example with void action:
    
     import static eu.ciechanowiec.conditional.Conditional.conditional;
    
     public static void main(String[] args) {
         conditional(10 % 2 == 0)
                 .onTrue(() -> System.out.println("Checked number is even"))
                 .onFalse(() -> System.out.println("Checked number is odd"))
                 .execute();
          // Output:
         // Checked number is even
     }
    Usage example with action returning a value:
    
     import static eu.ciechanowiec.conditional.Conditional.conditional;
    
     public static void main(String[] args) {
         String evenOrOdd = evenOrOdd(10);
         System.out.println("Result: " + evenOrOdd);
         // Output:
         // Returning a value on true...
         // Result: Even!
     }
    
     private static String evenOrOdd(int numToCheck) {
         return conditional(numToCheck % 2 == 0)
                           .onTrue(() -> {
                               System.out.println("Returning a value on true...");
                               return "Even!";
                           })
                           .onFalse(() -> "Odd!")
                           .get(String.class);
     }
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      ActionsList actionsOnFalse()
      Retrieves by reference an ActionsList that stores all actions submitted to this conditional and bound to a false value.
      ActionsList actionsOnTrue()
      Retrieves by reference an ActionsList that stores all actions submitted to this conditional and bound to a true value.
      static Conditional conditional​(boolean describedValue)
      Returns a new instance of a Conditional that describes the passed boolean value (true or false).
      boolean describedValue()
      Returns the value described by this conditional (true or false).
      Conditional discardActionsOnFalse()
      Removes all actions submitted to this conditional and bound to a false value.
      Conditional discardActionsOnTrue()
      Removes all actions submitted to this conditional and bound to a true value.
      Conditional discardAllActions()
      Removes all actions submitted to this conditional.
      Conditional execute()
      Executes all submitted actions, bound to the value described by this conditional.
      Conditional execute​(int cyclesToExecute)
      Executes the specified amount of cycles all submitted actions, bound to the value described by this conditional.
      <X extends Exception>
      Conditional
      execute​(Class<X> expectedException)
      Executes all submitted actions, bound to the value described by this conditional, and sets the passed exception class as a unary element of an exception list belonging to this method declaration (throws... clause).
      <X1 extends Exception,​X2 extends Exception>
      Conditional
      execute​(Class<X1> expectedExceptionOne, Class<X2> expectedExceptionTwo)
      Executes all submitted actions, bound to the value described by this conditional, and respectively sets the passed exception classes as elements of an exception list belonging to this method declaration (throws... clause).
      <X1 extends Exception,​X2 extends Exception,​X3 extends Exception>
      Conditional
      execute​(Class<X1> expectedExceptionOne, Class<X2> expectedExceptionTwo, Class<X3> expectedExceptionThree)
      Executes all submitted actions, bound to the value described by this conditional, and respectively sets the passed exception classes as elements of an exception list belonging to this method declaration (throws... clause).
      <X1 extends Exception,​X2 extends Exception,​X3 extends Exception,​X4 extends Exception>
      Conditional
      execute​(Class<X1> expectedExceptionOne, Class<X2> expectedExceptionTwo, Class<X3> expectedExceptionThree, Class<X4> expectedExceptionFour)
      Executes all submitted actions, bound to the value described by this conditional, and respectively sets the passed exception classes as elements of an exception list belonging to this method declaration (throws... clause).
      <T> T get​(Class<T> typeToGet)
      Executes a unary action submitted to this conditional and bound to the value described by this conditional; after that, returns a return value that is produced in the result of execution of the action, but cast into a specified type.
      <T,​X extends Exception>
      T
      get​(Class<T> typeToGet, Class<X> expectedException)
      Executes a unary action submitted to this conditional and bound to the value described by this conditional; after that, returns a return value that is produced in the result of execution of the action, but cast into a specified type.
      <T,​X1 extends Exception,​X2 extends Exception>
      T
      get​(Class<T> typeToGet, Class<X1> expectedExceptionOne, Class<X2> expectedExceptionTwo)
      Executes a unary action submitted to this conditional and bound to the value described by this conditional; after that, returns a return value that is produced in the result of execution of the action, but cast into a specified type.
      <T,​X1 extends Exception,​X2 extends Exception,​X3 extends Exception>
      T
      get​(Class<T> typeToGet, Class<X1> expectedExceptionOne, Class<X2> expectedExceptionTwo, Class<X3> expectedExceptionThree)
      Executes a unary action submitted to this conditional and bound to the value described by this conditional; after that, returns a return value that is produced in the result of execution of the action, but cast into a specified type.
      <T,​X1 extends Exception,​X2 extends Exception,​X3 extends Exception,​X4 extends Exception>
      T
      get​(Class<T> typeToGet, Class<X1> expectedExceptionOne, Class<X2> expectedExceptionTwo, Class<X3> expectedExceptionThree, Class<X4> expectedExceptionFour)
      Executes a unary action submitted to this conditional and bound to the value described by this conditional; after that, returns a return value that is produced in the result of execution of the action, but cast into a specified type.
      boolean isFalse()
      Answers whether this conditional describes a false value.
      static <T extends Exception>
      void
      isFalseOrThrow​(boolean conditionThatMustBeFalse, T exceptionToThrow)
      Assures that the passed boolean value is false.
      boolean isTrue()
      Answers whether this conditional describes a true value.
      static <T extends Exception>
      void
      isTrueOrThrow​(boolean conditionThatMustBeTrue, T exceptionToThrow)
      Assures that the passed boolean value is true.
      <T> Conditional onFalse​(Action<T> actionOnFalse)
      Submits an action to this conditional and bounds it to a false value.
      Conditional onFalse​(Runnable actionOnFalse)
      Submits an action to this conditional and bounds it to a false value.
      <T> Conditional onFalse​(Callable<T> actionOnFalse)
      Submits an action to this conditional and bounds it to a false value.
      static void onFalseExecute​(boolean conditionThatMustBeFalse, Runnable actionToExecute)
      Executes the submitted action if the passed boolean value is false.
      static <X extends Exception>
      void
      onFalseExecute​(boolean conditionThatMustBeFalse, Runnable actionToExecute, Class<X> expectedException)
      Executes the submitted action if the passed boolean value is false.
      <T extends Exception>
      Conditional
      onFalseThrow​(T exceptionToThrow)
      Submits an action to this conditional that throws the passed Exception.
      <T> Conditional onTrue​(Action<T> actionOnTrue)
      Submits an action to this conditional and bounds it to a true value.
      Conditional onTrue​(Runnable actionOnTrue)
      Submits an action to this conditional and bounds it to a true value.
      <T> Conditional onTrue​(Callable<T> actionOnTrue)
      Submits an action to this conditional and bounds it to a true value.
      static void onTrueExecute​(boolean conditionThatMustBeTrue, Runnable actionToExecute)
      Executes the submitted action if the passed boolean value is true.
      static <X extends Exception>
      void
      onTrueExecute​(boolean conditionThatMustBeTrue, Runnable actionToExecute, Class<X> expectedException)
      Executes the submitted action if the passed boolean value is true.
      <T extends Exception>
      Conditional
      onTrueThrow​(T exceptionToThrow)
      Submits an action to this conditional that throws the passed Exception.
    • Method Detail

      • conditional

        @Nonnull
        public static Conditional conditional​(boolean describedValue)
        Returns a new instance of a Conditional that describes the passed boolean value (true or false). That value is final and cannot be changed in conventional way.
        Parameters:
        describedValue - value that will be described by the created conditional
        Returns:
        new instance of a conditional that describes the passed boolean value
      • describedValue

        public boolean describedValue()
        Returns the value described by this conditional (true or false).
        Returns:
        value described by this conditional (true or false)
      • isTrue

        public boolean isTrue()
        Answers whether this conditional describes a true value.
        Returns:
        true if this conditional describes a true value; false otherwise
      • isFalse

        public boolean isFalse()
        Answers whether this conditional describes a false value.
        Returns:
        true if this conditional describes a false value; false otherwise
      • actionsOnTrue

        @Nonnull
        public ActionsList actionsOnTrue()
        Retrieves by reference an ActionsList that stores all actions submitted to this conditional and bound to a true value.
        Returns:
        ActionsList (by reference) that stores all actions submitted to this conditional and bound to a true value
      • actionsOnFalse

        @Nonnull
        public ActionsList actionsOnFalse()
        Retrieves by reference an ActionsList that stores all actions submitted to this conditional and bound to a false value.
        Returns:
        ActionsList (by reference) that stores all actions submitted to this conditional and bound to a false value
      • onTrue

        @Nonnull
        public <T> Conditional onTrue​(@Nonnull
                                      Callable<T> actionOnTrue)
        Submits an action to this conditional and bounds it to a true value.

        The submitted Callable is wrapped into an instance of an Action via an Action(Callable) constructor and is managed via API of that Action. See documentation for Action for details.

        Type Parameters:
        T - type of value returned in the result of submitted action execution
        Parameters:
        actionOnTrue - action that should be submitted to this conditional and bound to a true value
        Returns:
        this conditional after submitting an action
      • onTrue

        @Nonnull
        public Conditional onTrue​(@Nonnull
                                  Runnable actionOnTrue)
        Submits an action to this conditional and bounds it to a true value.

        The submitted Runnable is wrapped into an instance of an Action via an Action(Runnable) constructor and is managed via API of that Action. See documentation for Action for details.

        Parameters:
        actionOnTrue - action that should be submitted to this conditional and bound to a true value
        Returns:
        this conditional after submitting an action
      • onTrue

        @Nonnull
        public <T> Conditional onTrue​(@Nonnull
                                      Action<T> actionOnTrue)
        Submits an action to this conditional and bounds it to a true value.
        Type Parameters:
        T - type of value returned in the result of submitted action execution
        Parameters:
        actionOnTrue - action that should be submitted to this conditional and bound to a true value
        Returns:
        this conditional after submitting an action
      • onFalse

        @Nonnull
        public <T> Conditional onFalse​(@Nonnull
                                       Callable<T> actionOnFalse)
        Submits an action to this conditional and bounds it to a false value.

        The submitted Callable is wrapped into an instance of an Action via an Action(Callable) constructor and is managed via API of that Action. See documentation for Action for details.

        Type Parameters:
        T - type of value returned in the result of submitted action execution
        Parameters:
        actionOnFalse - action that should be submitted to this conditional and bound to a false value
        Returns:
        this conditional after submitting an action
      • onFalse

        @Nonnull
        public Conditional onFalse​(@Nonnull
                                   Runnable actionOnFalse)
        Submits an action to this conditional and bounds it to a false value.

        The submitted Runnable is wrapped into an instance of an Action via an Action(Runnable) constructor and is managed via API of that Action. See documentation for Action for details.

        Parameters:
        actionOnFalse - action that should be submitted to this conditional and bound to a false value
        Returns:
        this conditional after submitting an action
      • onFalse

        @Nonnull
        public <T> Conditional onFalse​(@Nonnull
                                       Action<T> actionOnFalse)
        Submits an action to this conditional and bounds it to a false value.
        Type Parameters:
        T - type of value returned in the result of submitted action execution
        Parameters:
        actionOnFalse - action that should be submitted to this conditional and bound to a false value
        Returns:
        this conditional after submitting an action
      • execute

        @Nonnull
        public Conditional execute()
        Executes all submitted actions, bound to the value described by this conditional.
        1. Execution is performed subsequently, starting from the first submitted action, bound to the value described by this conditional.
        2. Execution is performed via calling an Action.execute() method of every executed action. See documentation for that method for details about execution.
        3. If there are no submitted actions, bound to the value described by this conditional, then nothing happens: no action is executed, no exception is thrown.
        4. This method can be called multiple times. However, in case of calling it more than once side effects caused by a previous call are possible.
        Returns:
        this conditional after this method call
        Throws:
        Exception - if an Exception during execution of an action was thrown; note that the Exception isn't specified in a method declaration in a throws... clause in order to avoid enforcing that Exception handling (omitting of specifying the Exception in the method declaration is achieved via SneakyThrows on the underlying action)
      • execute

        @Nonnull
        public Conditional execute​(int cyclesToExecute)
        Executes the specified amount of cycles all submitted actions, bound to the value described by this conditional. For example, if sequence of relevant actions is [A -> B -> C] and the specified amount of cycles is 2, then those actions will be executed in the following order: [A -> B -> C -> A -> B -> C].

        For details on execution see documentation for execute().

        Parameters:
        cyclesToExecute - the amount of cycles that relevant actions should be executed; if value of the passed argument is 0 or less, then nothing happens: no action is executed, no exception is thrown
        Returns:
        this conditional after this method call
        Throws:
        Exception - if an Exception during execution of an action was thrown; note that the Exception isn't specified in a method declaration in a throws... clause in order to avoid enforcing that Exception handling (omitting of specifying the Exception in the method declaration is achieved via SneakyThrows on the underlying action)
      • execute

        @Nonnull
        public <X extends ExceptionConditional execute​(@Nullable
                                                         Class<X> expectedException)
                                                  throws X extends Exception
        Executes all submitted actions, bound to the value described by this conditional, and sets the passed exception class as a unary element of an exception list belonging to this method declaration (throws... clause). If the passed exception class is null, it will be ignored.
        1. The only thing this method does internally is calling an execute() method. Therefore, for details on the execution see documentation for execute().
        2. The only relevant difference between this method and an execute() method is that this method declaration has an exception list (throws... clause) with one element inside, which is the passed exception class. The reason behind such solution is that an execute() method doesn't have a throws... clause, although it is capable of throwing an Exception (the clause is omitted via SneakyThrows on the underlying action), which allows to avoid enforcing of Exception handling. In cases, where enforcing of such handling is, however, required, this method can be used.
        3. Consider the following conditional. Upon execution, it always throws an IOException, which is a subclass of an Exception:
          
               Conditional trueConditional = conditional(true)
                       .onTrue(() -> {
                           throw new IOException();
                       });
               
          However, the first execution method below will not enforce any Exception handling, while the second one - will:
          
               trueConditional.execute(); // Doesn't enforce exception handling
          
               try {
                   trueConditional.execute(IOException.class); // Does enforce exception handling
               } catch (IOException exception) {
                   log.error("Unable to read a file", exception);
               }
        Type Parameters:
        X - an exception class that will be set as a unary element of an exception list belonging to this method declaration, i.e. as a unary element of the throws... clause
        Parameters:
        expectedException - class representing an exception class that will be set as a unary element of an exception list belonging to this method declaration, i.e. as a unary element of the throws... clause
        Returns:
        this conditional after this method call
        Throws:
        X - if an Exception of X type during execution of an action was thrown
        X extends Exception
      • execute

        @Nonnull
        public <X1 extends Exception,​X2 extends ExceptionConditional execute​(@Nullable
                                                                                     Class<X1> expectedExceptionOne,
                                                                                     @Nullable
                                                                                     Class<X2> expectedExceptionTwo)
                                                                              throws X1 extends Exception,
                                                                                     X2 extends Exception
        Executes all submitted actions, bound to the value described by this conditional, and respectively sets the passed exception classes as elements of an exception list belonging to this method declaration (throws... clause). If the passed exception class is null, it will be ignored.
        1. The only thing this method does internally is calling an execute() method. Therefore, for details on the execution see documentation for execute().
        2. The only relevant difference between this method and an execute() method is that this method declaration has an exception list (throws... clause), which is respectively defined by the passed exception classes. The reason behind such solution is that an execute() method doesn't have a throws... clause, although it is capable of throwing an Exception (the clause is omitted via SneakyThrows on the underlying action), which allows to avoid enforcing of Exception handling. In cases, where enforcing of such handling is, however, required, this method can be used.
        3. Consider the following conditional. Upon execution, it always throws an IOException, which is a subclass of an Exception:
          
               Conditional trueConditional = conditional(true)
                       .onTrue(() -> {
                           throw new IOException();
                       });
               
          However, the first execution method below will not enforce any Exception handling, while the second one - will:
          
               trueConditional.execute(); // Doesn't enforce exception handling
          
               try {
                   trueConditional.execute(IOException.class); // Does enforce exception handling
               } catch (IOException exception) {
                   log.error("Unable to read a file", exception);
               }
        Type Parameters:
        X1 - an exception class that will be set as a first element of an exception list belonging to this method declaration, i.e. as a first element of the throws... clause
        X2 - an exception class that will be set as a second element of an exception list belonging to this method declaration, i.e. as a second element of the throws... clause
        Parameters:
        expectedExceptionOne - class representing an exception class that will be set as a first element of an exception list belonging to this method declaration, i.e. as a first element of the throws... clause
        expectedExceptionTwo - class representing an exception class that will be set as a second element of an exception list belonging to this method declaration, i.e. as a second element of the throws... clause
        Returns:
        this conditional after this method call
        Throws:
        X1 - if an Exception of X1 type during execution of an action was thrown
        X2 - if an Exception of X2 type during execution of an action was thrown
        X1 extends Exception
      • execute

        @Nonnull
        public <X1 extends Exception,​X2 extends Exception,​X3 extends ExceptionConditional execute​(@Nullable
                                                                                                                Class<X1> expectedExceptionOne,
                                                                                                                @Nullable
                                                                                                                Class<X2> expectedExceptionTwo,
                                                                                                                @Nullable
                                                                                                                Class<X3> expectedExceptionThree)
                                                                                                         throws X1 extends Exception,
                                                                                                                X2 extends Exception,
                                                                                                                X3 extends Exception
        Executes all submitted actions, bound to the value described by this conditional, and respectively sets the passed exception classes as elements of an exception list belonging to this method declaration (throws... clause). If the passed exception class is null, it will be ignored.
        1. The only thing this method does internally is calling an execute() method. Therefore, for details on the execution see documentation for execute().
        2. The only relevant difference between this method and an execute() method is that this method declaration has an exception list (throws... clause), which is respectively defined by the passed exception classes. The reason behind such solution is that an execute() method doesn't have a throws... clause, although it is capable of throwing an Exception (the clause is omitted via SneakyThrows on the underlying action), which allows to avoid enforcing of Exception handling. In cases, where enforcing of such handling is, however, required, this method can be used.
        3. Consider the following conditional. Upon execution, it always throws an IOException, which is a subclass of an Exception:
          
               Conditional trueConditional = conditional(true)
                       .onTrue(() -> {
                           throw new IOException();
                       });
               
          However, the first execution method below will not enforce any Exception handling, while the second one - will:
          
               trueConditional.execute(); // Doesn't enforce exception handling
          
               try {
                   trueConditional.execute(IOException.class); // Does enforce exception handling
               } catch (IOException exception) {
                   log.error("Unable to read a file", exception);
               }
        Type Parameters:
        X1 - an exception class that will be set as a first element of an exception list belonging to this method declaration, i.e. as a first element of the throws... clause
        X2 - an exception class that will be set as a second element of an exception list belonging to this method declaration, i.e. as a second element of the throws... clause
        X3 - an exception class that will be set as a third element of an exception list belonging to this method declaration, i.e. as a third element of the throws... clause
        Parameters:
        expectedExceptionOne - class representing an exception class that will be set as a first element of an exception list belonging to this method declaration, i.e. as a first element of the throws... clause
        expectedExceptionTwo - class representing an exception class that will be set as a second element of an exception list belonging to this method declaration, i.e. as a second element of the throws... clause
        expectedExceptionThree - class representing an exception class that will be set as a third element of an exception list belonging to this method declaration, i.e. as a third element of the throws... clause
        Returns:
        this conditional after this method call
        Throws:
        X1 - if an Exception of X1 type during execution of an action was thrown
        X2 - if an Exception of X2 type during execution of an action was thrown
        X3 - if an Exception of X3 type during execution of an action was thrown
        X1 extends Exception
      • execute

        @Nonnull
        public <X1 extends Exception,​X2 extends Exception,​X3 extends Exception,​X4 extends ExceptionConditional execute​(@Nullable
                                                                                                                                           Class<X1> expectedExceptionOne,
                                                                                                                                           @Nullable
                                                                                                                                           Class<X2> expectedExceptionTwo,
                                                                                                                                           @Nullable
                                                                                                                                           Class<X3> expectedExceptionThree,
                                                                                                                                           @Nullable
                                                                                                                                           Class<X4> expectedExceptionFour)
                                                                                                                                    throws X1 extends Exception,
                                                                                                                                           X2 extends Exception,
                                                                                                                                           X3 extends Exception,
                                                                                                                                           X4 extends Exception
        Executes all submitted actions, bound to the value described by this conditional, and respectively sets the passed exception classes as elements of an exception list belonging to this method declaration (throws... clause). If the passed exception class is null, it will be ignored.
        1. The only thing this method does internally is calling an execute() method. Therefore, for details on the execution see documentation for execute().
        2. The only relevant difference between this method and an execute() method is that this method declaration has an exception list (throws... clause), which is respectively defined by the passed exception classes. The reason behind such solution is that an execute() method doesn't have a throws... clause, although it is capable of throwing an Exception (the clause is omitted via SneakyThrows on the underlying action), which allows to avoid enforcing of Exception handling. In cases, where enforcing of such handling is, however, required, this method can be used.
        3. Consider the following conditional. Upon execution, it always throws an IOException, which is a subclass of an Exception:
          
               Conditional trueConditional = conditional(true)
                       .onTrue(() -> {
                           throw new IOException();
                       });
               
          However, the first execution method below will not enforce any Exception handling, while the second one - will:
          
               trueConditional.execute(); // Doesn't enforce exception handling
          
               try {
                   trueConditional.execute(IOException.class); // Does enforce exception handling
               } catch (IOException exception) {
                   log.error("Unable to read a file", exception);
               }
        Type Parameters:
        X1 - an exception class that will be set as a first element of an exception list belonging to this method declaration, i.e. as a first element of the throws... clause
        X2 - an exception class that will be set as a second element of an exception list belonging to this method declaration, i.e. as a second element of the throws... clause
        X3 - an exception class that will be set as a third element of an exception list belonging to this method declaration, i.e. as a third element of the throws... clause
        X4 - an exception class that will be set as a fourth element of an exception list belonging to this method declaration, i.e. as a fourth element of the throws... clause
        Parameters:
        expectedExceptionOne - class representing an exception class that will be set as a first element of an exception list belonging to this method declaration, i.e. as a first element of the throws... clause
        expectedExceptionTwo - class representing an exception class that will be set as a second element of an exception list belonging to this method declaration, i.e. as a second element of the throws... clause
        expectedExceptionThree - class representing an exception class that will be set as a third element of an exception list belonging to this method declaration, i.e. as a third element of the throws... clause
        expectedExceptionFour - class representing an exception class that will be set as a fourth element of an exception list belonging to this method declaration, i.e. as a fourth element of the throws... clause
        Returns:
        this conditional after this method call
        Throws:
        X1 - if an Exception of X1 type during execution of an action was thrown
        X2 - if an Exception of X2 type during execution of an action was thrown
        X3 - if an Exception of X3 type during execution of an action was thrown
        X4 - if an Exception of X4 type during execution of an action was thrown
        X1 extends Exception
      • onTrueExecute

        public static void onTrueExecute​(boolean conditionThatMustBeTrue,
                                         @Nonnull
                                         Runnable actionToExecute)
        Executes the submitted action if the passed boolean value is true. If the passed boolean value is false, does nothing.
        Parameters:
        conditionThatMustBeTrue - condition that must be true in order for the passed action to be executed
        actionToExecute - action to execute if the passed boolean value is true
        Throws:
        Exception - if an Exception during execution of an action was thrown; note that the Exception isn't specified in a method declaration in a throws... clause in order to avoid enforcing that Exception handling (omitting of specifying the Exception in the method declaration is achieved via SneakyThrows on the underlying action)
      • onTrueExecute

        public static <X extends Exception> void onTrueExecute​(boolean conditionThatMustBeTrue,
                                                               @Nonnull
                                                               Runnable actionToExecute,
                                                               @Nullable
                                                               Class<X> expectedException)
                                                        throws X extends Exception
        Executes the submitted action if the passed boolean value is true. If the passed boolean value is false, does nothing.
        1. The internal logic of this method is exactly the same as for an onTrueExecute(boolean, Runnable) method.
        2. The only relevant difference between this method and an onTrueExecute(boolean, Runnable) method is that this method declaration has an exception list (throws... clause) with one element inside, which is the passed exception class. The reason behind such solution is that an onTrueExecute(boolean, Runnable) method doesn't have a throws... clause, although it is capable of throwing an Exception (the clause is omitted via SneakyThrows on the underlying action), which allows to avoid enforcing of Exception handling. In cases, where enforcing of such handling is, however, required, this method can be used.
        3. Consider the following action. It always throws an IOException, which is a subclass of an Exception:
          
               Runnable action = () -> {
                   throw new IOException();
               };
               
          However, the first execution method below will not enforce any Exception handling, while the second one - will:
          
               onTrueExecute(true, action); // Doesn't enforce exception handling
          
               try {
                   onTrueExecute(true, action, IOException.class); // Does enforce exception handling
               } catch (IOException exception) {
                   log.error("Unable to read a file", exception);
               }
        Type Parameters:
        X - an exception class that will be set as a unary element of an exception list belonging to this method declaration, i.e. as a unary element of the throws... clause
        Parameters:
        conditionThatMustBeTrue - condition that must be true in order for the passed action to be executed
        actionToExecute - action to execute if the passed boolean value is true
        expectedException - class representing an exception class that will be set as a unary element of an exception list belonging to this method declaration, i.e. as a unary element of the throws... clause
        Throws:
        X - if an Exception of X type during execution of an action was thrown
        X extends Exception
      • onFalseExecute

        public static void onFalseExecute​(boolean conditionThatMustBeFalse,
                                          @Nonnull
                                          Runnable actionToExecute)
        Executes the submitted action if the passed boolean value is false. If the passed boolean value is true, does nothing.
        Parameters:
        conditionThatMustBeFalse - condition that must be false in order for the passed action to be executed
        actionToExecute - action to execute if the passed boolean value is false
        Throws:
        Exception - if an Exception during execution of an action was thrown; note that the Exception isn't specified in a method declaration in a throws... clause in order to avoid enforcing that Exception handling (omitting of specifying the Exception in the method declaration is achieved via SneakyThrows on the underlying action)
      • onFalseExecute

        public static <X extends Exception> void onFalseExecute​(boolean conditionThatMustBeFalse,
                                                                @Nonnull
                                                                Runnable actionToExecute,
                                                                @Nullable
                                                                Class<X> expectedException)
                                                         throws X extends Exception
        Executes the submitted action if the passed boolean value is false. If the passed boolean value is true, does nothing.
        1. The internal logic of this method is exactly the same as for an onFalseExecute(boolean, Runnable) method.
        2. The only relevant difference between this method and an onFalseExecute(boolean, Runnable) method is that this method declaration has an exception list (throws... clause) with one element inside, which is the passed exception class. The reason behind such solution is that an onFalseExecute(boolean, Runnable) method doesn't have a throws... clause, although it is capable of throwing an Exception (the clause is omitted via SneakyThrows on the underlying action), which allows to avoid enforcing of Exception handling. In cases, where enforcing of such handling is, however, required, this method can be used.
        3. Consider the following action. It always throws an IOException, which is a subclass of an Exception:
          
               Runnable action = () -> {
                   throw new IOException();
               };
               
          However, the first execution method below will not enforce any Exception handling, while the second one - will:
          
               onFalseExecute(false, action); // Doesn't enforce exception handling
          
               try {
                   onFalseExecute(false, action, IOException.class); // Does enforce exception handling
               } catch (IOException exception) {
                   log.error("Unable to read a file", exception);
               }
        Type Parameters:
        X - an exception class that will be set as a unary element of an exception list belonging to this method declaration, i.e. as a unary element of the throws... clause
        Parameters:
        conditionThatMustBeFalse - condition that must be false in order for the passed action to be executed
        actionToExecute - action to execute if the passed boolean value is false
        expectedException - class representing an exception class that will be set as a unary element of an exception list belonging to this method declaration, i.e. as a unary element of the throws... clause
        Throws:
        X - if an Exception of X type during execution of an action was thrown
        X extends Exception
      • get

        @Nullable
        public <T> T get​(@Nonnull
                         Class<T> typeToGet)
        Executes a unary action submitted to this conditional and bound to the value described by this conditional; after that, returns a return value that is produced in the result of execution of the action, but cast into a specified type.
        1. For successful execution of this method exactly one action should be submitted to this conditional and be bound to the value described by this conditional.
        2. For details on the action execution see documentation for execute(), respectively adjusted to the unary character of this method.
        3. This method can be called multiple times. However, in case of calling it more than once side effects caused by a previous call are possible.
        Type Parameters:
        T - type to which the return value will be cast into
        Parameters:
        typeToGet - Class representing a type (<T>) to which the return value will be cast into
        Returns:
        return value that is produced in the result of execution of a unary action submitted to this conditional and bound to the value described by this conditional; the produced value is cast into a specified type (typeToGet); if null is produced, then null is returned regardless of the passed typeToGet; if the executed action was submitted with the use of Runnable (onTrue(Runnable), onFalse(Runnable)), then null is always returned
        Throws:
        MismatchedReturnTypeException - if a return value cannot be cast into a specified type (typeToGet) due to ClassCastException
        UndeterminedReturnValueException - if not exactly one action was submitted to this conditional and was bound to the value described by this conditional
        Exception - if an Exception different from the described above during execution of an action was thrown; note that the Exception isn't specified in a method declaration in a throws... clause in order to avoid enforcing that Exception handling (omitting of specifying the Exception in the method declaration is achieved via SneakyThrows on the underlying action)
      • get

        @Nullable
        public <T,​X extends Exception> T get​(@Nonnull
                                                   Class<T> typeToGet,
                                                   @Nullable
                                                   Class<X> expectedException)
                                            throws X extends Exception
        Executes a unary action submitted to this conditional and bound to the value described by this conditional; after that, returns a return value that is produced in the result of execution of the action, but cast into a specified type.

        The passed exception class is set as a unary element of an exception list belonging to this method declaration (throws... clause). If the passed exception class is null, it will be ignored.

        1. The only thing this method does internally is calling a get(Class) method. Therefore, for details on the execution see documentation for get(Class).
        2. The only relevant difference between this method and a get(Class) method is that this method declaration has an exception list (throws... clause) with one element inside, which is the passed exception class. The reason behind such solution is that get(Class) method doesn't have a throws... clause, although it is capable of throwing an Exception (the clause is omitted via SneakyThrows on the underlying action), which allows to avoid enforcing of Exception handling. In cases, where enforcing of such handling is, however, required, this method can be used.
        3. Consider the following conditional. Upon execution, it always throws an IOException, which is a subclass of an Exception:
          
               Conditional trueConditional = conditional(true)
                       .onTrue(() -> {
                           throw new IOException();
                       });
               
          However, the first get method below will not enforce any Exception handling, while the second one - will:
          
               String resultOne = trueConditional.get(String.class); // Doesn't enforce exception handling
          
               try {
                   String resultTwo = trueConditional.get(String.class, IOException.class); // Does enforce exception handling
               } catch (IOException exception) {
                   log.error("Unable to read a file", exception);
               }
        Type Parameters:
        T - type to which the return value will be cast into
        X - an exception class that will be set as a unary element of an exception list belonging to this method declaration, i.e. as a unary element of the throws... clause
        Parameters:
        typeToGet - Class representing a type (<T>) to which the return value will be cast into
        expectedException - class representing an exception class that will be set as a unary element of an exception list belonging to this method declaration, i.e. as a unary element of the throws... clause
        Returns:
        return value that is produced in the result of execution of a unary action submitted to this conditional and bound to the value described by this conditional; the produced value is cast into a specified type (typeToGet); if null is produced, then null is returned regardless of the passed typeToGet; if the executed action was submitted with the use of Runnable (onTrue(Runnable), onFalse(Runnable)), then null is always returned
        Throws:
        X - if an Exception of X1 type during execution of an action was thrown
        X extends Exception
      • get

        @Nullable
        public <T,​X1 extends Exception,​X2 extends Exception> T get​(@Nonnull
                                                                               Class<T> typeToGet,
                                                                               @Nullable
                                                                               Class<X1> expectedExceptionOne,
                                                                               @Nullable
                                                                               Class<X2> expectedExceptionTwo)
                                                                        throws X1 extends Exception,
                                                                               X2 extends Exception
        Executes a unary action submitted to this conditional and bound to the value described by this conditional; after that, returns a return value that is produced in the result of execution of the action, but cast into a specified type.

        The passed exception classes are set as elements of an exception list belonging to this method declaration (throws... clause). If the passed exception class is null, it will be ignored.

        1. The only thing this method does internally is calling a get(Class) method. Therefore, for details on the execution see documentation for get(Class).
        2. The only relevant difference between this method and a get(Class) method is that this method declaration has an exception list (throws... clause), which is respectively defined by the passed exception classes. The reason behind such solution is that get(Class) method doesn't have a throws... clause, although it is capable of throwing an Exception (the clause is omitted via SneakyThrows on the underlying action), which allows to avoid enforcing of Exception handling. In cases, where enforcing of such handling is, however, required, this method can be used.
        3. Consider the following conditional. Upon execution, it always throws an IOException, which is a subclass of an Exception:
          
               Conditional trueConditional = conditional(true)
                       .onTrue(() -> {
                           throw new IOException();
                       });
               
          However, the first get method below will not enforce any Exception handling, while the second one - will:
          
               String resultOne = trueConditional.get(String.class); // Doesn't enforce exception handling
          
               try {
                   String resultTwo = trueConditional.get(String.class, IOException.class); // Does enforce exception handling
               } catch (IOException exception) {
                   log.error("Unable to read a file", exception);
               }
        Type Parameters:
        T - type to which the return value will be cast into
        X1 - an exception class that will be set as a first element of an exception list belonging to this method declaration, i.e. as a first element of the throws... clause
        X2 - an exception class that will be set as a second element of an exception list belonging to this method declaration, i.e. as a second element of the throws... clause
        Parameters:
        typeToGet - Class representing a type (<T>) to which the return value will be cast into
        expectedExceptionOne - class representing an exception class that will be set as a first element of an exception list belonging to this method declaration, i.e. as a first element of the throws... clause
        expectedExceptionTwo - class representing an exception class that will be set as a second element of an exception list belonging to this method declaration, i.e. as a second element of the throws... clause
        Returns:
        return value that is produced in the result of execution of a unary action submitted to this conditional and bound to the value described by this conditional; the produced value is cast into a specified type (typeToGet); if null is produced, then null is returned regardless of the passed typeToGet; if the executed action was submitted with the use of Runnable (onTrue(Runnable), onFalse(Runnable)), then null is always returned
        Throws:
        X1 - if an Exception of X1 type during execution of an action was thrown
        X2 - if an Exception of X2 type during execution of an action was thrown
        X1 extends Exception
      • get

        @Nullable
        public <T,​X1 extends Exception,​X2 extends Exception,​X3 extends Exception> T get​(@Nonnull
                                                                                                          Class<T> typeToGet,
                                                                                                          @Nullable
                                                                                                          Class<X1> expectedExceptionOne,
                                                                                                          @Nullable
                                                                                                          Class<X2> expectedExceptionTwo,
                                                                                                          @Nullable
                                                                                                          Class<X3> expectedExceptionThree)
                                                                                                   throws X1 extends Exception,
                                                                                                          X2 extends Exception,
                                                                                                          X3 extends Exception
        Executes a unary action submitted to this conditional and bound to the value described by this conditional; after that, returns a return value that is produced in the result of execution of the action, but cast into a specified type.

        The passed exception classes are set as elements of an exception list belonging to this method declaration (throws... clause). If the passed exception class is null, it will be ignored.

        1. The only thing this method does internally is calling a get(Class) method. Therefore, for details on the execution see documentation for get(Class).
        2. The only relevant difference between this method and a get(Class) method is that this method declaration has an exception list (throws... clause), which is respectively defined by the passed exception classes. The reason behind such solution is that get(Class) method doesn't have a throws... clause, although it is capable of throwing an Exception (the clause is omitted via SneakyThrows on the underlying action), which allows to avoid enforcing of Exception handling. In cases, where enforcing of such handling is, however, required, this method can be used.
        3. Consider the following conditional. Upon execution, it always throws an IOException, which is a subclass of an Exception:
          
               Conditional trueConditional = conditional(true)
                       .onTrue(() -> {
                           throw new IOException();
                       });
               
          However, the first get method below will not enforce any Exception handling, while the second one - will:
          
               String resultOne = trueConditional.get(String.class); // Doesn't enforce exception handling
          
               try {
                   String resultTwo = trueConditional.get(String.class, IOException.class); // Does enforce exception handling
               } catch (IOException exception) {
                   log.error("Unable to read a file", exception);
               }
        Type Parameters:
        T - type to which the return value will be cast into
        X1 - an exception class that will be set as a first element of an exception list belonging to this method declaration, i.e. as a first element of the throws... clause
        X2 - an exception class that will be set as a second element of an exception list belonging to this method declaration, i.e. as a second element of the throws... clause
        X3 - an exception class that will be set as a third element of an exception list belonging to this method declaration, i.e. as a third element of the throws... clause
        Parameters:
        typeToGet - Class representing a type (<T>) to which the return value will be cast into
        expectedExceptionOne - class representing an exception class that will be set as a first element of an exception list belonging to this method declaration, i.e. as a first element of the throws... clause
        expectedExceptionTwo - class representing an exception class that will be set as a second element of an exception list belonging to this method declaration, i.e. as a second element of the throws... clause
        expectedExceptionThree - class representing an exception class that will be set as a third element of an exception list belonging to this method declaration, i.e. as a third element of the throws... clause
        Returns:
        return value that is produced in the result of execution of a unary action submitted to this conditional and bound to the value described by this conditional; the produced value is cast into a specified type (typeToGet); if null is produced, then null is returned regardless of the passed typeToGet; if the executed action was submitted with the use of Runnable (onTrue(Runnable), onFalse(Runnable)), then null is always returned
        Throws:
        X1 - if an Exception of X1 type during execution of an action was thrown
        X2 - if an Exception of X2 type during execution of an action was thrown
        X3 - if an Exception of X3 type during execution of an action was thrown
        X1 extends Exception
      • get

        @Nullable
        public <T,​X1 extends Exception,​X2 extends Exception,​X3 extends Exception,​X4 extends Exception> T get​(@Nonnull
                                                                                                                                     Class<T> typeToGet,
                                                                                                                                     @Nullable
                                                                                                                                     Class<X1> expectedExceptionOne,
                                                                                                                                     @Nullable
                                                                                                                                     Class<X2> expectedExceptionTwo,
                                                                                                                                     @Nullable
                                                                                                                                     Class<X3> expectedExceptionThree,
                                                                                                                                     @Nullable
                                                                                                                                     Class<X4> expectedExceptionFour)
                                                                                                                              throws X1 extends Exception,
                                                                                                                                     X2 extends Exception,
                                                                                                                                     X3 extends Exception,
                                                                                                                                     X4 extends Exception
        Executes a unary action submitted to this conditional and bound to the value described by this conditional; after that, returns a return value that is produced in the result of execution of the action, but cast into a specified type.

        The passed exception classes are set as elements of an exception list belonging to this method declaration (throws... clause). If the passed exception class is null, it will be ignored.

        1. The only thing this method does internally is calling a get(Class) method. Therefore, for details on the execution see documentation for get(Class).
        2. The only relevant difference between this method and a get(Class) method is that this method declaration has an exception list (throws... clause), which is respectively defined by the passed exception classes. The reason behind such solution is that get(Class) method doesn't have a throws... clause, although it is capable of throwing an Exception (the clause is omitted via SneakyThrows on the underlying action), which allows to avoid enforcing of Exception handling. In cases, where enforcing of such handling is, however, required, this method can be used.
        3. Consider the following conditional. Upon execution, it always throws an IOException, which is a subclass of an Exception:
          
               Conditional trueConditional = conditional(true)
                       .onTrue(() -> {
                           throw new IOException();
                       });
               
          However, the first get method below will not enforce any Exception handling, while the second one - will:
          
               String resultOne = trueConditional.get(String.class); // Doesn't enforce exception handling
          
               try {
                   String resultTwo = trueConditional.get(String.class, IOException.class); // Does enforce exception handling
               } catch (IOException exception) {
                   log.error("Unable to read a file", exception);
               }
        Type Parameters:
        T - type to which the return value will be cast into
        X1 - an exception class that will be set as a first element of an exception list belonging to this method declaration, i.e. as a first element of the throws... clause
        X2 - an exception class that will be set as a second element of an exception list belonging to this method declaration, i.e. as a second element of the throws... clause
        X3 - an exception class that will be set as a third element of an exception list belonging to this method declaration, i.e. as a third element of the throws... clause
        X4 - an exception class that will be set as a fourth element of an exception list belonging to this method declaration, i.e. as a fourth element of the throws... clause
        Parameters:
        typeToGet - Class representing a type (<T>) to which the return value will be cast into
        expectedExceptionOne - class representing an exception class that will be set as a first element of an exception list belonging to this method declaration, i.e. as a first element of the throws... clause
        expectedExceptionTwo - class representing an exception class that will be set as a second element of an exception list belonging to this method declaration, i.e. as a second element of the throws... clause
        expectedExceptionThree - class representing an exception class that will be set as a third element of an exception list belonging to this method declaration, i.e. as a third element of the throws... clause
        expectedExceptionFour - class representing an exception class that will be set as a fourth element of an exception list belonging to this method declaration, i.e. as a fourth element of the throws... clause
        Returns:
        return value that is produced in the result of execution of a unary action submitted to this conditional and bound to the value described by this conditional; the produced value is cast into a specified type (typeToGet); if null is produced, then null is returned regardless of the passed typeToGet; if the executed action was submitted with the use of Runnable (onTrue(Runnable), onFalse(Runnable)), then null is always returned
        Throws:
        X1 - if an Exception of X1 type during execution of an action was thrown
        X2 - if an Exception of X2 type during execution of an action was thrown
        X3 - if an Exception of X3 type during execution of an action was thrown
        X4 - if an Exception of X4 type during execution of an action was thrown
        X1 extends Exception
      • discardAllActions

        @Nonnull
        public Conditional discardAllActions()
        Removes all actions submitted to this conditional. This conditional will not contain any actions after this method returns.
        Returns:
        this conditional after this method call
      • discardActionsOnTrue

        @Nonnull
        public Conditional discardActionsOnTrue()
        Removes all actions submitted to this conditional and bound to a true value. This conditional will not contain any actions bound to a true value after this method returns.
        Returns:
        this conditional after this method call
      • discardActionsOnFalse

        @Nonnull
        public Conditional discardActionsOnFalse()
        Removes all actions submitted to this conditional and bound to a false value. This conditional will not contain any actions bound to a false value after this method returns.
        Returns:
        this conditional after this method call
      • onTrueThrow

        @Nonnull
        public <T extends ExceptionConditional onTrueThrow​(@Nonnull
                                                             T exceptionToThrow)
        Submits an action to this conditional that throws the passed Exception. The action is bound to a true value.

        The submitted action isn't prioritized in relation to other actions, particularly in relation to previously submitted actions. It means that regardless of an action submitted via this method, actions execution order remains usual as described in documentation for execute(): execution is performed subsequently, starting from the first submitted action, bound to the value described by this conditional.

        Type Parameters:
        T - type of the passed exception
        Parameters:
        exceptionToThrow - exception that is thrown if a submitted action is executed
        Returns:
        this conditional after submitting an action
      • onFalseThrow

        @Nonnull
        public <T extends ExceptionConditional onFalseThrow​(@Nonnull
                                                              T exceptionToThrow)
        Submits an action to this conditional that throws the passed Exception. The action is bound to a false value.

        The submitted action isn't prioritized in relation to other actions, particularly in relation to previously submitted actions. It means that regardless of an action submitted via this method, actions execution order remains usual as described in documentation for execute(): execution is performed subsequently, starting from the first submitted action, bound to the value described by this conditional.

        Type Parameters:
        T - type of the passed exception
        Parameters:
        exceptionToThrow - exception that is thrown if a submitted action is executed
        Returns:
        this conditional after submitting an action
      • isTrueOrThrow

        public static <T extends Exception> void isTrueOrThrow​(boolean conditionThatMustBeTrue,
                                                               @Nonnull
                                                               T exceptionToThrow)
                                                        throws T extends Exception
        Assures that the passed boolean value is true.

        Throws the passed Exception if the passed boolean value is false. Does nothing if the passed boolean value is true.

        Type Parameters:
        T - type of the passed exception
        Parameters:
        conditionThatMustBeTrue - boolean value that is supposed to be true
        exceptionToThrow - if the passed boolean value is false
        Throws:
        T - if the passed boolean value is false
        T extends Exception
      • isFalseOrThrow

        public static <T extends Exception> void isFalseOrThrow​(boolean conditionThatMustBeFalse,
                                                                @Nonnull
                                                                T exceptionToThrow)
                                                         throws T extends Exception
        Assures that the passed boolean value is false.

        Throws the passed Exception if the passed boolean value is true. Does nothing if the passed boolean value is false.

        Type Parameters:
        T - type of the passed exception
        Parameters:
        conditionThatMustBeFalse - boolean value that is supposed to be false
        exceptionToThrow - if the passed boolean value is true
        Throws:
        T - if the passed boolean value is true
        T extends Exception