Class OptionalFactory
Object
OptionalFactory
Utility functions to create
Optional
from flags.
See OptionalUtilities
for utility functions for already-instantiated Optional
s.
- Author:
- Owen Feehan
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Optional
<T> Creates only if a boolean flag is true, otherwise returnsOptional.empty()
.static <T> Optional
<T> create
(boolean flag, T value) Creates only if a boolean flag is true, otherwise returnsOptional.empty()
.CreatesOptional.empty()
for an empty string, or otherwiseOptional.of()
.createChecked
(boolean flag, CheckedSupplier<T, E> supplier) Likecreate(boolean, T)
but accepts a supplier that throws a checked/exception.static <T> Optional
<T> createFlat
(boolean flag, Supplier<Optional<T>> valueIfFlagTrue)
-
Method Details
-
create
Creates only if a boolean flag is true, otherwise returnsOptional.empty()
.- Type Parameters:
T
- type of optional- Parameters:
flag
- boolean flag.value
- a value used only ifflag
is true.- Returns:
- an optional that is defined or empty depending on the flag.
-
create
Creates only if a boolean flag is true, otherwise returnsOptional.empty()
.- Type Parameters:
T
- type of optional- Parameters:
flag
- boolean flag.supplier
- a function to create a value T only called ifflag
is true.- Returns:
- an optional that is defined or empty depending on the flag.
-
createChecked
public static <T,E extends Exception> Optional<T> createChecked(boolean flag, CheckedSupplier<T, E> supplier) throws ELikecreate(boolean, T)
but accepts a supplier that throws a checked/exception.- Type Parameters:
T
- type of optionalE
- the checked exception- Parameters:
flag
- boolean flagsupplier
- a function to create a value T only called ifflag
is true- Returns:
- an optional that is defined or empty depending on the flag
- Throws:
E
- ifsupplier
throws it
-
create
CreatesOptional.empty()
for an empty string, or otherwiseOptional.of()
.- Parameters:
string
- the string (possibly empty or null).- Returns:
- the optional.
-
createFlat
- Type Parameters:
T
- type in optional.- Parameters:
flag
- iff true an populated optional is returned, otherwiseOptional.empty()
.valueIfFlagTrue
- used to generate a positive value.- Returns:
- a filled or empty optional depending on flag.
-