Interface Maybe<T>
- Type Parameters:
T- the type of the value
- All Known Implementing Classes:
Maybe.None, Maybe.Some
A value that can be absent: Maybe.Some value or Maybe.None.
jsonlens uses this type, not null and not Optional, for values that can be
absent. For example, JsonModel.member(Object, ca.marcusdunn.jsonlens.model.JsonString) gives
Maybe.None for a member that is not in the object. Unlike Optional, Maybe is sealed, so a
switch with record patterns can examine it:
Maybe<String> present = Maybe.some("value");
Maybe<String> absent = Maybe.none();
String text = switch (absent) {
case Maybe.Some(String value) -> value;
case Maybe.None() -> "default";
};
int length = present.map(String::length).orElse(0); // 5
Result<String, String> required = absent.toResult(() -> "value is missing"); // Err
A Maybe.Some value is never null.
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic final recordAn absent value.static final recordA present value. -
Method Summary
Modifier and TypeMethodDescriptiondefault <U> Maybe<U> Applies a function that can give an absent value to a present value.default booleanisNone()Returns true for an absent value.default booleanisSome()Returns true for a present value.default <U> Maybe<U> Changes a present value.static <T> Maybe<T> none()Returns an absent value.default TReturns the present value, or a fallback value.default TReturns the present value, or a value from a supplier.static <T> Maybe<T> some(T value) Makes a present value.Makes a result: a success for a present value, or an error for an absent value.
-
Method Details
-
some
Makes a present value.- Type Parameters:
T- the type of the value- Parameters:
value- the value- Returns:
- a present value
-
none
Returns an absent value.- Type Parameters:
T- the type of the value- Returns:
- an absent value
-
isSome
-
isNone
-
map
-
flatMap
-
orElse
-
orElseGet
-
toResult
-