diff options
| author | Benjamin J. Culkin <bjculkin@mix.wvu.edu> | 2017-10-08 22:39:59 -0300 |
|---|---|---|
| committer | Benjamin J. Culkin <bjculkin@mix.wvu.edu> | 2017-10-08 22:39:59 -0300 |
| commit | c82e3b3b2de0633317ec8fc85925e91422820597 (patch) | |
| tree | 96567416ce23c5ce85601f9cedc3a94bb1c55cba /base/src/main/java/bjc/utils/funcutils/Isomorphism.java | |
| parent | b3ac1c8690c3e14c879913e5dcc03a5f5e14876e (diff) | |
Start splitting into maven modules
Diffstat (limited to 'base/src/main/java/bjc/utils/funcutils/Isomorphism.java')
| -rw-r--r-- | base/src/main/java/bjc/utils/funcutils/Isomorphism.java | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/base/src/main/java/bjc/utils/funcutils/Isomorphism.java b/base/src/main/java/bjc/utils/funcutils/Isomorphism.java new file mode 100644 index 0000000..2d3655e --- /dev/null +++ b/base/src/main/java/bjc/utils/funcutils/Isomorphism.java @@ -0,0 +1,60 @@ +package bjc.utils.funcutils; + +import java.util.function.Function; + +/** + * A pair of functions to transform between a pair of types. + * + * @author bjculkin + * + * @param <S> + * The source type of the isomorphism. + * + * @param <D> + * The destination type of isomorphism. + * + */ +public class Isomorphism<S, D> { + private Function<S, D> toFunc; + private Function<D, S> fromFunc; + + /** + * Create a new isomorphism. + * + * @param to + * The 'forward' function, from the source to the + * definition. + * + * @param from + * The 'backward' function, from the definition to the + * source. + */ + public Isomorphism(Function<S, D> to, Function<D, S> from) { + toFunc = to; + fromFunc = from; + } + + /** + * Apply the isomorphism forward. + * + * @param val + * The source value. + * + * @return The destination value. + */ + public D to(S val) { + return toFunc.apply(val); + } + + /** + * Apply the isomorphism backward. + * + * @param val + * The destination value. + * + * @return The source value. + */ + public S from(D val) { + return fromFunc.apply(val); + } +} |
