summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils/data
diff options
context:
space:
mode:
authorbculkin2442 <bjculkin@mix.wvu.edu>2016-05-10 16:02:45 -0400
committerbculkin2442 <bjculkin@mix.wvu.edu>2016-05-10 16:02:45 -0400
commit61fd71f69e080790da722e0e03b71ecd7c2538a2 (patch)
treee5c1150b27b84d550f807e44ac82688216451f00 /BJC-Utils2/src/main/java/bjc/utils/data
parent87ae1dfc8d8cb7b51d7bda4750ce841bbe691cfc (diff)
General update
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/data')
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/data/BoundLazy.java69
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/data/BoundListHolder.java10
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/data/Lazy.java10
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/data/ListHolder.java10
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/data/Pair.java26
5 files changed, 94 insertions, 31 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/BoundLazy.java b/BJC-Utils2/src/main/java/bjc/utils/data/BoundLazy.java
index 9ab3c05..1256e31 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/data/BoundLazy.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/data/BoundLazy.java
@@ -5,20 +5,42 @@ import java.util.function.Supplier;
import java.util.function.UnaryOperator;
import bjc.utils.funcdata.FunctionalList;
-import bjc.utils.funcdata.IFunctionalList;
+import bjc.utils.funcdata.IList;
+/*
+ * Implements a lazy holder that has been bound
+ */
class BoundLazy<OldType, BoundContainedType>
implements IHolder<BoundContainedType> {
+ /*
+ * The old value
+ */
private Supplier<IHolder<OldType>> oldSupplier;
+ /*
+ * The function to use to transform the old value into a new value
+ */
private Function<OldType, IHolder<BoundContainedType>> binder;
+ /*
+ * The bound value being held
+ */
private IHolder<BoundContainedType> boundHolder;
+ /*
+ * Whether the bound value has been actualized or not
+ */
private boolean holderBound;
- private IFunctionalList<UnaryOperator<BoundContainedType>> actions = new FunctionalList<>();
+ /*
+ * Transformations currently pending on the bound value
+ */
+ private IList<UnaryOperator<BoundContainedType>> actions =
+ new FunctionalList<>();
+ /*
+ * Create a new bound lazy value
+ */
public BoundLazy(Supplier<IHolder<OldType>> supp,
Function<OldType, IHolder<BoundContainedType>> binder) {
oldSupplier = supp;
@@ -26,19 +48,31 @@ class BoundLazy<OldType, BoundContainedType>
}
@Override
- public <BoundType> IHolder<BoundType> bind(
- Function<BoundContainedType, IHolder<BoundType>> bindr) {
- IFunctionalList<UnaryOperator<BoundContainedType>> pendingActions = new FunctionalList<>();
-
+ public <BoundType> IHolder<BoundType>
+ bind(Function<BoundContainedType, IHolder<BoundType>> bindr) {
+ /*
+ * Prepare a list of pending actions
+ */
+ IList<UnaryOperator<BoundContainedType>> pendingActions =
+ new FunctionalList<>();
actions.forEach(pendingActions::add);
+ /*
+ * Create the new supplier of a value
+ */
Supplier<IHolder<BoundContainedType>> typeSupplier = () -> {
IHolder<BoundContainedType> oldHolder = boundHolder;
+ /*
+ * Bind the value if it hasn't been bound before
+ */
if (!holderBound) {
oldHolder = oldSupplier.get().unwrap(binder);
}
+ /*
+ * Apply all the pending actions
+ */
return pendingActions.reduceAux(oldHolder, (action, state) -> {
return state.transform(action);
}, (value) -> value);
@@ -48,15 +82,18 @@ class BoundLazy<OldType, BoundContainedType>
}
@Override
- public <MappedType> IHolder<MappedType> map(
- Function<BoundContainedType, MappedType> mapper) {
- IFunctionalList<UnaryOperator<BoundContainedType>> pendingActions = new FunctionalList<>();
-
+ public <MappedType> IHolder<MappedType>
+ map(Function<BoundContainedType, MappedType> mapper) {
+ // Prepare a list of pending actions
+ IList<UnaryOperator<BoundContainedType>> pendingActions =
+ new FunctionalList<>();
actions.forEach(pendingActions::add);
+ // Prepare the new supplier
Supplier<MappedType> typeSupplier = () -> {
IHolder<BoundContainedType> oldHolder = boundHolder;
+ // Bound the value if it hasn't been bound
if (!holderBound) {
oldHolder = oldSupplier.get().unwrap(binder);
}
@@ -80,16 +117,16 @@ class BoundLazy<OldType, BoundContainedType>
}
@Override
- public IHolder<BoundContainedType> transform(
- UnaryOperator<BoundContainedType> transformer) {
+ public IHolder<BoundContainedType>
+ transform(UnaryOperator<BoundContainedType> transformer) {
actions.add(transformer);
return this;
}
@Override
- public <UnwrappedType> UnwrappedType unwrap(
- Function<BoundContainedType, UnwrappedType> unwrapper) {
+ public <UnwrappedType> UnwrappedType
+ unwrap(Function<BoundContainedType, UnwrappedType> unwrapper) {
if (!holderBound) {
boundHolder = oldSupplier.get().unwrap(binder::apply);
}
@@ -98,8 +135,8 @@ class BoundLazy<OldType, BoundContainedType>
}
@Override
- public <NewType> Function<BoundContainedType, IHolder<NewType>> lift(
- Function<BoundContainedType, NewType> func) {
+ public <NewType> Function<BoundContainedType, IHolder<NewType>>
+ lift(Function<BoundContainedType, NewType> func) {
return (val) -> {
return new Lazy<>(func.apply(val));
};
diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/BoundListHolder.java b/BJC-Utils2/src/main/java/bjc/utils/data/BoundListHolder.java
index fcb62f6..85ec8f6 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/data/BoundListHolder.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/data/BoundListHolder.java
@@ -3,20 +3,20 @@ package bjc.utils.data;
import java.util.function.Function;
import java.util.function.UnaryOperator;
-import bjc.utils.funcdata.IFunctionalList;
+import bjc.utils.funcdata.IList;
class BoundListHolder<ContainedType> implements IHolder<ContainedType> {
- private IFunctionalList<IHolder<ContainedType>> heldHolders;
+ private IList<IHolder<ContainedType>> heldHolders;
public BoundListHolder(
- IFunctionalList<IHolder<ContainedType>> toHold) {
+ IList<IHolder<ContainedType>> toHold) {
heldHolders = toHold;
}
@Override
public <BoundType> IHolder<BoundType> bind(
Function<ContainedType, IHolder<BoundType>> binder) {
- IFunctionalList<IHolder<BoundType>> boundHolders = heldHolders
+ IList<IHolder<BoundType>> boundHolders = heldHolders
.map((containedHolder) -> {
return containedHolder.bind(binder);
});
@@ -27,7 +27,7 @@ class BoundListHolder<ContainedType> implements IHolder<ContainedType> {
@Override
public <MappedType> IHolder<MappedType> map(
Function<ContainedType, MappedType> mapper) {
- IFunctionalList<IHolder<MappedType>> mappedHolders = heldHolders
+ IList<IHolder<MappedType>> mappedHolders = heldHolders
.map((containedHolder) -> {
return containedHolder.map(mapper);
});
diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/Lazy.java b/BJC-Utils2/src/main/java/bjc/utils/data/Lazy.java
index 62b0bb0..fb1a517 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/data/Lazy.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/data/Lazy.java
@@ -5,7 +5,7 @@ import java.util.function.Supplier;
import java.util.function.UnaryOperator;
import bjc.utils.funcdata.FunctionalList;
-import bjc.utils.funcdata.IFunctionalList;
+import bjc.utils.funcdata.IList;
/**
* A holder that holds a means to create a value, but doesn't actually
@@ -18,7 +18,7 @@ import bjc.utils.funcdata.IFunctionalList;
public class Lazy<ContainedType> implements IHolder<ContainedType> {
private Supplier<ContainedType> valueSupplier;
- private IFunctionalList<UnaryOperator<ContainedType>> actions = new FunctionalList<>();
+ private IList<UnaryOperator<ContainedType>> actions = new FunctionalList<>();
private boolean valueMaterialized;
@@ -49,7 +49,7 @@ public class Lazy<ContainedType> implements IHolder<ContainedType> {
}
private Lazy(Supplier<ContainedType> supp,
- IFunctionalList<UnaryOperator<ContainedType>> pendingActions) {
+ IList<UnaryOperator<ContainedType>> pendingActions) {
valueSupplier = supp;
actions = pendingActions;
@@ -58,7 +58,7 @@ public class Lazy<ContainedType> implements IHolder<ContainedType> {
@Override
public <BoundType> IHolder<BoundType> bind(
Function<ContainedType, IHolder<BoundType>> binder) {
- IFunctionalList<UnaryOperator<ContainedType>> pendingActions = new FunctionalList<>();
+ IList<UnaryOperator<ContainedType>> pendingActions = new FunctionalList<>();
actions.forEach(pendingActions::add);
@@ -78,7 +78,7 @@ public class Lazy<ContainedType> implements IHolder<ContainedType> {
@Override
public <MappedType> IHolder<MappedType> map(
Function<ContainedType, MappedType> mapper) {
- IFunctionalList<UnaryOperator<ContainedType>> pendingActions = new FunctionalList<>();
+ IList<UnaryOperator<ContainedType>> pendingActions = new FunctionalList<>();
actions.forEach(pendingActions::add);
diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/ListHolder.java b/BJC-Utils2/src/main/java/bjc/utils/data/ListHolder.java
index 8dc33d3..03765ed 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/data/ListHolder.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/data/ListHolder.java
@@ -4,7 +4,7 @@ import java.util.function.Function;
import java.util.function.UnaryOperator;
import bjc.utils.funcdata.FunctionalList;
-import bjc.utils.funcdata.IFunctionalList;
+import bjc.utils.funcdata.IList;
/**
* A holder that represents a set of non-deterministic computations
@@ -15,9 +15,9 @@ import bjc.utils.funcdata.IFunctionalList;
* The type of contained value
*/
public class ListHolder<ContainedType> implements IHolder<ContainedType> {
- private IFunctionalList<ContainedType> heldValues;
+ private IList<ContainedType> heldValues;
- private ListHolder(IFunctionalList<ContainedType> toHold) {
+ private ListHolder(IList<ContainedType> toHold) {
heldValues = toHold;
}
@@ -41,7 +41,7 @@ public class ListHolder<ContainedType> implements IHolder<ContainedType> {
@Override
public <BoundType> IHolder<BoundType> bind(
Function<ContainedType, IHolder<BoundType>> binder) {
- IFunctionalList<IHolder<BoundType>> boundValues = heldValues
+ IList<IHolder<BoundType>> boundValues = heldValues
.map(binder);
return new BoundListHolder<>(boundValues);
@@ -50,7 +50,7 @@ public class ListHolder<ContainedType> implements IHolder<ContainedType> {
@Override
public <MappedType> IHolder<MappedType> map(
Function<ContainedType, MappedType> mapper) {
- IFunctionalList<MappedType> mappedValues = heldValues.map(mapper);
+ IList<MappedType> mappedValues = heldValues.map(mapper);
return new ListHolder<>(mappedValues);
}
diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/Pair.java b/BJC-Utils2/src/main/java/bjc/utils/data/Pair.java
index 0d2c1b0..eb421bc 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/data/Pair.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/data/Pair.java
@@ -15,7 +15,9 @@ import java.util.function.Function;
*/
public class Pair<LeftType, RightType>
implements IPair<LeftType, RightType> {
+ // The left value
private LeftType leftValue;
+ // The right value
private RightType rightValue;
/**
@@ -40,24 +42,40 @@ public class Pair<LeftType, RightType>
@Override
public <BoundLeft, BoundRight> IPair<BoundLeft, BoundRight> bind(
BiFunction<LeftType, RightType, IPair<BoundLeft, BoundRight>> binder) {
+ if (binder == null) {
+ throw new NullPointerException("Binder must not be null.");
+ }
+
return binder.apply(leftValue, rightValue);
}
@Override
public <BoundLeft> IPair<BoundLeft, RightType> bindLeft(
Function<LeftType, IPair<BoundLeft, RightType>> leftBinder) {
+ if (leftBinder == null) {
+ throw new NullPointerException("Binder must not be null");
+ }
+
return leftBinder.apply(leftValue);
}
@Override
public <BoundRight> IPair<LeftType, BoundRight> bindRight(
Function<RightType, IPair<LeftType, BoundRight>> rightBinder) {
+ if (rightBinder == null) {
+ throw new NullPointerException("Binder must not be null");
+ }
+
return rightBinder.apply(rightValue);
}
@Override
public <MergedType> MergedType
merge(BiFunction<LeftType, RightType, MergedType> merger) {
+ if (merger == null) {
+ throw new NullPointerException("Merger must not be null");
+ }
+
return merger.apply(leftValue, rightValue);
}
@@ -70,12 +88,20 @@ public class Pair<LeftType, RightType>
@Override
public <NewLeft> IPair<NewLeft, RightType>
mapLeft(Function<LeftType, NewLeft> mapper) {
+ if (mapper == null) {
+ throw new NullPointerException("Mapper must not be null");
+ }
+
return new Pair<>(mapper.apply(leftValue), rightValue);
}
@Override
public <NewRight> IPair<LeftType, NewRight>
mapRight(Function<RightType, NewRight> mapper) {
+ if (mapper == null) {
+ throw new NullPointerException("Mapper must not be null");
+ }
+
return new Pair<>(leftValue, mapper.apply(rightValue));
}
} \ No newline at end of file