summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils/data/Identity.java
diff options
context:
space:
mode:
authorbculkin2442 <bjculkin@mix.wvu.edu>2016-04-17 15:01:44 -0400
committerbculkin2442 <bjculkin@mix.wvu.edu>2016-04-17 15:01:44 -0400
commit77fcc58d1facffbc3af50be8c05985350e9f1355 (patch)
treeb7b81d24c107e644924dc526f8bb034efc62d2dc /BJC-Utils2/src/main/java/bjc/utils/data/Identity.java
parenta5850915df72f5968fd1b281eb9e455d50c580ee (diff)
Code maintenace and changes
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/data/Identity.java')
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/data/Identity.java115
1 files changed, 115 insertions, 0 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/Identity.java b/BJC-Utils2/src/main/java/bjc/utils/data/Identity.java
new file mode 100644
index 0000000..dcc7bef
--- /dev/null
+++ b/BJC-Utils2/src/main/java/bjc/utils/data/Identity.java
@@ -0,0 +1,115 @@
+package bjc.utils.data;
+
+import java.util.function.Function;
+import java.util.function.UnaryOperator;
+
+/**
+ * @author ben
+ *
+ * @param <ContainedType>
+ */
+/**
+ * Simple implementation of IHolder that has no hidden behavior
+ *
+ * @author ben
+ *
+ * @param <ContainedType>
+ * The type contained in the holder
+ */
+public class Identity<ContainedType> implements IHolder<ContainedType> {
+ private ContainedType heldValue;
+
+ /**
+ * Create a holder holding null
+ */
+ public Identity() {
+ heldValue = null;
+ }
+
+ /**
+ * Create a holder holding the specified value
+ *
+ * @param value
+ * The value to hold
+ */
+ public Identity(ContainedType value) {
+ heldValue = value;
+ }
+
+ @Override
+ public <BoundType> IHolder<BoundType> bind(
+ Function<ContainedType, IHolder<BoundType>> binder) {
+ return binder.apply(heldValue);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see java.lang.Object#equals(java.lang.Object)
+ */
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ } else if (obj == null) {
+ return false;
+ } else if (getClass() != obj.getClass()) {
+ return false;
+ }
+
+ Identity<?> other = (Identity<?>) obj;
+
+ if (heldValue == null) {
+ if (other.heldValue != null) {
+ return false;
+ }
+ } else if (!heldValue.equals(other.heldValue)) {
+ return false;
+ }
+
+ return true;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see java.lang.Object#hashCode()
+ */
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+
+ int result = 1;
+
+ int fieldHash = (heldValue == null) ? 0 : heldValue.hashCode();
+
+ result = prime * result + fieldHash;
+
+ return result;
+ }
+
+ @Override
+ public <MappedType> IHolder<MappedType> map(
+ Function<ContainedType, MappedType> mapper) {
+ return new Identity<>(mapper.apply(heldValue));
+ }
+
+ @Override
+ public String toString() {
+ return "holding[v=" + heldValue + "]";
+ }
+
+ @Override
+ public IHolder<ContainedType> transform(
+ UnaryOperator<ContainedType> transformer) {
+ heldValue = transformer.apply(heldValue);
+
+ return this;
+ }
+
+ @Override
+ public <UnwrappedType> UnwrappedType unwrap(
+ Function<ContainedType, UnwrappedType> unwrapper) {
+ return unwrapper.apply(heldValue);
+ }
+} \ No newline at end of file