summaryrefslogtreecommitdiff
path: root/base/src/main/java/bjc/utils/funcutils/Callables.java
blob: 58953474aaafc74264b810e5b908f332080cebc0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package bjc.utils.funcutils;

import java.util.concurrent.*;
import java.util.function.*;

/**
 * Utility function for dealing with callables and other things.
 * 
 * @author Ben Culkin
 *
 */
public class Callables
{
	/**
	 * Perform a 'bind' that appends a function to a callable.
	 * 
	 * @param <Input> The type originally returned by the callable.
	 * @param <Output> The type returned by the function.
	 * 
	 * @param call The original callable.
	 * @param func The function to use to transform the result.
	 * 
	 * @return A callable which applies the given function to the result of them.
	 */
	public static <Input, Output> Callable<Output> bind(
			Callable<Input> call, Function<Input, Callable<Output>> func)
	{
		return () -> func.apply(call.call()).call();
	}
	
	/**
	 * Convert a normal function to a function on callables.
	 * 
	 * @param <Input> The input to the function.
	 * @param <Output> The output from the function.
	 * 
	 * @param func The function to convert.
	 * 
	 * @return The function, made to work over callables.
	 */
	public static <Input, Output> Function<Callable<Input>, Callable<Output>>
	fmap(Function<Input, Output> func)
	{
		return (inp) -> () -> func.apply(inp.call());
	}
	
	/**
	 * Convert a future into a callable.
	 * 
	 * @param <Output> The type returned by the future.
	 * 
	 * @param fut The future to convert.
	 * 
	 * @return A future which yields that value.
	 */
	public static <Output> Callable<Output> obtain(Future<Output> fut)
	{
		return () -> fut.get();
	}
}