diff options
| author | bculkin2442 <bjculkin@mix.wvu.edu> | 2016-02-29 09:27:10 -0500 |
|---|---|---|
| committer | bculkin2442 <bjculkin@mix.wvu.edu> | 2016-02-29 09:27:10 -0500 |
| commit | b3eb57f0796ef79f58ed76a1baf1cdb315772b6a (patch) | |
| tree | 3fb54b1b7c97d75ed64b1187becd20f7cedcf4f7 | |
| parent | 25959ffced5eb4e239610e79f05c1774e54ec29c (diff) | |
Factored interface out of Pair
| -rw-r--r-- | BJC-Utils2/src/main/java/bjc/utils/data/IPair.java | 33 | ||||
| -rw-r--r-- | BJC-Utils2/src/main/java/bjc/utils/data/Pair.java | 34 |
2 files changed, 47 insertions, 20 deletions
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<L, R> { + + /** + * 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. + */ + <L2, R2> IPair<L2, R2> apply(Function<L, L2> lf, Function<R, R2> 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> E merge(BiFunction<L, R, E> bf); + + void doWith(BiConsumer<L, R> 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 <R> * The type of the thing held on the right (second) */ -public class Pair<L, R> { +public class Pair<L, R> implements IPair<L, R> { /** * 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<L, R> { 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 <L2, R2> Pair<L2, R2> apply(Function<L, L2> lf, + @Override + public <L2, R2> IPair<L2, R2> apply(Function<L, L2> lf, Function<R, R2> rf) { return new Pair<L2, R2>(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> E merge(BiFunction<L, R, E> bf) { return bf.apply(l, r); } + /* (non-Javadoc) + * @see bjc.utils.data.IPair#doWith(java.util.function.BiConsumer) + */ + @Override public void doWith(BiConsumer<L, R> bc) { bc.accept(l, r); } |
