Interface Maybe<A>
- Type Parameters:
A- the contained type
- All Superinterfaces:
Effect.Applicant<A>, Iterable<A>
- All Known Subinterfaces:
Either.Projection<A,B, L, R>
- All Known Implementing Classes:
Either.AbstractProjection, Either.LeftProjection, Either.RightProjection, Option, Option.None, Option.Some
Implemented by things that may or may not contain a value.
Note there are some methods suggested by this interface that cannot be expressed here as the return type cannot be expressed in Java's type system (due to the lack of higher-kinded types). These are for instance (where M is the implementing Maybe sub-type):
- <B> Maybe<B> map(Function<? super A, B>)
- <B> Maybe<B> flatMap(Function<? super A, Maybe<B>>
- Maybe<A> filter(Predicate<? super A>)
- Since:
- 1.0
-
Method Summary
Modifier and TypeMethodDescriptionbooleanTests whether the option contains a given value as an element.booleanWhether this isis definedand applying the predicate to the contained value returns true.booleanReturnstrueif emptyor the result of the application of the given function to the value.get()Get the value if defined.Get the valueif definedor call the supplier and return its value if not.getOrElse(B other) Get the value if defined, otherwise returnsother.Deprecated.getOrError(Supplier<String> msg) Get the value or throws an error with the supplied message if not defined.Get the value if defined or null if not.getOrThrow(Supplier<X> ifUndefined) Get the value or throws the supplied throwable if not defined.booleanIf the type contains a value return true.booleanisEmpty()If the type does not contain a value return true.iterator()Return an iterator for this type.Methods inherited from interface Effect.Applicant
foreachMethods inherited from interface Iterable
forEach, spliterator
-
Method Details
-
get
A get()Get the value if defined. Throw an exception otherwise.- Returns:
- the wrapped value
- Throws:
NoSuchElementException- if this is a none
-
getOrElse
Get the value if defined, otherwise returnsother.- Type Parameters:
B- default value type- Parameters:
other- value to return if thisis empty- Returns:
- wrapped value if this
is defined, otherwise returnsother
-
getOr
Get the valueif definedor call the supplier and return its value if not. ReplacesgetOrElse(Supplier). Get the valueif definedor call the supplier and return its value if not.- Parameters:
supplier- called if thisis empty- Returns:
- the wrapped value or the value from the
Supplier
-
getOrElse
Deprecated.since 3.0getOrElse(Supplier)is being replaced withgetOr(Supplier). In Java 8 type inference cannot disambiguate between an overloaded method taking a generic A and the same method taking a Supplier<A>.Get the valueif definedor call the supplier and return its value if not.- Parameters:
supplier- called if thisis empty- Returns:
- the wrapped value or the value from the
Supplier
-
getOrNull
A getOrNull()Get the value if defined or null if not.Although the use of null is discouraged, code written to use these must often interface with code that expects and returns nulls.
- Returns:
- the value or null if not defined
-
getOrError
Get the value or throws an error with the supplied message if not defined.Used when absolutely sure this
is defined.- Parameters:
msg- the message for the error.- Returns:
- the contained value.
-
getOrThrow
Get the value or throws the supplied throwable if not defined.Used when absolutely sure this
is defined.- Type Parameters:
X- exception type- Parameters:
ifUndefined- the supplier of the throwable.- Returns:
- the contained value.
- Throws:
X- the throwable the supplier creates if there is no value.- Since:
- 2.0
-
isDefined
boolean isDefined()If the type contains a value return true.- Returns:
trueif this holds a value,falseotherwise.
-
isEmpty
boolean isEmpty()If the type does not contain a value return true.- Returns:
trueif this does not hold a value,falseotherwise.
-
exists
Whether this isis definedand applying the predicate to the contained value returns true.- Parameters:
p- the predicate to test, must not be null- Returns:
trueif defined and the predicate returns true for the contained value,falseotherwise.
-
iterator
-
forall
Returnstrueif emptyor the result of the application of the given function to the value.- Parameters:
p- The predicate function to test on the contained value, must not be null- Returns:
trueif no value or returns the result of the application of the given function to the value.
-
contains
Tests whether the option contains a given value as an element.- Parameters:
elem- the element to test- Returns:
trueif the option has an element that is equal to elem,falseotherwise- Since:
- 6.1
-
getOrElse(Supplier)is being replaced withgetOr(Supplier).