Package org.javimmutable.collections
Class Maybe<T>
java.lang.Object
org.javimmutable.collections.Maybe<T>
- All Implemented Interfaces:
Serializable
,Iterable<T>
,IStreamable<T>
,SplitableIterable<T>
Used to handle cases when a value may or may not exist. The value of a
Maybe
can
be null but the notNull()
method can be used to
change a null value into an empty NotNull
when desired.
Alternatively, nullToEmpty()
can be used to convert a null
value into an empty Maybe
.
Provides a variety of utility methods to allow call chaining.- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionInvokes absentAction if this is empty.Invokes presentAction with this value if this is full.applyThrows
(Proc0Throws<E> absentAction) Invokes absentAction if this is empty.applyThrows
(Proc1Throws<? super T, E> presentAction) Invokes presentAction with this value if this is full.static <T> Maybe
<T> Determine if the object is an instance of the specified Class or a subclass.static <T> Maybe
<T> empty()
Returns aMaybe
with no value.static <T> Maybe
<T> Returns aMaybe
containing the first value of the collection.static <T> Maybe
<T> Returns aMaybe
containing the first value of the collection for which the predicate returns true.abstract <A> Maybe
<A> Produce aMaybe
based on this one.Produce aMaybe
based on this one.abstract <A> Maybe
<A> Produce aMaybe
based on this one.flatMapThrows
(Func0Throws<Maybe<A>, E> absentMapping, Func1Throws<? super T, Maybe<A>, E> presentMapping) Produce aMaybe
based on this one.flatMapThrows
(Func0Throws<Maybe<T>, E> absentMapping) Produce aMaybe
based on this one.flatMapThrows
(Func1Throws<? super T, Maybe<A>, E> presentMapping) Produce aMaybe
based on this one.abstract T
Gets this value.abstract T
Gets this value.abstract T
Gets this value.abstract boolean
isEmpty()
Returns true if this has no valueabstract boolean
isFull()
Returns true if this has a valueProduce a fullMaybe
.abstract <U> Maybe
<U> Produce a fullMaybe
.abstract <U> Maybe
<U> Produce aMaybe
that is empty if this is empty or else contains the result of passing this value to the given mapping function.mapThrows
(Func0Throws<? extends T, E> absentMapping) Produce a fullMaybe
.mapThrows
(Func0Throws<? extends U, E> absentMapping, Func1Throws<? super T, ? extends U, E> presentMapping) Produce a fullMaybe
.mapThrows
(Func1Throws<? super T, ? extends U, E> presentMapping) Produce aMaybe
that is empty if this is empty or else contains the result of passing this value to the given mapping function.abstract <U> U
Gets a value based on this value.abstract <U> U
Gets a value based on this value.abstract <U,
E extends Exception>
UmatchOrThrows
(Func0Throws<U, E> absentMapping, Func1Throws<? super T, U, E> presentMapping) Gets a value based on this value.abstract <U,
E extends Exception>
UmatchThrows
(U absentValue, Func1Throws<? super T, U, E> presentMapping) Gets a value based on this value.notNull()
If this empty or has a null value returnsNotNull.empty()
, otherwise returns aNotNull
containing the same value as this.If this empty or has a null value returnsempty()
, otherwise returns this.static <T> Maybe
<T> of
(T valueOrNull) Returns aMaybe
containing the value.Returns this if this is full and predicate returns false.Returns this if this is full and predicate returns true.abstract T
Gets this value.Gets this value.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.javimmutable.collections.IStreamable
getSpliteratorCharacteristics, iterator, parallelStream, spliterator, stream
Methods inherited from interface org.javimmutable.collections.SplitableIterable
forEachThrows, indexedForEach, indexedForEachThrows, reduce, reduceThrows
-
Method Details
-
empty
-
of
Returns aMaybe
containing the value. Null is a valid value. -
cast
Determine if the object is an instance of the specified Class or a subclass. If the object is null, returns aMaybe
containing null. If the object is not null but not of the correct class, returns an empty Holder. Otherwise returns a Holder containing the value cast to the target type.- Type Parameters:
T
- type of the class- Parameters:
klass
- class to cast the object tovalueOrNull
- object to be case- Returns:
- a
Maybe
-
first
-
first
-
notNull
If this empty or has a null value returnsNotNull.empty()
, otherwise returns aNotNull
containing the same value as this. -
nullToEmpty
If this empty or has a null value returnsempty()
, otherwise returns this. -
map
Produce a fullMaybe
. If thisMaybe
is full it is returned. Otherwise the absentMapping function is called to provide a value for the resultMaybe
.- Parameters:
absentMapping
- produces value if this is empty- Returns:
- a full
Maybe
-
map
Produce aMaybe
that is empty if this is empty or else contains the result of passing this value to the given mapping function.- Parameters:
presentMapping
- maps this value to new value- Returns:
- a possibly empty
Maybe
-
map
@Nonnull public abstract <U> Maybe<U> map(@Nonnull Func0<? extends U> absentMapping, @Nonnull Func1<? super T, ? extends U> presentMapping) Produce a fullMaybe
. If this is empty the absentMapping function is called to provide a value. Otherwise the presentMapping function is called to produce a new value based on this value.- Parameters:
absentMapping
- produces value when this is emptypresentMapping
- maps this value to new value- Returns:
- a full
Maybe
-
mapThrows
@Nonnull public abstract <E extends Exception> Maybe<T> mapThrows(@Nonnull Func0Throws<? extends T, E> absentMapping) throws EProduce a fullMaybe
. If thisMaybe
is full it is returned. Otherwise the absentMapping function is called to provide a value for the resultMaybe
.- Parameters:
absentMapping
- produces value if this is empty- Returns:
- a full
Maybe
- Throws:
E
-
mapThrows
@Nonnull public abstract <U,E extends Exception> Maybe<U> mapThrows(@Nonnull Func1Throws<? super T, ? extends U, throws EE> presentMapping) Produce aMaybe
that is empty if this is empty or else contains the result of passing this value to the given mapping function.- Parameters:
presentMapping
- maps this value to new value- Returns:
- a possibly empty
Maybe
- Throws:
E
-
mapThrows
@Nonnull public abstract <U,E extends Exception> Maybe<U> mapThrows(@Nonnull Func0Throws<? extends U, E> absentMapping, @Nonnull Func1Throws<? super T, throws E? extends U, E> presentMapping) Produce a fullMaybe
. If this is empty the absentMapping function is called to provide a value. Otherwise the presentMapping function is called to produce a new value based on this value.- Parameters:
absentMapping
- produces value when this is emptypresentMapping
- maps this value to new value- Returns:
- a full
Maybe
- Throws:
E
-
flatMap
-
flatMap
-
flatMap
@Nonnull public abstract <A> Maybe<A> flatMap(@Nonnull Func0<Maybe<A>> absentMapping, @Nonnull Func1<? super T, Maybe<A>> presentMapping) -
flatMapThrows
@Nonnull public abstract <E extends Exception> Maybe<T> flatMapThrows(@Nonnull Func0Throws<Maybe<T>, E> absentMapping) throws E -
flatMapThrows
@Nonnull public abstract <A,E extends Exception> Maybe<A> flatMapThrows(@Nonnull Func1Throws<? super T, Maybe<A>, throws EE> presentMapping) -
flatMapThrows
@Nonnull public abstract <A,E extends Exception> Maybe<A> flatMapThrows(@Nonnull Func0Throws<Maybe<A>, E> absentMapping, @Nonnull Func1Throws<? super T, throws EMaybe<A>, E> presentMapping) -
select
@Nonnull public abstract Maybe<T> select(@Nonnull java.util.function.Predicate<? super T> predicate) Returns this if this is full and predicate returns true. Otherwise an emptyMaybe
is returned.- Parameters:
predicate
- determines whether to accept this value- Returns:
- a
Maybe
-
reject
@Nonnull public abstract Maybe<T> reject(@Nonnull java.util.function.Predicate<? super T> predicate) Returns this if this is full and predicate returns false. Otherwise an emptyMaybe
is returned.- Parameters:
predicate
- determines whether to reject this value- Returns:
- a
Maybe
-
apply
Invokes absentAction if this is empty.- Parameters:
absentAction
- action to call if this is empty- Returns:
- this
-
apply
Invokes presentAction with this value if this is full.- Parameters:
presentAction
- action to call if this is full- Returns:
- this
-
applyThrows
@Nonnull public abstract <E extends Exception> Maybe<T> applyThrows(@Nonnull Proc0Throws<E> absentAction) throws E Invokes absentAction if this is empty.- Parameters:
absentAction
- action to call if this is empty- Returns:
- this
- Throws:
E
-
applyThrows
@Nonnull public abstract <E extends Exception> Maybe<T> applyThrows(@Nonnull Proc1Throws<? super T, E> presentAction) throws EInvokes presentAction with this value if this is full.- Parameters:
presentAction
- action to call if this is full- Returns:
- this
- Throws:
E
-
unsafeGet
Gets this value. Throws NoSuchElementException if this is empty.- Returns:
- this value
- Throws:
NoSuchElementException
- if this is empty
-
unsafeGet
public abstract <E extends Exception> T unsafeGet(@Nonnull Func0<E> absentExceptionMapping) throws E Gets this value. Calls absentExceptionMapping to get an exception to throw if this is empty.- Returns:
- this value
- Throws:
E
- an exception produced by mapping if this is empty
-
get
Gets this value. If this is empty returns absentValue instead.- Parameters:
absentValue
- value to return if this is empty
-
getOrNull
Gets this value. If this is empty returns null. -
getOr
Gets this value. If this is empty returns result of calling absentMapping instead.- Parameters:
absentMapping
- function to generate value to return if this is empty
-
match
Gets a value based on this value. If this is empty absentValue is returned. Otherwise this value is passed to presentMapping to obtain a return value.- Parameters:
absentValue
- value to return if this is emptypresentMapping
- function to map this value into return value- Returns:
- a value
-
matchOr
public abstract <U> U matchOr(@Nonnull Func0<U> absentMapping, @Nonnull Func1<? super T, U> presentMapping) Gets a value based on this value. If this is empty absentMapping is called to obtain a return value. Otherwise this value is passed to presentMapping to obtain a return value.- Parameters:
absentMapping
- function to produce a value to return if this is emptypresentMapping
- function to map this value into return value- Returns:
- a value
-
matchThrows
public abstract <U,E extends Exception> U matchThrows(U absentValue, @Nonnull Func1Throws<? super T, U, throws EE> presentMapping) Gets a value based on this value. If this is empty absentValue is returned. Otherwise this value is passed to presentMapping to obtain a return value.- Parameters:
absentValue
- value to return if this is emptypresentMapping
- function to map this value into return value- Returns:
- a value
- Throws:
E
-
matchOrThrows
public abstract <U,E extends Exception> U matchOrThrows(@Nonnull Func0Throws<U, E> absentMapping, @Nonnull Func1Throws<? super T, throws EU, E> presentMapping) Gets a value based on this value. If this is empty absentMapping is called to obtain a return value. Otherwise this value is passed to presentMapping to obtain a return value.- Parameters:
absentMapping
- function to produce a value to return if this is emptypresentMapping
- function to map this value into return value- Returns:
- a value
- Throws:
E
-
isEmpty
public abstract boolean isEmpty()Returns true if this has no value -
isFull
public abstract boolean isFull()Returns true if this has a value
-