From 77fcc58d1facffbc3af50be8c05985350e9f1355 Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Sun, 17 Apr 2016 15:01:44 -0400 Subject: Code maintenace and changes --- .../src/main/java/bjc/utils/data/Identity.java | 115 +++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 BJC-Utils2/src/main/java/bjc/utils/data/Identity.java (limited to 'BJC-Utils2/src/main/java/bjc/utils/data/Identity.java') 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 + */ +/** + * Simple implementation of IHolder that has no hidden behavior + * + * @author ben + * + * @param + * The type contained in the holder + */ +public class Identity implements IHolder { + 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 IHolder bind( + Function> 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 IHolder map( + Function mapper) { + return new Identity<>(mapper.apply(heldValue)); + } + + @Override + public String toString() { + return "holding[v=" + heldValue + "]"; + } + + @Override + public IHolder transform( + UnaryOperator transformer) { + heldValue = transformer.apply(heldValue); + + return this; + } + + @Override + public UnwrappedType unwrap( + Function unwrapper) { + return unwrapper.apply(heldValue); + } +} \ No newline at end of file -- cgit v1.2.3