Package javascalautils.concurrent
Class FutureCompanion
- java.lang.Object
-
- javascalautils.concurrent.FutureCompanion
-
public final class FutureCompanion extends java.lang.Object
Acts as a Scala type companion object forFuture
.
The primary purpose is to get the Scala feel of instantiating classes.
In Scala you can define a companion object for a class, acting as a static reference/singleton for that class allowing you do define factory methods.
One use case is to define methods with the same name as the class and let these methods invoke the constructor thus creating a nice way to create instances without using the word "new".
This can be achieved in java by statically importing a method and then using it.
The limitation is that classes may not have method with the same name as the class itself hence new companion classes have to be created.
To be able to use it in a neat concise way one needs to statically import the method.import static javascalautils.FutureCompanion.Future; Future<Integer> resultSuccess = Future(() -> 9 / 3); // The Future will at some point contain: Success(3) Future<Integer> resultFailure = Future(() -> 9 / 0); // The Future will at some point contain: Failure(ArithmeticException) //code that performs a side effect but has not return type can be written like this Future<Unit> f = Future(() -> { database.delete(someId); });
- Since:
- 1.3
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> Future<T>
Future(java.util.concurrent.Future<T> future)
Allows for converting a blocking JDKFuture
into a non-blocking future.static <T> Future<T>
Future(java.util.concurrent.Future<T> future, Executor executor)
Allows for converting a blocking JDKFuture
into a non-blocking future.static <T> Future<T>
Future(ThrowableFunction0<T> function)
Allows for easy creation of asynchronous computations that will be executed in the future.static <T> Future<T>
Future(ThrowableFunction0<T> function, Executor executor)
Allows for easy creation of asynchronous computations that will be executed in the future.static Future<Unit>
Future(VoidFunction0 function)
Allows for easy creation of asynchronous computations that will be executed in the future.static Future<Unit>
Future(VoidFunction0 function, Executor executor)
Allows for easy creation of asynchronous computations that will be executed in the future.
-
-
-
Method Detail
-
Future
public static Future<Unit> Future(VoidFunction0 function)
Allows for easy creation of asynchronous computations that will be executed in the future.
The purpose is to allow the user to invoke a side-effecting function that may succeed/fail but has now return type.
E.g. deleting something from a database may not have an interesting return type. One is only interested of the outcome,Success
/Failure
.
The method relays the execution toFuture.apply(ThrowableFunction0)
.
Best used in conjunction with statically importing this method.import static javascalautils.concurrent.FutureCompanion.Future; Future<Unit> t = Future(() -> { database.delete(someId); });
- Parameters:
function
- The function to render either the value T or raise an exception.- Returns:
- The future that will hold either
Unit
or an exception. - Since:
- 1.9
- See Also:
Future.apply(ThrowableFunction0)
-
Future
public static Future<Unit> Future(VoidFunction0 function, Executor executor)
Allows for easy creation of asynchronous computations that will be executed in the future.
The purpose is to allow the user to invoke a side-effecting function that may succeed/fail but has now return type.
E.g. deleting something from a database may not have an interesting return type. One is only interested of the outcome,Success
/Failure
.
The method relays the execution toFuture.apply(ThrowableFunction0)
.
Best used in conjunction with statically importing this method.import static javascalautils.concurrent.FutureCompanion.Future; Future<Unit> t = Future(() -> { database.delete(someId); }, someExecutor);
- Parameters:
function
- The function to render either the value T or raise an exception.executor
- The executor to use to compute/execute the Future holding the provided function- Returns:
- The future that will hold either
Unit
or an exception. - Since:
- 1.9
- See Also:
Future.apply(ThrowableFunction0)
-
Future
public static <T> Future<T> Future(ThrowableFunction0<T> function)
Allows for easy creation of asynchronous computations that will be executed in the future.
The method relays the execution toFuture.apply(ThrowableFunction0)
.
Best used in conjunction with statically importing this method.import static javascalautils.concurrent.FutureCompanion.Future; Future<Integer> resultSuccess = Future(() -> 9 / 3); // The Future will at some point contain: Success(3) Future<Integer> resultFailure = Future(() -> 9 / 0); // The Future will at some point contain: Failure(ArithmeticException)
- Type Parameters:
T
- The type for the Future- Parameters:
function
- The function to render either the value T or raise an exception.- Returns:
- The future that will hold the result provided by the function
- Since:
- 1.3
- See Also:
Future.apply(ThrowableFunction0)
-
Future
public static <T> Future<T> Future(ThrowableFunction0<T> function, Executor executor)
Allows for easy creation of asynchronous computations that will be executed in the future.
The method relays the execution toFuture.apply(ThrowableFunction0)
.
Best used in conjunction with statically importing this method.import static javascalautils.concurrent.FutureCompanion.Future; Future<Integer> resultSuccess = Future(() -> 9 / 3, someExecutor); // The Future will at some point contain: Success(3) Future<Integer> resultFailure = Future(() -> 9 / 0, someExecutor); // The Future will at some point contain: Failure(ArithmeticException)
- Type Parameters:
T
- The type for the Future- Parameters:
function
- The function to render either the value T or raise an exception.executor
- The executor to use to compute/execute the Future holding the provided function- Returns:
- The future that will hold the result provided by the function
- Since:
- 1.4
- See Also:
Future.apply(ThrowableFunction0, Executor)
-
Future
public static <T> Future<T> Future(java.util.concurrent.Future<T> future)
Allows for converting a blocking JDKFuture
into a non-blocking future.
This will use the default executor to wait/block on the provided future.- Type Parameters:
T
- The type for the Future- Parameters:
future
- The blocking future- Returns:
- The future that will hold the result provided by the future
- Since:
- 1.5
-
Future
public static <T> Future<T> Future(java.util.concurrent.Future<T> future, Executor executor)
Allows for converting a blocking JDKFuture
into a non-blocking future.- Type Parameters:
T
- The type for the Future- Parameters:
future
- The blocking futureexecutor
- The executor to use to compute/execute the Future holding the provided function- Returns:
- The future that will hold the result provided by the future
- Since:
- 1.5
-
-