-
public interface MemoizedConcurrentlyMarker interface for concurrently memoized functions. Usage example with good old Fibonacci sequence (doesn't actually show concurrent calls, but should be enough to illustrate the use):
public static class Fibonacci { private static final CompletableFuture<Long> ONE = CompletableFuture.completedFuture( 1L ); private final Function<Long,CompletableFuture<Long>> fibMem; public Fibonacci() { fibMem = MemoizedConcurrently.of( this::fib ); } public CompletableFuture<Long> fib( Long n ) { if( n <= 2 ) return ONE; return fibMem.apply( n - 1 ) .thenCompose( x -> fibMem.apply( n - 2 ) .thenApply( y -> x + y ) ); } } System.out.println( new Fibonacci().fib( 50L ).join() );
-
-
Method Summary
Static Methods Modifier and Type Method Description static <T,R>
java.util.function.Function<T,java.util.concurrent.CompletableFuture<R>>of(java.util.function.Function<T,java.util.concurrent.CompletableFuture<R>> function)Lift the given function to a thread-safe, concurrently memoizing version.
-
-
-
Method Detail
-
of
static <T,R> java.util.function.Function<T,java.util.concurrent.CompletableFuture<R>> of(java.util.function.Function<T,java.util.concurrent.CompletableFuture<R>> function)
Lift the given function to a thread-safe, concurrently memoizing version. The returned function computes the return value for a given argument only once. On subsequent calls given the same argument the memoized value is returned. The returned function has a few interesting properties:- The function does not permit
nullvalues. - Different threads will always wind up using the same value instances, so the function may compute values that are supposed to be singletons.
- Concurrent callers won't block each other.
- Type Parameters:
T- the type parameter of function input argumentR- the type parameter of function output argument- Parameters:
function- a possibly recursive (asynchronous) function- Returns:
- the memoizing equivalent
- The function does not permit
-
-