From 87ae1dfc8d8cb7b51d7bda4750ce841bbe691cfc Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Sat, 7 May 2016 12:51:23 -0400 Subject: General changes --- .../src/main/java/bjc/utils/data/IHolder.java | 82 ++++++++++++++++++---- 1 file changed, 68 insertions(+), 14 deletions(-) (limited to 'BJC-Utils2/src/main/java/bjc/utils/data/IHolder.java') 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 * The type of value held */ -public interface IHolder { +public interface IHolder extends Functor { /** * Bind a function across the value in this container * @@ -22,8 +25,8 @@ public interface IHolder { * The function to bind to the value * @return A holder from binding the value */ - public IHolder bind( - Function> binder); + public IHolder + bind(Function> binder); /** * Apply an action to the value @@ -31,7 +34,7 @@ public interface IHolder { * @param action * The action to apply to the value */ - public default void doWith(Consumer action) { + public default void doWith(Consumer action) { transform((value) -> { action.accept(value); @@ -39,13 +42,64 @@ public interface IHolder { }); } + @Override + default + Function, Functor> + fmap(Function func) { + return (argumentFunctor) -> { + if (!(argumentFunctor instanceof IHolder)) { + throw new IllegalArgumentException( + "This functor only supports mapping over instances of IHolder"); + } + + IHolder holder = (IHolder) 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 + * 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 Function> + lift(Function func); + + /** + * Make this holder lazy + * + * @return A lazy version of this holder + */ + public default IHolder makeLazy() { + return new WrappedLazy<>(this); + } + + /** + * Make this holder a list + * + * @return A list version of this holder + */ + public default IHolder makeList() { + return new BoundListHolder<>(new FunctionalList<>(this)); + } + + /** + * Make this holder optional + * + * @return An optional version of this holder + */ + public default IHolder makeOptional() { + return new WrappedOption<>(this); } /** @@ -60,8 +114,8 @@ public interface IHolder { * The function to do mapping with * @return A holder with the mapped value */ - public IHolder map( - Function mapper); + public IHolder + map(Function mapper); /** * Replace the held value with a new one @@ -83,8 +137,8 @@ public interface IHolder { * The function to transform the value with * @return The holder itself, for easy chaining */ - public IHolder transform( - UnaryOperator transformer); + public IHolder + transform(UnaryOperator transformer); /** * Unwrap the value contained in this holder so that it is no longer @@ -96,6 +150,6 @@ public interface IHolder { * The function to use to unwrap the value * @return The unwrapped held value */ - public UnwrappedType unwrap( - Function unwrapper); + public UnwrappedType + unwrap(Function unwrapper); } -- cgit v1.2.3