summaryrefslogtreecommitdiff
path: root/base/src/main/java/bjc/utils/funcutils/Callables.java
diff options
context:
space:
mode:
authorBen Culkin <scorpress@gmail.com>2020-12-01 20:19:34 -0500
committerBen Culkin <scorpress@gmail.com>2020-12-01 20:19:34 -0500
commitfefd6eb2917b9a0856c247353545cc13876b6eda (patch)
tree374e2d31426accd3f00c6bbbff2ff75123d92da2 /base/src/main/java/bjc/utils/funcutils/Callables.java
parent03de62016afa4e392f32069ec28ad58ee38699da (diff)
An assortment of changes/new things
Diffstat (limited to 'base/src/main/java/bjc/utils/funcutils/Callables.java')
-rw-r--r--base/src/main/java/bjc/utils/funcutils/Callables.java60
1 files changed, 60 insertions, 0 deletions
diff --git a/base/src/main/java/bjc/utils/funcutils/Callables.java b/base/src/main/java/bjc/utils/funcutils/Callables.java
new file mode 100644
index 0000000..5895347
--- /dev/null
+++ b/base/src/main/java/bjc/utils/funcutils/Callables.java
@@ -0,0 +1,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();
+ }
+}