fj.data.Option Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of functionaljava Show documentation
Show all versions of functionaljava Show documentation
Functional Java is an open source library that supports closures for the Java programming language
package fj.data;
import static fj.Bottom.error;
import fj.F;
import fj.F0;
import fj.F2;
import fj.P;
import fj.P1;
import fj.P2;
import fj.P3;
import fj.P4;
import fj.P5;
import fj.P6;
import fj.P7;
import fj.P8;
import fj.Unit;
import fj.Show;
import fj.function.Effect1;
import fj.Equal;
import fj.Ord;
import fj.Hash;
import static fj.Function.*;
import static fj.P.p;
import static fj.Unit.unit;
import static fj.data.List.cons;
import static fj.data.List.cons_;
import static fj.data.Validation.parseByte;
import static fj.data.Validation.parseDouble;
import static fj.data.Validation.parseFloat;
import static fj.data.Validation.parseInt;
import static fj.data.Validation.parseLong;
import static fj.data.Validation.parseShort;
import static fj.Show.optionShow;
import java.util.Collection;
import java.util.Iterator;
/**
* An optional value that may be none (no value) or some (a value). This type is a replacement for
* the use of null
with better type checks.
*
* @version %build.number%
*/
public abstract class Option implements Iterable {
private Option() {
}
@Override
public String toString() {
return optionShow(Show.anyShow()).showS(this);
}
/**
* Returns an iterator for this optional value. This method exists to permit the use in a for
-each loop.
*
* @return A iterator for this optional value.
*/
public final Iterator iterator() {
return toCollection().iterator();
}
/**
* Returns the value from this optional value, or fails if there is no value.
*
* @return The value from this optional value, or fails if there is no value.
*/
public abstract A some();
/**
* Returns true
if this optional value has a value, false
otherwise.
*
* @return true
if this optional value has a value, false
otherwise.
*/
public final boolean isSome() {
return this instanceof Some;
}
/**
* Returns false
if this optional value has a value, true
otherwise.
*
* @return false
if this optional value has a value, true
otherwise.
*/
public final boolean isNone() {
return this instanceof None;
}
/**
* A first-class version of the isSome method.
*
* @return A function that returns true if a given optional value has a value, otherwise false.
*/
public static F