From b3eb57f0796ef79f58ed76a1baf1cdb315772b6a Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Mon, 29 Feb 2016 09:27:10 -0500 Subject: Factored interface out of Pair --- BJC-Utils2/src/main/java/bjc/utils/data/IPair.java | 33 +++++++++++++++++++++ BJC-Utils2/src/main/java/bjc/utils/data/Pair.java | 34 +++++++++------------- 2 files changed, 47 insertions(+), 20 deletions(-) create mode 100644 BJC-Utils2/src/main/java/bjc/utils/data/IPair.java (limited to 'BJC-Utils2/src/main') diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/IPair.java b/BJC-Utils2/src/main/java/bjc/utils/data/IPair.java new file mode 100644 index 0000000..9da7d22 --- /dev/null +++ b/BJC-Utils2/src/main/java/bjc/utils/data/IPair.java @@ -0,0 +1,33 @@ +package bjc.utils.data; + +import java.util.function.BiConsumer; +import java.util.function.BiFunction; +import java.util.function.Function; + +public interface IPair { + + /** + * Create a new pair by applying the given functions to the left/right. + * Does not change the internal contents of this pair. + * + * @param lf + * The function to apply to the left value. + * @param rf + * The function to apply to the right value. + * @return A new pair containing the two modified values. + */ + IPair apply(Function lf, Function rf); + + /** + * Collapse this pair to a single value. Does not change the internal + * contents of this pair. + * + * @param bf + * The function to use to collapse the pair. + * @return The collapsed value. + */ + E merge(BiFunction bf); + + void doWith(BiConsumer bc); + +} \ No newline at end of file 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 5bd9d63..4e7f6ae 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/Pair.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/Pair.java @@ -14,16 +14,16 @@ import java.util.function.Function; * @param * The type of the thing held on the right (second) */ -public class Pair { +public class Pair implements IPair { /** * The left value of the pair */ - private L l; + protected L l; /** * The right value of the pair */ - private R r; + protected R r; /** * Create a new pair that holds two nulls. @@ -45,33 +45,27 @@ public class Pair { r = right; } - /** - * Create a new pair by applying the given functions to the left/right. - * Does not change the internal contents of this pair. - * - * @param lf - * The function to apply to the left value. - * @param rf - * The function to apply to the right value. - * @return A new pair containing the two modified values. + /* (non-Javadoc) + * @see bjc.utils.data.IPair#apply(java.util.function.Function, java.util.function.Function) */ - public Pair apply(Function lf, + @Override + public IPair apply(Function lf, Function rf) { return new Pair(lf.apply(l), rf.apply(r)); } - /** - * Collapse this pair to a single value. Does not change the internal - * contents of this pair. - * - * @param bf - * The function to use to collapse the pair. - * @return The collapsed value. + /* (non-Javadoc) + * @see bjc.utils.data.IPair#merge(java.util.function.BiFunction) */ + @Override public E merge(BiFunction bf) { return bf.apply(l, r); } + /* (non-Javadoc) + * @see bjc.utils.data.IPair#doWith(java.util.function.BiConsumer) + */ + @Override public void doWith(BiConsumer bc) { bc.accept(l, r); } -- cgit v1.2.3