diff options
Diffstat (limited to 'src/main/java/bjc/data/Option.java')
| -rw-r--r-- | src/main/java/bjc/data/Option.java | 51 |
1 files changed, 26 insertions, 25 deletions
diff --git a/src/main/java/bjc/data/Option.java b/src/main/java/bjc/data/Option.java index b5d6d5e..6ca8b13 100644 --- a/src/main/java/bjc/data/Option.java +++ b/src/main/java/bjc/data/Option.java @@ -11,9 +11,17 @@ import java.util.function.UnaryOperator; * @param <ContainedType> * The type of the value that may or may not be held. */ -public class Option<ContainedType> implements IHolder<ContainedType> { +public class Option<ContainedType> implements Holder<ContainedType> { private ContainedType held; - + private boolean isHolding; + + /** + * Create a new empty optional. + */ + public Option() { + isHolding = false; + } + /** * Create a new optional, using the given initial value. * @@ -22,38 +30,35 @@ public class Option<ContainedType> implements IHolder<ContainedType> { */ public Option(final ContainedType seed) { held = seed; + isHolding = true; } @Override - public <BoundType> IHolder<BoundType> - bind(final Function<ContainedType, IHolder<BoundType>> binder) { - if (held == null) - return new Option<>(null); + public <BoundType> Holder<BoundType> + bind(final Function<ContainedType, Holder<BoundType>> binder) { + if (isHolding) return new Option<>(); return binder.apply(held); } @Override - public <NewType> Function<ContainedType, IHolder<NewType>> + public <NewType> Function<ContainedType, Holder<NewType>> lift(final Function<ContainedType, NewType> func) { return val -> new Option<>(func.apply(val)); } @Override - public <MappedType> IHolder<MappedType> + public <MappedType> Holder<MappedType> map(final Function<ContainedType, MappedType> mapper) { - if (held == null) - return new Option<>(null); + if (isHolding) return new Option<>(); return new Option<>(mapper.apply(held)); } @Override - public IHolder<ContainedType> + public Holder<ContainedType> transform(final UnaryOperator<ContainedType> transformer) { - if (held != null) { - held = transformer.apply(held); - } + if (isHolding) held = transformer.apply(held); return this; } @@ -61,8 +66,7 @@ public class Option<ContainedType> implements IHolder<ContainedType> { @Override public <UnwrappedType> UnwrappedType unwrap(final Function<ContainedType, UnwrappedType> unwrapper) { - if (held == null) - return null; + if (isHolding) return null; return unwrapper.apply(held); } @@ -84,20 +88,17 @@ public class Option<ContainedType> implements IHolder<ContainedType> { @Override public boolean equals(final Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (!(obj instanceof Option<?>)) - return false; + if (this == obj) return true; + if (obj == null) return false; + if (!(obj instanceof Option<?>)) return false; final Option<?> other = (Option<?>) obj; if (held == null) { - if (other.held != null) - return false; - } else if (!held.equals(other.held)) + if (other.held != null) return false; + } else if (!held.equals(other.held)) { return false; + } return true; } |
