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/ListHolder.java | 79 ++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 BJC-Utils2/src/main/java/bjc/utils/data/ListHolder.java (limited to 'BJC-Utils2/src/main/java/bjc/utils/data/ListHolder.java') diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/ListHolder.java b/BJC-Utils2/src/main/java/bjc/utils/data/ListHolder.java new file mode 100644 index 0000000..8dc33d3 --- /dev/null +++ b/BJC-Utils2/src/main/java/bjc/utils/data/ListHolder.java @@ -0,0 +1,79 @@ +package bjc.utils.data; + +import java.util.function.Function; +import java.util.function.UnaryOperator; + +import bjc.utils.funcdata.FunctionalList; +import bjc.utils.funcdata.IFunctionalList; + +/** + * A holder that represents a set of non-deterministic computations + * + * @author ben + * + * @param + * The type of contained value + */ +public class ListHolder implements IHolder { + private IFunctionalList heldValues; + + private ListHolder(IFunctionalList toHold) { + heldValues = toHold; + } + + /** + * Create a new list holder + * + * @param values + * The possible values for the computation + */ + @SafeVarargs + public ListHolder(ContainedType... values) { + heldValues = new FunctionalList<>(); + + if (values != null) { + for (ContainedType containedValue : values) { + heldValues.add(containedValue); + } + } + } + + @Override + public IHolder bind( + Function> binder) { + IFunctionalList> boundValues = heldValues + .map(binder); + + return new BoundListHolder<>(boundValues); + } + + @Override + public IHolder map( + Function mapper) { + IFunctionalList mappedValues = heldValues.map(mapper); + + return new ListHolder<>(mappedValues); + } + + @Override + public IHolder transform( + UnaryOperator transformer) { + heldValues = heldValues.map(transformer); + + return this; + } + + @Override + public UnwrappedType unwrap( + Function unwrapper) { + return unwrapper.apply(heldValues.randItem()); + } + + @Override + public Function> lift( + Function func) { + return (val) -> { + return new ListHolder<>(new FunctionalList<>(func.apply(val))); + }; + } +} -- cgit v1.2.3