Package cms.util.maybe
Class Maybe<T>
java.lang.Object
cms.util.maybe.Maybe<T>
- All Implemented Interfaces:
Iterable<T>
,Collection<T>
,Set<T>
An object that may contain a value of type
T
. Similar to
Java's Optional class but uses a fast checked exception instead of
a slow unchecked exception.-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T,
U extends T>
Maybe<T> Convert aMaybe<U>
to aMaybe<T>
, whenT
is a supertype ofU
.boolean
containsAll
(Collection<?> c) static <T> Maybe
<T> from
(T v) Create a Maybe from a possibly null value v.fromCatching
(ThrowingSupplier<T, E> valueOrThrow, Class<? super E> exnClass) Create aMaybe
from the result of a computation, returningMaybes.None
if the computation returns null or throws a particular exception.static <T> Maybe
<T> fromOptional
(Optional<? extends T> optional) Create aMaybe
from anOptional
value.abstract T
get()
Returns the contained value, if present.static <T> T
getOptional
(Optional<? extends T> optional) Get the value in anOptional
, if present; otherwise throw the checked exceptionNoMaybeValue
.abstract boolean
Returns whether a value is contained in thisMaybe
.iterator()
Provide an iterator that yields either oneT
or none, depending.static <T> Maybe
<T> none()
Returns an emptyMaybe
.abstract T
Returns the contained value, if any; otherwise, returnsother
.abstract T
Returns the contained value, if any; otherwise, returnsother.get()
.orElseMaybe
(Supplier<? extends Maybe<? extends T>> other) Returns this if a value is contained; otherwise, returnsother.get()
.orElseThrow
(Supplier<E> throwable) Returns the contained value, if any; otherwise, throws the specified exception.static <T> Maybe
<T> some
(T v) Creates aMaybe
from a non-null argument.abstract <U> Maybe
<U> If a valuev
is present, returns aMaybe
containingf(v)
, which must be non-null.abstract void
Callcons
on the contained value, if any.abstract void
If a value is contained, run consThen on the value; otherwise run procElseabstract <U> Maybe
<U> If a valuev
is present, returnsf(v)
.Create anOptional
from aMaybe
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArray
-
Constructor Details
-
Method Details
-
isPresent
public abstract boolean isPresent()Returns whether a value is contained in thisMaybe
.- Returns:
- whether a value is contained.
-
get
Returns the contained value, if present. Otherwise, throws the exception.- Returns:
- the contained value
- Throws:
NoMaybeValue
- if no value is contained in this Maybe. This method is primarily useful for writing code that is supposed to perform side effects, e.g.:Maybe
<T>
m = ... try { ... m.get() ... } catch (NoMaybeValue e) { ... }
-
thenMaybe
If a valuev
is present, returnsf(v)
. Otherwise, returns an emptyMaybe
. (This is a monadic bind.) This method is useful for chaining together a series ofMaybe
accesses. For example, supposingT.foo()
returns a Maybe:Maybe<T> mt = ... mt.thenMaybe(t -> t.foo().then(f -> ...)) .orElse(...)
- Type Parameters:
U
- The type of the value that may be returned by the functionf
.- Parameters:
f
- The function to be applied to the contained value, if any.- Returns:
- the
Maybe
returned byf
, if a value is contained in thisMaybe
. Otherwise, an emptyMaybe
.
-
then
If a valuev
is present, returns aMaybe
containingf(v)
, which must be non-null. Otherwise, returns an emptyMaybe
. (This is a monadic bind composed with a monadic unit.) This method can be used conveniently along with orElse to handle both maybe cases, e.g.:Maybe<T> mt = ... mt.then(t -> ...) .orElse(...)
- Type Parameters:
U
- The type of the value that may be returned by the functionf
.- Parameters:
f
- The function to be applied to the contained value, if any.- Returns:
- a
Maybe
containing the valuef
, if a value is contained in thisMaybe
. Otherwise, an emptyMaybe
.
-
orElse
Returns the contained value, if any; otherwise, returnsother
. Note: since orElse is an ordinary method call, its argument is always computed, unlike a Javaelse
statement. If the argument is expensive to compute or has side effects,orElseGet()
should be used instead.- Parameters:
other
- The value to be returned if no value is in thisMaybe
.- Returns:
- The contained value, or an empty
Maybe
.
-
orElseGet
-
orElseThrow
-
orElseMaybe
-
thenDo
-
thenElse
-
iterator
-
containsAll
- Specified by:
containsAll
in interfaceCollection<T>
- Specified by:
containsAll
in interfaceSet<T>
-
from
Create a Maybe from a possibly null value v. The Maybe will contain a value if v is non-null.- Type Parameters:
T
- The type ofMaybe
to be created.- Parameters:
v
- The value to put into aMaybe
, or null.
-
fromOptional
-
toOptional
-
getOptional
Get the value in anOptional
, if present; otherwise throw the checked exceptionNoMaybeValue
. This method allowsOptional
s to be used as if they were Maybes.- Returns:
- The value in the optional, if any
- Throws:
NoMaybeValue
- if no value is present.
-
fromCatching
public static <T,E extends Throwable> Maybe<T> fromCatching(ThrowingSupplier<T, E> valueOrThrow, Class<? super E> exnClass) Create aMaybe
from the result of a computation, returningMaybes.None
if the computation returns null or throws a particular exception. Note that this method only catches the specified exception. If the computation throws any other exception, it is propagated.As an example, the following code:
Maybe<Float> oldScoreClient; try { oldScoreClient = Maybe.some(Float.parseFloat(vals[5])); } catch (NumberFormatException e) { oldScoreClient = Maybe.none(); }
final Maybe<Float> oldScoreClient = Maybe.fromCatching(() -> Float.parseFloat(vals[5]), NumberFormatException.class)
- Type Parameters:
T
- the type parameter of the resultingMaybe
E
- the type of the exception being caught- Parameters:
valueOrThrow
- a computation, represented by aThrowingSupplier
that either returns a value to store in this maybe or throws an exception.exnClass
- the exception thatvalueOrThrow
might throw.- Returns:
- a
Maybes.Some
instance containing the result ofvalueOrThrow
, orMaybes.None
if thevalueOrThrow
throws.
-
none
Returns an emptyMaybe
. -
some
Creates aMaybe
from a non-null argument.- Parameters:
v
- must be non-null- Throws:
IllegalArgumentException
- if a null value is passed to it.
-
cast
-