Package org.n52.janmayen
Class Optionals
java.lang.Object
org.n52.janmayen.Optionals
Utility functions for
Optional.-
Method Summary
Modifier and TypeMethodDescriptionstatic booleanChecks if any of the optionals is present.static booleanChecks if any of the optionals is present.static booleanChecks if any of the optionals is present.static <U> Optional<U> ifPresentOrElse(Optional<U> optional, Consumer<U> consumer, Runnable ifNotPresent) Feeds {code optional}'s value toconsumerif it is present or else runsifNotPresent.static <U,T> Optional <U> Maps theoptionalusingmapperif it is present or runsifNotPresent.static <U extends Comparable<? super U>>
Comparator<Optional<U>> Creates a comparator to sort by the optional's value,nulls first.static <U> Comparator<Optional<U>> nullsFirst(Comparator<? super U> comparator) Creates a comparator wrappingcomparatorto sort by the optional's value,nulls first.static <U extends Comparable<? super U>>
Comparator<Optional<U>> Creates a comparator to sort by the optional's value,nulls last.static <U> Comparator<Optional<U>> nullsLast(Comparator<? super U> comparator) Creates a comparator wrappingcomparatorto sort by the optional's value,nulls last.static <U> Optional<U> Gets the first value of the first optional that is present.static <U> Optional<U> Gets the first value of the first optional that is present.static <U> Optional<U> Gets the value ofaif it is present or else the value ofb.static <U> Optional<U> Gets the value ofaif it is present or else the value ofb.static <U> Optional<U> Gets the first value of the first optional that is present.static <U> UorElseNull(Optional<U> optional) Gets the value ofoptionalif it is present,nullotherwise.
-
Method Details
-
any
Checks if any of the optionals is present.- Parameters:
optionals- the optionals- Returns:
- if at least one is present
-
any
Checks if any of the optionals is present.- Parameters:
optionals- the optionals- Returns:
- if at least one is present
-
any
Checks if any of the optionals is present.- Parameters:
optionals- the optionals- Returns:
- if at least one is present
-
nullsLast
Creates a comparator wrappingcomparatorto sort by the optional's value,nulls last.- Type Parameters:
U- the optional's type- Parameters:
comparator- the comparator- Returns:
- the new comparator
-
nullsLast
Creates a comparator to sort by the optional's value,nulls last.- Type Parameters:
U- the optional's type- Returns:
- the new comparator
-
nullsFirst
Creates a comparator wrappingcomparatorto sort by the optional's value,nulls first.- Type Parameters:
U- the optional's type- Parameters:
comparator- the comparator- Returns:
- the new comparator
-
nullsFirst
Creates a comparator to sort by the optional's value,nulls first.- Type Parameters:
U- the optional's type- Returns:
- the new comparator
-
orElseNull
Gets the value ofoptionalif it is present,nullotherwise.- Type Parameters:
U- the optional's type- Parameters:
optional- the optional- Returns:
- the optional's value or
null
-
ifPresentOrElse
public static <U> Optional<U> ifPresentOrElse(Optional<U> optional, Consumer<U> consumer, Runnable ifNotPresent) Feeds {code optional}'s value toconsumerif it is present or else runsifNotPresent.- Type Parameters:
U- the optional's type- Parameters:
optional- the optionalconsumer- the consumerifNotPresent- the runnable- Returns:
- the optional
-
mapOrElse
public static <U,T> Optional<U> mapOrElse(Optional<T> optional, Function<? super T, ? extends U> mapper, Runnable ifNotPresent) Maps theoptionalusingmapperif it is present or runsifNotPresent.- Type Parameters:
U- the optional's typeT- the resulting optional's type- Parameters:
optional- the optinalmapper- the mapperifNotPresent- the runnable- Returns:
- the new optional
-
or
Gets the value ofaif it is present or else the value ofb.- Type Parameters:
U- the optional's type- Parameters:
a- the first optionalb- a supplier for the second optional- Returns:
- the value
-
or
Gets the value ofaif it is present or else the value ofb.- Type Parameters:
U- the optional's type- Parameters:
a- the first optionalb- a the second optional- Returns:
- the value
-
or
Gets the first value of the first optional that is present.- Type Parameters:
U- the optional's type- Parameters:
optionals- the optionals- Returns:
- the first present value
-
or
Gets the first value of the first optional that is present.- Type Parameters:
U- the optional's type- Parameters:
optionals- the optionals- Returns:
- the first present value
-
or
Gets the first value of the first optional that is present.- Type Parameters:
U- the optional's type- Parameters:
optionals- the optionals- Returns:
- the first present value
-