Package org.organicdesign.fp.oneOf
Interface Option<T>
-
- All Superinterfaces:
Serializable
- All Known Implementing Classes:
None
,Option.Some
public interface Option<T> extends Serializable
Indicates presence or absence of a value (null is a valid, present value). None often means "end of stream" or "none found". This is NOT a type-safe null (unless you want that, in which case use thesomeOrNullNoneOf(Object)
static factory method). You can think of this class as OneOf1OrNone.
-
-
Nested Class Summary
Nested Classes Modifier and Type Interface Description static class
Option.Some<T>
Represents the presence of a value, even if that value is null.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Deprecated Methods Modifier and Type Method Description T
get()
Return the value wrapped in this Option.T
getOrElse(T t)
If this is Some, return the value wrapped in this Option.boolean
isSome()
Is this Some?<U> U
match(Fn1<T,U> has, Fn0<U> hasNot)
Pass in a function to execute if its Some and another to execute if its None.static <T> Option<T>
none()
Calling this instead of referring to NONE directly can make the type infrencer happy.static <T> Option<T>
of(T t)
Deprecated.static <T> Option<T>
some(T t)
Public static factory method for constructing the Some Option.static <T> Option<T>
someOrNullNoneOf(T t)
Construct an option, but if t is null, make it None instead of Some.<U> Option<U>
then(Fn1<T,Option<U>> f)
If this is Some, Apply the given function, else return None.
-
-
-
Method Detail
-
get
T get()
Return the value wrapped in this Option. Only safe to call this on Some.
-
getOrElse
T getOrElse(T t)
If this is Some, return the value wrapped in this Option. Otherwise, return the given value.
-
then
<U> Option<U> then(Fn1<T,Option<U>> f)
If this is Some, Apply the given function, else return None. Use this to chain options together, failing fast at the first none() or continuing through as many operations that return some as possible.
-
isSome
boolean isSome()
Is this Some?
-
match
<U> U match(Fn1<T,U> has, Fn0<U> hasNot)
Pass in a function to execute if its Some and another to execute if its None.
-
none
static <T> Option<T> none()
Calling this instead of referring to NONE directly can make the type infrencer happy.
-
of
@Deprecated static <T> Option<T> of(T t)
Deprecated.Wouldsome(Object)
be better for your purposes? For some reason, this returns none if you pass it a none. This is like flatmapping Options. Is this good for chaining or is this just weird? If the first, I'm going to rename it flatten() or flatmap() or something. If the second, it should be deleted. Remember, you also haveOr
for chaining or returning an error which is probably a better choice than Option for that kind of thing.
-
some
static <T> Option<T> some(T t)
Public static factory method for constructing the Some Option.
-
someOrNullNoneOf
static <T> Option<T> someOrNullNoneOf(T t)
Construct an option, but if t is null, make it None instead of Some.
-
-