summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils/data/IHolder.java
diff options
context:
space:
mode:
authorbculkin2442 <bjculkin@mix.wvu.edu>2016-05-07 12:51:23 -0400
committerbculkin2442 <bjculkin@mix.wvu.edu>2016-05-07 12:51:23 -0400
commit87ae1dfc8d8cb7b51d7bda4750ce841bbe691cfc (patch)
tree290f31282898bd39300c70646c6fe2b65832886a /BJC-Utils2/src/main/java/bjc/utils/data/IHolder.java
parentfb7d03388e298258563c22abda1bd46cdaf991b7 (diff)
General changes
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/data/IHolder.java')
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/data/IHolder.java82
1 files changed, 68 insertions, 14 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/IHolder.java b/BJC-Utils2/src/main/java/bjc/utils/data/IHolder.java
index ecf3f14..58ae5db 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/data/IHolder.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/data/IHolder.java
@@ -4,6 +4,9 @@ import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.UnaryOperator;
+import bjc.utils.funcdata.FunctionalList;
+import bjc.utils.funcdata.theory.Functor;
+
/**
* A holder of a single value.
*
@@ -12,7 +15,7 @@ import java.util.function.UnaryOperator;
* @param <ContainedType>
* The type of value held
*/
-public interface IHolder<ContainedType> {
+public interface IHolder<ContainedType> extends Functor<ContainedType> {
/**
* Bind a function across the value in this container
*
@@ -22,8 +25,8 @@ public interface IHolder<ContainedType> {
* The function to bind to the value
* @return A holder from binding the value
*/
- public <BoundType> IHolder<BoundType> bind(
- Function<ContainedType, IHolder<BoundType>> binder);
+ public <BoundType> IHolder<BoundType>
+ bind(Function<ContainedType, IHolder<BoundType>> binder);
/**
* Apply an action to the value
@@ -31,7 +34,7 @@ public interface IHolder<ContainedType> {
* @param action
* The action to apply to the value
*/
- public default void doWith(Consumer<ContainedType> action) {
+ public default void doWith(Consumer<? super ContainedType> action) {
transform((value) -> {
action.accept(value);
@@ -39,13 +42,64 @@ public interface IHolder<ContainedType> {
});
}
+ @Override
+ default <ArgType, ReturnType>
+ Function<Functor<ArgType>, Functor<ReturnType>>
+ fmap(Function<ArgType, ReturnType> func) {
+ return (argumentFunctor) -> {
+ if (!(argumentFunctor instanceof IHolder<?>)) {
+ throw new IllegalArgumentException(
+ "This functor only supports mapping over instances of IHolder");
+ }
+
+ IHolder<ArgType> holder = (IHolder<ArgType>) argumentFunctor;
+
+ return holder.map(func);
+ };
+ }
+
+ @Override
+ public default ContainedType getValue() {
+ return unwrap((value) -> value);
+ }
+
/**
- * Get the value contained in this holder without changing it.
+ * Lifts a function to bind over this holder
*
- * @return The value held in this holder
+ * @param <NewType>
+ * The type of the functions return
+ * @param func
+ * The function to lift over the holder
+ * @return The function lifted over the holder
*/
- public default ContainedType getValue() {
- return unwrap((value) -> value);
+ public <NewType> Function<ContainedType, IHolder<NewType>>
+ lift(Function<ContainedType, NewType> func);
+
+ /**
+ * Make this holder lazy
+ *
+ * @return A lazy version of this holder
+ */
+ public default IHolder<ContainedType> makeLazy() {
+ return new WrappedLazy<>(this);
+ }
+
+ /**
+ * Make this holder a list
+ *
+ * @return A list version of this holder
+ */
+ public default IHolder<ContainedType> makeList() {
+ return new BoundListHolder<>(new FunctionalList<>(this));
+ }
+
+ /**
+ * Make this holder optional
+ *
+ * @return An optional version of this holder
+ */
+ public default IHolder<ContainedType> makeOptional() {
+ return new WrappedOption<>(this);
}
/**
@@ -60,8 +114,8 @@ public interface IHolder<ContainedType> {
* The function to do mapping with
* @return A holder with the mapped value
*/
- public <MappedType> IHolder<MappedType> map(
- Function<ContainedType, MappedType> mapper);
+ public <MappedType> IHolder<MappedType>
+ map(Function<ContainedType, MappedType> mapper);
/**
* Replace the held value with a new one
@@ -83,8 +137,8 @@ public interface IHolder<ContainedType> {
* The function to transform the value with
* @return The holder itself, for easy chaining
*/
- public IHolder<ContainedType> transform(
- UnaryOperator<ContainedType> transformer);
+ public IHolder<ContainedType>
+ transform(UnaryOperator<ContainedType> transformer);
/**
* Unwrap the value contained in this holder so that it is no longer
@@ -96,6 +150,6 @@ public interface IHolder<ContainedType> {
* The function to use to unwrap the value
* @return The unwrapped held value
*/
- public <UnwrappedType> UnwrappedType unwrap(
- Function<ContainedType, UnwrappedType> unwrapper);
+ public <UnwrappedType> UnwrappedType
+ unwrap(Function<ContainedType, UnwrappedType> unwrapper);
}