Class Action<T>
- java.lang.Object
-
- eu.ciechanowiec.conditional.Action<T>
-
- Type Parameters:
T- type of value returned in the result of action execution
public class Action<T> extends Object
Entity that can:- execute a predefined command in a void manner
(see
get(...)methods of this class); - execute a predefined command and return the result of that execution
(see
execute(...)methods of this class).
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidexecute()Executes this action.<X extends Exception>
voidexecute(Class<X> expectedException)Executes this action 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>
voidexecute(Class<X1> expectedExceptionOne, Class<X2> expectedExceptionTwo)Executes this action 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>
voidexecute(Class<X1> expectedExceptionOne, Class<X2> expectedExceptionTwo, Class<X3> expectedExceptionThree)Executes this action 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>
voidexecute(Class<X1> expectedExceptionOne, Class<X2> expectedExceptionTwo, Class<X3> expectedExceptionThree, Class<X4> expectedExceptionFour)Executes this action and respectively sets the passed exception classes as elements of an exception list belonging to this method declaration (throws...clause).Tget()Executes this action and returns a return value that is produced in the result of execution of this action.<S> Sget(Class<S> typeToGet)Executes this action and returns a return value that is produced in the result of execution of this action, but cast into a specified type.<S,X extends Exception>
Sget(Class<S> typeToGet, Class<X> expectedException)Executes this action and returns a return value that is produced in the result of execution of this action, but cast into a specified type.<S,X1 extends Exception,X2 extends Exception>
Sget(Class<S> typeToGet, Class<X1> expectedExceptionOne, Class<X2> expectedExceptionTwo)Executes this action and returns a return value that is produced in the result of execution of this action, but cast into a specified type.<S,X1 extends Exception,X2 extends Exception,X3 extends Exception>
Sget(Class<S> typeToGet, Class<X1> expectedExceptionOne, Class<X2> expectedExceptionTwo, Class<X3> expectedExceptionThree)Executes this action and returns a return value that is produced in the result of execution of this action, but cast into a specified type.<S,X1 extends Exception,X2 extends Exception,X3 extends Exception,X4 extends Exception>
Sget(Class<S> typeToGet, Class<X1> expectedExceptionOne, Class<X2> expectedExceptionTwo, Class<X3> expectedExceptionThree, Class<X4> expectedExceptionFour)Executes this action and returns a return value that is produced in the result of execution of this action, but cast into a specified type.
-
-
-
Constructor Detail
-
Action
public Action(@Nonnull Callable<T> engine)Constructs an action based on the passedCallable.Contrary to an action based on a passed
Runnable, this constructor constructs an action which in the result of its execution returns a value computed by the call of aCallable.call()method belonging to the passedCallable. Therefore, type parameter of an action constructed with this constructor is strongly recommended to describe the type of value that is supposed to be returned, e.g.Action<String>(see examples below).Example:
Action<String> actionOne = new Action<>(() -> "Hello, Universe!"); Action<String> actionTwo = new Action<>(() -> { System.out.println("Hello, Universe!"); return "How can I help you?"; }); int num = 10; Action<String> actionThree = new Action<>(() -> { if (num % 2 != 0) { throw new Exception("The passed number must be even"); } return "The passed number is even"; });- Parameters:
engine- entity used for method calls of a constructed action
-
Action
public Action(@Nonnull Runnable engine)Constructs an action based on the passedRunnable.Contrary to an action based on a passed
Callable, this constructor constructs an action which in the result of its execution always returnsnull. Therefore, type parameter of an action constructed with this constructor can be defined with a wildcard:Action<?>(see examples below).The passed
Runnableis wrapped into aCallable. As a result, the execution of this action is performed via nested method calls. Firstly the wrapperCallable.call()method is called that subsequently:- calls the wrapped
Runnable.run()method; - returns
null.
Example:
Action<?> actionOne = new Action<>(() -> System.out.println("Hello, Universe!")); Action<?> actionTwo = new Action<>(() -> { System.out.println("Hello, Universe!"); System.out.println("How can I help you?"); });- Parameters:
engine- entity used for method calls of a constructed action
- calls the wrapped
-
-
Method Detail
-
execute
public void execute()
Executes this action.- Execution is performed via calling a
Callable.call()method of the underlyingCallable. - If this action was constructed with the use of
Runnable, then calling aCallable.call()method of the underlyingCallablecauses the call of aRunnable.run()method belonging to theRunnablewrapped into the underlyingCallable:Action.execute() └──Callable.call() └──Runnable.run() - A return value that is produced in the result of execution of this action is ignored.
- 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.
- Throws:
Exception- if anExceptionduring execution of this 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 viaSneakyThrows)
- Execution is performed via calling a
-
execute
public <X extends Exception> void execute(@Nullable Class<X> expectedException) throws X extends Exception
Executes this action 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 viaSneakyThrows), 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 anyAction<?> action = new Action<>(() -> { throw new IOException(); });Exceptionhandling, while the second one - will:action.execute(); // Doesn't enforce exception handling try { action.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- Throws:
X- if anExceptionofXtype during execution of this action was thrownX extends Exception
- The only thing this method does internally is calling an
-
execute
public <X1 extends Exception,X2 extends Exception> void execute(@Nullable Class<X1> expectedExceptionOne, @Nullable Class<X2> expectedExceptionTwo) throws X1 extends Exception, X2 extends Exception
Executes this action 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 viaSneakyThrows), 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 anyAction<?> action = new Action<>(() -> { throw new IOException(); });Exceptionhandling, while the second one - will:action.execute(); // Doesn't enforce exception handling try { action.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- Throws:
X1- if anExceptionofX1type during execution of this action was thrownX2- if anExceptionofX2type during execution of this action was thrownX1 extends Exception
- The only thing this method does internally is calling an
-
execute
public <X1 extends Exception,X2 extends Exception,X3 extends Exception> void execute(@Nullable Class<X1> expectedExceptionOne, @Nullable Class<X2> expectedExceptionTwo, @Nullable Class<X3> expectedExceptionThree) throws X1 extends Exception, X2 extends Exception, X3 extends Exception
Executes this action 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 viaSneakyThrows), 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 anyAction<?> action = new Action<>(() -> { throw new IOException(); });Exceptionhandling, while the second one - will:action.execute(); // Doesn't enforce exception handling try { action.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- Throws:
X1- if anExceptionofX1type during execution of this action was thrownX2- if anExceptionofX2type during execution of this action was thrownX3- if anExceptionofX3type during execution of this action was thrownX1 extends Exception
- The only thing this method does internally is calling an
-
execute
public <X1 extends Exception,X2 extends Exception,X3 extends Exception,X4 extends Exception> void 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 this action 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 viaSneakyThrows), 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 anyAction<?> action = new Action<>(() -> { throw new IOException(); });Exceptionhandling, while the second one - will:action.execute(); // Doesn't enforce exception handling try { action.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- Throws:
X1- if anExceptionofX1type during execution of this action was thrownX2- if anExceptionofX2type during execution of this action was thrownX3- if anExceptionofX3type during execution of this action was thrownX4- if anExceptionofX4type during execution of this action was thrownX1 extends Exception
- The only thing this method does internally is calling an
-
get
@Nullable public T get()
Executes this action and returns a return value that is produced in the result of execution of this action.- 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.
- For details on the execution see documentation for
execute().
- Returns:
- return value that is produced in the result of execution of this action;
if
nullis produced, thennullis returned; if this action was constructed with the use ofRunnable, thennullis always returned - Throws:
Exception- if anExceptionduring execution of this 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 viaSneakyThrows)
-
get
@Nullable public <S> S get(@Nonnull Class<S> typeToGet)Executes this action and returns a return value that is produced in the result of execution of this action, but cast into a specified type.- 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.
- For details on the execution see documentation for
execute(). - Example with
String:Callable<String> callableWithString = () -> "Hello, Universe!"; Action<String> actionWithString = new Action<>(callableWithString); String returnedValue = actionWithString.get(String.class); // result: "Hello, Universe!" - Example with
null:Callable<?> callableWithNull = () -> null; Action<?> actionWithNull = new Action<>(callableWithNull); String returnedValue = actionWithNull.get(String.class); // result: null - Example with
List:List<String> listToReturn = new ArrayList<>(List.of("Hello, Universe!")); Callable<List<String>> callableWithList = () -> listToReturn; Action<List<String>> actionWithList = new Action<>(callableWithList); List<String> returnedValue = actionWithList.get(List.class); // result: listToReturn == returnedValue
- Type Parameters:
S- type to which the return value will be cast into- Parameters:
typeToGet-Classrepresenting a type (<S>) to which the return value will be cast into- Returns:
- return value that is produced in the result of execution of this action and
cast into a specified type (
typeToGet); ifnullis produced, thennullis returned regardless of the passedtypeToGet; if this action was constructed with the use ofRunnable, thennullis always returned - Throws:
MismatchedReturnTypeException- if a return value cannot be cast into a specified type (typeToGet) due toClassCastExceptionException- if anExceptionduring execution of this 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 viaSneakyThrows)
-
get
@Nullable public <S,X extends Exception> S get(@Nonnull Class<S> typeToGet, @Nullable Class<X> expectedException) throws X extends Exception
Executes this action and returns a return value that is produced in the result of execution of this 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 that aget(Class)method doesn't have athrows...clause, although it is capable of throwing anException(the clause is omitted viaSneakyThrows), 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 get method below will not enforce anyAction<?> action = new Action<>(() -> { throw new IOException(); });Exceptionhandling, while the second one - will:String resultOne = action.get(String.class); // Doesn't enforce exception handling try { String resultTwo = action.get(String.class, IOException.class); // Does enforce exception handling } catch (IOException exception) { log.error("Unable to read a file", exception); }
- Type Parameters:
S- 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 (<S>) 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 this action and
cast into a specified type (
typeToGet); ifnullis produced, thennullis returned regardless of the passedtypeToGet; if this action was constructed with the use ofRunnable, thennullis always returned - Throws:
X- if anExceptionofXtype during execution of this action was thrownX extends Exception
- The only thing this method does internally is calling a
-
get
@Nullable public <S,X1 extends Exception,X2 extends Exception> S get(@Nonnull Class<S> typeToGet, @Nullable Class<X1> expectedExceptionOne, @Nullable Class<X2> expectedExceptionTwo) throws X1 extends Exception, X2 extends Exception
Executes this action and returns a return value that is produced in the result of execution of this 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 that aget(Class)method doesn't have athrows...clause, although it is capable of throwing anException(the clause is omitted viaSneakyThrows), 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 get method below will not enforce anyAction<?> action = new Action<>(() -> { throw new IOException(); });Exceptionhandling, while the second one - will:String resultOne = action.get(String.class); // Doesn't enforce exception handling try { String resultTwo = action.get(String.class, IOException.class); // Does enforce exception handling } catch (IOException exception) { log.error("Unable to read a file", exception); }
- Type Parameters:
S- 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 (<S>) 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 this action and
cast into a specified type (
typeToGet); ifnullis produced, thennullis returned regardless of the passedtypeToGet; if this action was constructed with the use ofRunnable, thennullis always returned - Throws:
X1- if anExceptionofX1type during execution of this action was thrownX2- if anExceptionofX2type during execution of this action was thrownX1 extends Exception
- The only thing this method does internally is calling a
-
get
@Nullable public <S,X1 extends Exception,X2 extends Exception,X3 extends Exception> S get(@Nonnull Class<S> typeToGet, @Nullable Class<X1> expectedExceptionOne, @Nullable Class<X2> expectedExceptionTwo, @Nullable Class<X3> expectedExceptionThree) throws X1 extends Exception, X2 extends Exception, X3 extends Exception
Executes this action and returns a return value that is produced in the result of execution of this 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 that aget(Class)method doesn't have athrows...clause, although it is capable of throwing anException(the clause is omitted viaSneakyThrows), 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 get method below will not enforce anyAction<?> action = new Action<>(() -> { throw new IOException(); });Exceptionhandling, while the second one - will:String resultOne = action.get(String.class); // Doesn't enforce exception handling try { String resultTwo = action.get(String.class, IOException.class); // Does enforce exception handling } catch (IOException exception) { log.error("Unable to read a file", exception); }
- Type Parameters:
S- 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 (<S>) 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 this action and
cast into a specified type (
typeToGet); ifnullis produced, thennullis returned regardless of the passedtypeToGet; if this action was constructed with the use ofRunnable, thennullis always returned - Throws:
X1- if anExceptionofX1type during execution of this action was thrownX2- if anExceptionofX2type during execution of this action was thrownX3- if anExceptionofX3type during execution of this action was thrownX1 extends Exception
- The only thing this method does internally is calling a
-
get
@Nullable public <S,X1 extends Exception,X2 extends Exception,X3 extends Exception,X4 extends Exception> S get(@Nonnull Class<S> 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 this action and returns a return value that is produced in the result of execution of this 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 that aget(Class)method doesn't have athrows...clause, although it is capable of throwing anException(the clause is omitted viaSneakyThrows), 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 get method below will not enforce anyAction<?> action = new Action<>(() -> { throw new IOException(); });Exceptionhandling, while the second one - will:String resultOne = action.get(String.class); // Doesn't enforce exception handling try { String resultTwo = action.get(String.class, IOException.class); // Does enforce exception handling } catch (IOException exception) { log.error("Unable to read a file", exception); }
- Type Parameters:
S- 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 (<S>) 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 this action and
cast into a specified type (
typeToGet); ifnullis produced, thennullis returned regardless of the passedtypeToGet; if this action was constructed with the use ofRunnable, thennullis always returned - Throws:
X1- if anExceptionofX1type during execution of this action was thrownX2- if anExceptionofX2type during execution of this action was thrownX3- if anExceptionofX3type during execution of this action was thrownX4- if anExceptionofX4type during execution of this action was thrownX1 extends Exception
- The only thing this method does internally is calling a
-
-