Class Conditional
- java.lang.Object
-
- eu.ciechanowiec.conditional.Conditional
-
public final class Conditional extends Object
Basic entity of Conditional library, that replacesif-elsestatements and ternary operators with a featured fluent interface.Conditionalcan be instantiated via aconditional(boolean)method. That method accepts a boolean argument (true/false), which becomes an immutable value described by a constructedConditional. Described value determines behavior of a given instance ofConditional.- To a given instance of
Conditionalmultiple actions can be submitted in a fluent manner. - Every submitted action is bound either to a
trueorfalseboolean value, depending on the used submission method. - 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
Conditionalis finalized via anexecute(...)orget(...)methods respectively, that trigger actions bound to a value described by a given instance ofConditional. Conditionalis lazy, which means that respective submitted actions will be triggered when and only when anexecute(...)orget(...)methods are called, hence mere submission of an action doesn't suffice to trigger that action.
Usage example with action returning a value: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 }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 ActionsListactionsOnFalse()Retrieves by reference anActionsListthat stores all actions submitted to this conditional and bound to afalsevalue.ActionsListactionsOnTrue()Retrieves by reference anActionsListthat stores all actions submitted to this conditional and bound to atruevalue.static Conditionalconditional(boolean describedValue)booleandescribedValue()Returns the value described by this conditional (trueorfalse).ConditionaldiscardActionsOnFalse()Removes all actions submitted to this conditional and bound to afalsevalue.ConditionaldiscardActionsOnTrue()Removes all actions submitted to this conditional and bound to atruevalue.ConditionaldiscardAllActions()Removes all actions submitted to this conditional.Conditionalexecute()Executes all submitted actions, bound to the value described by this conditional.Conditionalexecute(int cyclesToExecute)Executes the specified amount of cycles all submitted actions, bound to the value described by this conditional.<X extends Exception>
Conditionalexecute(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>
Conditionalexecute(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>
Conditionalexecute(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>
Conditionalexecute(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> Tget(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>
Tget(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>
Tget(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>
Tget(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>
Tget(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.booleanisFalse()Answers whether this conditional describes afalsevalue.static <T extends Exception>
voidisFalseOrThrow(boolean conditionThatMustBeFalse, T exceptionToThrow)Assures that the passed boolean value isfalse.booleanisTrue()Answers whether this conditional describes atruevalue.static <T extends Exception>
voidisTrueOrThrow(boolean conditionThatMustBeTrue, T exceptionToThrow)Assures that the passed boolean value istrue.<T> ConditionalonFalse(Action<T> actionOnFalse)Submits an action to this conditional and bounds it to afalsevalue.ConditionalonFalse(Runnable actionOnFalse)Submits an action to this conditional and bounds it to afalsevalue.<T> ConditionalonFalse(Callable<T> actionOnFalse)Submits an action to this conditional and bounds it to afalsevalue.static voidonFalseExecute(boolean conditionThatMustBeFalse, Runnable actionToExecute)Executes the submitted action if the passed boolean value isfalse.static <X extends Exception>
voidonFalseExecute(boolean conditionThatMustBeFalse, Runnable actionToExecute, Class<X> expectedException)Executes the submitted action if the passed boolean value isfalse.<T extends Exception>
ConditionalonFalseThrow(T exceptionToThrow)Submits an action to this conditional that throws the passedException.<T> ConditionalonTrue(Action<T> actionOnTrue)Submits an action to this conditional and bounds it to atruevalue.ConditionalonTrue(Runnable actionOnTrue)Submits an action to this conditional and bounds it to atruevalue.<T> ConditionalonTrue(Callable<T> actionOnTrue)Submits an action to this conditional and bounds it to atruevalue.static voidonTrueExecute(boolean conditionThatMustBeTrue, Runnable actionToExecute)Executes the submitted action if the passed boolean value istrue.static <X extends Exception>
voidonTrueExecute(boolean conditionThatMustBeTrue, Runnable actionToExecute, Class<X> expectedException)Executes the submitted action if the passed boolean value istrue.<T extends Exception>
ConditionalonTrueThrow(T exceptionToThrow)Submits an action to this conditional that throws the passedException.
-
-
-
Method Detail
-
conditional
@Nonnull public static Conditional conditional(boolean describedValue)
Returns a new instance of aConditionalthat describes the passed boolean value (trueorfalse). 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 (trueorfalse).- Returns:
- value described by this conditional (
trueorfalse)
-
isTrue
public boolean isTrue()
Answers whether this conditional describes atruevalue.- Returns:
trueif this conditional describes atruevalue;falseotherwise
-
isFalse
public boolean isFalse()
Answers whether this conditional describes afalsevalue.- Returns:
trueif this conditional describes afalsevalue;falseotherwise
-
actionsOnTrue
@Nonnull public ActionsList actionsOnTrue()
Retrieves by reference anActionsListthat stores all actions submitted to this conditional and bound to atruevalue.- Returns:
ActionsList(by reference) that stores all actions submitted to this conditional and bound to atruevalue
-
actionsOnFalse
@Nonnull public ActionsList actionsOnFalse()
Retrieves by reference anActionsListthat stores all actions submitted to this conditional and bound to afalsevalue.- Returns:
ActionsList(by reference) that stores all actions submitted to this conditional and bound to afalsevalue
-
onTrue
@Nonnull public <T> Conditional onTrue(@Nonnull Callable<T> actionOnTrue)
Submits an action to this conditional and bounds it to atruevalue.The submitted
Callableis wrapped into an instance of anActionvia anAction(Callable)constructor and is managed via API of thatAction. See documentation forActionfor 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 atruevalue- 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 atruevalue.The submitted
Runnableis wrapped into an instance of anActionvia anAction(Runnable)constructor and is managed via API of thatAction. See documentation forActionfor details.- Parameters:
actionOnTrue- action that should be submitted to this conditional and bound to atruevalue- 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 atruevalue.- 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 atruevalue- 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 afalsevalue.The submitted
Callableis wrapped into an instance of anActionvia anAction(Callable)constructor and is managed via API of thatAction. See documentation forActionfor 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 afalsevalue- 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 afalsevalue.The submitted
Runnableis wrapped into an instance of anActionvia anAction(Runnable)constructor and is managed via API of thatAction. See documentation forActionfor details.- Parameters:
actionOnFalse- action that should be submitted to this conditional and bound to afalsevalue- 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 afalsevalue.- 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 afalsevalue- Returns:
- this conditional after submitting an action
-
execute
@Nonnull public Conditional execute()
Executes all submitted actions, bound to the value described by this conditional.- Execution is performed subsequently, starting from the first submitted action, bound to the value described by this conditional.
- Execution is performed via calling an
Action.execute()method of every executed action. See documentation for that method for details about execution. - 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.
- 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 anExceptionduring execution of an action was thrown; note that theExceptionisn't specified in a method declaration in athrows...clause in order to avoid enforcing thatExceptionhandling (omitting of specifying theExceptionin the method declaration is achieved viaSneakyThrowson 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 is0or less, then nothing happens: no action is executed, no exception is thrown- Returns:
- this conditional after this method call
- Throws:
Exception- if anExceptionduring execution of an action was thrown; note that theExceptionisn't specified in a method declaration in athrows...clause in order to avoid enforcing thatExceptionhandling (omitting of specifying theExceptionin the method declaration is achieved viaSneakyThrowson the underlying action)
-
execute
@Nonnull public <X extends Exception> Conditional 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 isnull, it will be ignored.- The only thing this method does internally is calling an
execute()method. Therefore, for details on the execution see documentation forexecute(). - 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 anexecute()method doesn't have athrows...clause, although it is capable of throwing anException(the clause is omitted viaSneakyThrowson the underlying action), which allows to avoid enforcing ofExceptionhandling. In cases, where enforcing of such handling is, however, required, this method can be used. - Consider the following conditional. Upon execution, it always throws an
IOException, which is a subclass of anException:
However, the first execution method below will not enforce anyConditional trueConditional = conditional(true) .onTrue(() -> { throw new IOException(); });Exceptionhandling, 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 thethrows...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 thethrows...clause- Returns:
- this conditional after this method call
- Throws:
X- if anExceptionofXtype during execution of an action was thrownX extends Exception
- The only thing this method does internally is calling an
-
execute
@Nonnull public <X1 extends Exception,X2 extends Exception> Conditional 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 isnull, it will be ignored.- The only thing this method does internally is calling an
execute()method. Therefore, for details on the execution see documentation forexecute(). - 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 anexecute()method doesn't have athrows...clause, although it is capable of throwing anException(the clause is omitted viaSneakyThrowson the underlying action), which allows to avoid enforcing ofExceptionhandling. In cases, where enforcing of such handling is, however, required, this method can be used. - Consider the following conditional. Upon execution, it always throws an
IOException, which is a subclass of anException:
However, the first execution method below will not enforce anyConditional trueConditional = conditional(true) .onTrue(() -> { throw new IOException(); });Exceptionhandling, 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 thethrows...clauseX2- 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 thethrows...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 thethrows...clauseexpectedExceptionTwo- 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 thethrows...clause- Returns:
- this conditional after this method call
- Throws:
X1- if anExceptionofX1type during execution of an action was thrownX2- if anExceptionofX2type during execution of an action was thrownX1 extends Exception
- The only thing this method does internally is calling an
-
execute
@Nonnull public <X1 extends Exception,X2 extends Exception,X3 extends Exception> Conditional 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 isnull, it will be ignored.- The only thing this method does internally is calling an
execute()method. Therefore, for details on the execution see documentation forexecute(). - 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 anexecute()method doesn't have athrows...clause, although it is capable of throwing anException(the clause is omitted viaSneakyThrowson the underlying action), which allows to avoid enforcing ofExceptionhandling. In cases, where enforcing of such handling is, however, required, this method can be used. - Consider the following conditional. Upon execution, it always throws an
IOException, which is a subclass of anException:
However, the first execution method below will not enforce anyConditional trueConditional = conditional(true) .onTrue(() -> { throw new IOException(); });Exceptionhandling, 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 thethrows...clauseX2- 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 thethrows...clauseX3- 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 thethrows...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 thethrows...clauseexpectedExceptionTwo- 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 thethrows...clauseexpectedExceptionThree- 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 thethrows...clause- Returns:
- this conditional after this method call
- Throws:
X1- if anExceptionofX1type during execution of an action was thrownX2- if anExceptionofX2type during execution of an action was thrownX3- if anExceptionofX3type during execution of an action was thrownX1 extends Exception
- The only thing this method does internally is calling an
-
execute
@Nonnull public <X1 extends Exception,X2 extends Exception,X3 extends Exception,X4 extends Exception> Conditional 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 isnull, it will be ignored.- The only thing this method does internally is calling an
execute()method. Therefore, for details on the execution see documentation forexecute(). - 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 anexecute()method doesn't have athrows...clause, although it is capable of throwing anException(the clause is omitted viaSneakyThrowson the underlying action), which allows to avoid enforcing ofExceptionhandling. In cases, where enforcing of such handling is, however, required, this method can be used. - Consider the following conditional. Upon execution, it always throws an
IOException, which is a subclass of anException:
However, the first execution method below will not enforce anyConditional trueConditional = conditional(true) .onTrue(() -> { throw new IOException(); });Exceptionhandling, 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 thethrows...clauseX2- 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 thethrows...clauseX3- 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 thethrows...clauseX4- 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 thethrows...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 thethrows...clauseexpectedExceptionTwo- 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 thethrows...clauseexpectedExceptionThree- 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 thethrows...clauseexpectedExceptionFour- 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 thethrows...clause- Returns:
- this conditional after this method call
- Throws:
X1- if anExceptionofX1type during execution of an action was thrownX2- if anExceptionofX2type during execution of an action was thrownX3- if anExceptionofX3type during execution of an action was thrownX4- if anExceptionofX4type during execution of an action was thrownX1 extends Exception
- The only thing this method does internally is calling an
-
onTrueExecute
public static void onTrueExecute(boolean conditionThatMustBeTrue, @Nonnull Runnable actionToExecute)Executes the submitted action if the passed boolean value istrue. If the passed boolean value isfalse, does nothing.- Parameters:
conditionThatMustBeTrue- condition that must betruein order for the passed action to be executedactionToExecute- action to execute if the passed boolean value istrue- Throws:
Exception- if anExceptionduring execution of an action was thrown; note that theExceptionisn't specified in a method declaration in athrows...clause in order to avoid enforcing thatExceptionhandling (omitting of specifying theExceptionin the method declaration is achieved viaSneakyThrowson 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 istrue. If the passed boolean value isfalse, does nothing.- The internal logic of this method is exactly the same as for an
onTrueExecute(boolean, Runnable)method. - 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 anonTrueExecute(boolean, Runnable)method doesn't have athrows...clause, although it is capable of throwing anException(the clause is omitted viaSneakyThrowson the underlying action), which allows to avoid enforcing ofExceptionhandling. In cases, where enforcing of such handling is, however, required, this method can be used. - Consider the following action. It always throws an
IOException, which is a subclass of anException:
However, the first execution method below will not enforce anyRunnable action = () -> { throw new IOException(); };Exceptionhandling, 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 thethrows...clause- Parameters:
conditionThatMustBeTrue- condition that must betruein order for the passed action to be executedactionToExecute- action to execute if the passed boolean value istrueexpectedException- 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 thethrows...clause- Throws:
X- if anExceptionofXtype during execution of an action was thrownX extends Exception
- The internal logic of this method is exactly the same as for an
-
onFalseExecute
public static void onFalseExecute(boolean conditionThatMustBeFalse, @Nonnull Runnable actionToExecute)Executes the submitted action if the passed boolean value isfalse. If the passed boolean value istrue, does nothing.- Parameters:
conditionThatMustBeFalse- condition that must befalsein order for the passed action to be executedactionToExecute- action to execute if the passed boolean value isfalse- Throws:
Exception- if anExceptionduring execution of an action was thrown; note that theExceptionisn't specified in a method declaration in athrows...clause in order to avoid enforcing thatExceptionhandling (omitting of specifying theExceptionin the method declaration is achieved viaSneakyThrowson 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 isfalse. If the passed boolean value istrue, does nothing.- The internal logic of this method is exactly the same as for an
onFalseExecute(boolean, Runnable)method. - 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 anonFalseExecute(boolean, Runnable)method doesn't have athrows...clause, although it is capable of throwing anException(the clause is omitted viaSneakyThrowson the underlying action), which allows to avoid enforcing ofExceptionhandling. In cases, where enforcing of such handling is, however, required, this method can be used. - Consider the following action. It always throws an
IOException, which is a subclass of anException:
However, the first execution method below will not enforce anyRunnable action = () -> { throw new IOException(); };Exceptionhandling, 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 thethrows...clause- Parameters:
conditionThatMustBeFalse- condition that must befalsein order for the passed action to be executedactionToExecute- action to execute if the passed boolean value isfalseexpectedException- 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 thethrows...clause- Throws:
X- if anExceptionofXtype during execution of an action was thrownX extends Exception
- The internal logic of this method is exactly the same as for an
-
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.- 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.
- For details on the action execution see documentation for
execute(), respectively adjusted to the unary character of this method. - 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-Classrepresenting 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); ifnullis produced, thennullis returned regardless of the passedtypeToGet; if the executed action was submitted with the use ofRunnable(onTrue(Runnable),onFalse(Runnable)), thennullis always returned - Throws:
MismatchedReturnTypeException- if a return value cannot be cast into a specified type (typeToGet) due toClassCastExceptionUndeterminedReturnValueException- if not exactly one action was submitted to this conditional and was bound to the value described by this conditionalException- if anExceptiondifferent from the described above during execution of an action was thrown; note that theExceptionisn't specified in a method declaration in athrows...clause in order to avoid enforcing thatExceptionhandling (omitting of specifying theExceptionin the method declaration is achieved viaSneakyThrowson 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 isnull, it will be ignored.- The only thing this method does internally is calling a
get(Class)method. Therefore, for details on the execution see documentation forget(Class). - 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 thatget(Class)method doesn't have athrows...clause, although it is capable of throwing anException(the clause is omitted viaSneakyThrowson the underlying action), which allows to avoid enforcing ofExceptionhandling. In cases, where enforcing of such handling is, however, required, this method can be used. - Consider the following conditional. Upon execution, it always throws an
IOException, which is a subclass of anException:
However, the first get method below will not enforce anyConditional trueConditional = conditional(true) .onTrue(() -> { throw new IOException(); });Exceptionhandling, 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 intoX- 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 thethrows...clause- Parameters:
typeToGet-Classrepresenting a type (<T>) to which the return value will be cast intoexpectedException- 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 thethrows...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); ifnullis produced, thennullis returned regardless of the passedtypeToGet; if the executed action was submitted with the use ofRunnable(onTrue(Runnable),onFalse(Runnable)), thennullis always returned - Throws:
X- if anExceptionofX1type during execution of an action was thrownX extends Exception
- The only thing this method does internally is calling a
-
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 isnull, it will be ignored.- The only thing this method does internally is calling a
get(Class)method. Therefore, for details on the execution see documentation forget(Class). - 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 thatget(Class)method doesn't have athrows...clause, although it is capable of throwing anException(the clause is omitted viaSneakyThrowson the underlying action), which allows to avoid enforcing ofExceptionhandling. In cases, where enforcing of such handling is, however, required, this method can be used. - Consider the following conditional. Upon execution, it always throws an
IOException, which is a subclass of anException:
However, the first get method below will not enforce anyConditional trueConditional = conditional(true) .onTrue(() -> { throw new IOException(); });Exceptionhandling, 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 intoX1- 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 thethrows...clauseX2- 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 thethrows...clause- Parameters:
typeToGet-Classrepresenting a type (<T>) to which the return value will be cast intoexpectedExceptionOne- 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 thethrows...clauseexpectedExceptionTwo- 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 thethrows...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); ifnullis produced, thennullis returned regardless of the passedtypeToGet; if the executed action was submitted with the use ofRunnable(onTrue(Runnable),onFalse(Runnable)), thennullis always returned - Throws:
X1- if anExceptionofX1type during execution of an action was thrownX2- if anExceptionofX2type during execution of an action was thrownX1 extends Exception
- The only thing this method does internally is calling a
-
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 isnull, it will be ignored.- The only thing this method does internally is calling a
get(Class)method. Therefore, for details on the execution see documentation forget(Class). - 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 thatget(Class)method doesn't have athrows...clause, although it is capable of throwing anException(the clause is omitted viaSneakyThrowson the underlying action), which allows to avoid enforcing ofExceptionhandling. In cases, where enforcing of such handling is, however, required, this method can be used. - Consider the following conditional. Upon execution, it always throws an
IOException, which is a subclass of anException:
However, the first get method below will not enforce anyConditional trueConditional = conditional(true) .onTrue(() -> { throw new IOException(); });Exceptionhandling, 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 intoX1- 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 thethrows...clauseX2- 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 thethrows...clauseX3- 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 thethrows...clause- Parameters:
typeToGet-Classrepresenting a type (<T>) to which the return value will be cast intoexpectedExceptionOne- 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 thethrows...clauseexpectedExceptionTwo- 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 thethrows...clauseexpectedExceptionThree- 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 thethrows...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); ifnullis produced, thennullis returned regardless of the passedtypeToGet; if the executed action was submitted with the use ofRunnable(onTrue(Runnable),onFalse(Runnable)), thennullis always returned - Throws:
X1- if anExceptionofX1type during execution of an action was thrownX2- if anExceptionofX2type during execution of an action was thrownX3- if anExceptionofX3type during execution of an action was thrownX1 extends Exception
- The only thing this method does internally is calling a
-
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 isnull, it will be ignored.- The only thing this method does internally is calling a
get(Class)method. Therefore, for details on the execution see documentation forget(Class). - 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 thatget(Class)method doesn't have athrows...clause, although it is capable of throwing anException(the clause is omitted viaSneakyThrowson the underlying action), which allows to avoid enforcing ofExceptionhandling. In cases, where enforcing of such handling is, however, required, this method can be used. - Consider the following conditional. Upon execution, it always throws an
IOException, which is a subclass of anException:
However, the first get method below will not enforce anyConditional trueConditional = conditional(true) .onTrue(() -> { throw new IOException(); });Exceptionhandling, 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 intoX1- 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 thethrows...clauseX2- 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 thethrows...clauseX3- 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 thethrows...clauseX4- 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 thethrows...clause- Parameters:
typeToGet-Classrepresenting a type (<T>) to which the return value will be cast intoexpectedExceptionOne- 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 thethrows...clauseexpectedExceptionTwo- 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 thethrows...clauseexpectedExceptionThree- 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 thethrows...clauseexpectedExceptionFour- 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 thethrows...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); ifnullis produced, thennullis returned regardless of the passedtypeToGet; if the executed action was submitted with the use ofRunnable(onTrue(Runnable),onFalse(Runnable)), thennullis always returned - Throws:
X1- if anExceptionofX1type during execution of an action was thrownX2- if anExceptionofX2type during execution of an action was thrownX3- if anExceptionofX3type during execution of an action was thrownX4- if anExceptionofX4type during execution of an action was thrownX1 extends Exception
- The only thing this method does internally is calling a
-
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 atruevalue. This conditional will not contain any actions bound to atruevalue 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 afalsevalue. This conditional will not contain any actions bound to afalsevalue after this method returns.- Returns:
- this conditional after this method call
-
onTrueThrow
@Nonnull public <T extends Exception> Conditional onTrueThrow(@Nonnull T exceptionToThrow)
Submits an action to this conditional that throws the passedException. The action is bound to atruevalue.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 Exception> Conditional onFalseThrow(@Nonnull T exceptionToThrow)
Submits an action to this conditional that throws the passedException. The action is bound to afalsevalue.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 istrue.Throws the passed
Exceptionif the passed boolean value isfalse. Does nothing if the passed boolean value istrue.- Type Parameters:
T- type of the passed exception- Parameters:
conditionThatMustBeTrue- boolean value that is supposed to betrueexceptionToThrow- if the passed boolean value isfalse- Throws:
T- if the passed boolean value isfalseT 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 isfalse.Throws the passed
Exceptionif the passed boolean value istrue. Does nothing if the passed boolean value isfalse.- Type Parameters:
T- type of the passed exception- Parameters:
conditionThatMustBeFalse- boolean value that is supposed to befalseexceptionToThrow- if the passed boolean value istrue- Throws:
T- if the passed boolean value istrueT extends Exception
-
-