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; } @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 Function> lift(Function func) { return (val) -> { return new Identity<>(func.apply(val)); }; } @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); } }