com.ajjpj.abase.collection.immutable.AOption Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of a-base Show documentation
Show all versions of a-base Show documentation
a-base is a library of basic (hence the name) classes, most notably immutable collection classes with copy-on-write operations
package com.ajjpj.abase.collection.immutable;
import com.ajjpj.abase.collection.ACollectionHelper;
import com.ajjpj.abase.collection.AEquality;
import com.ajjpj.abase.function.AFunction1;
import com.ajjpj.abase.function.APredicate;
import java.util.Collections;
import java.util.Iterator;
import java.util.NoSuchElementException;
/**
* This class represents an object that may or may not be present. It is intended as a more explicit, less error prone
* replacement for using null
to represent the absence of a value.
*
* There are two flavors of AOption
: AOption.some(...)
and AOption.none()
.
* AOption.some(x)
represents the presence of a value, namely 'x', while AOption.none()
* represents 'no value'.
*
* There are two key reasons for using this class instead of using null
to encode the absence of a value.
*
* Firstly, AOption
explicitly shows in the code that a value is optional, forcing code to explicitly deal
* with that fact. A method returning a value that may or may not be present (e.g. a call to get(...)
on
* a map) can specify AOption as its return type to make that characteristic explicit.
*
* Secondly, code can operate on an AOption
regardless of whether there is a value or not (e.g. by calling
* map()
). This functional style of programming can make for code that is easier to read and less cluttered
* than having if
statements checking for the presence of values all over the place.
*
* This class was inspired by the Option
class from the Scala standard library. Thanks for the excellent
* code, Scala team!
*
* @author arno
*/
public abstract class AOption implements ACollection> {
public static AOption some(T el) {
return new ASome<>(el);
}
@SuppressWarnings("unchecked")
public static AOption none() {
return (AOption) ANone.INSTANCE;
}
public static AOption fromNullable(T nullable) {
return nullable != null ? some(nullable) : AOption.none();
}
public abstract boolean isDefined();
public boolean isEmpty() {
return !isDefined();
}
public abstract T get();
public T getOrElse(T el) {
return isDefined() ? get() : el;
}
@Override public AFilterMonadic> flatMap(AFunction1, T, E> f) throws E {
throw new UnsupportedOperationException("AOption can not be flattened");
}
@Override public ACollection> flatten() {
throw new UnsupportedOperationException("AOption can not be flattened");
}
@Override public String mkString() {
return ACollectionHelper.mkString(this);
}
@Override public String mkString(String separator) {
return ACollectionHelper.mkString(this, separator);
}
@Override public String mkString(String prefix, String separator, String suffix) {
return ACollectionHelper.mkString(this, prefix, separator, suffix);
}
public abstract boolean equals(Object o);
public abstract int hashCode();
static class ASome extends AOption {
private final T el;
ASome(T el) {
this.el = el;
}
@Override public T get() {
return el;
}
@Override public boolean isDefined() {
return true;
}
@Override public AOption find(APredicate pred) throws E {
return filter(pred);
}
@Override public AOption map(AFunction1 f) throws E {
return some(f.apply(el));
}
@Override public AOption filter(APredicate pred) throws E {
if(pred.apply(el))
return this;
else
return none();
}
@Override public int size() {
return 1;
}
@Override public boolean nonEmpty() {
return true;
}
@Override public boolean forAll(APredicate pred) throws E {
return find(pred).isDefined();
}
@Override public boolean exists(APredicate pred) throws E {
return find(pred).isDefined();
}
@Override public AMap> groupBy(AFunction1 f) throws E {
return groupBy(f, AEquality.EQUALS);
}
@Override public AMap> groupBy(AFunction1 f, AEquality keyEquality) throws E { //TODO junit
final AMap> result = AHashMap.empty(keyEquality);
return result.updated(f.apply(el), this);
}
@Override public AList toList() {
return AList.nil().cons(el);
}
@Override public AHashSet toSet() {
return AHashSet.empty().added(el);
}
@Override public AHashSet toSet(AEquality equality) {
return AHashSet.empty(equality).added(el);
}
@Override public Iterator iterator() {
return Collections.singletonList(el).iterator();
}
@Override public String toString() {
return "AOption.some(" + el + ")";
}
@Override public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ASome aSome = (ASome) o;
if (el != null ? !el.equals(aSome.el) : aSome.el != null) return false;
return true;
}
@Override public int hashCode() {
return el != null ? el.hashCode() : 0;
}
}
static class ANone extends AOption