summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbculkin2442 <bjculkin@mix.wvu.edu>2017-04-10 22:54:16 -0400
committerbculkin2442 <bjculkin@mix.wvu.edu>2017-04-10 22:54:16 -0400
commit2cc559513eda04aabbc140c2024ebf650631bccb (patch)
treeba401e31f3faa6564b87c21ae01571d24ee5902c
parent889fac2bdf993dc86f64a8893c0260fdcf848acb (diff)
Add isomorphisms.
An isomorphism is a pair of functions between a source and destination type. Ideally, they should be inverses, but java has no way to check that by itself.
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/funcutils/Isomorphism.java60
1 files changed, 60 insertions, 0 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcutils/Isomorphism.java b/BJC-Utils2/src/main/java/bjc/utils/funcutils/Isomorphism.java
new file mode 100644
index 0000000..2d3655e
--- /dev/null
+++ b/BJC-Utils2/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);
+ }
+}