-
- Type Parameters:
T
- type of the value
public interface ConfigValue<T>
A typed value of aConfig
node.You can use accessor methods on
Config
to obtain this value, such asConfig.as(Class)
. A typed value that has all the methods ofOptional
- including the ones added in JDK9 and newer. In addition it has methods to access config values assupplier()
.
-
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description <N> ConfigValue<N>
as(Function<T,N> mapper)
Convert thisConfigValue
to a different type using a mapper function.Optional<T>
asOptional()
Returns a typed value asOptional
.default Optional<T>
filter(Predicate<? super T> predicate)
If a value is present, and the value matches the given predicate, return anOptional
describing the value, otherwise return an emptyOptional
.default <U> Optional<U>
flatMap(Function<? super T,Optional<U>> mapper)
If a value is present, apply the providedOptional
-bearing mapping function to it, return that result, otherwise return an emptyOptional
.default T
get()
Typed value of the representedConfig
node.default void
ifPresent(Consumer<? super T> consumer)
If a value is present, invoke the specified consumer with the value, otherwise do nothing.default void
ifPresentOrElse(Consumer<T> action, Runnable emptyAction)
If a value is present, performs the given action with the value, otherwise performs the given empty-based action.default boolean
isPresent()
Returntrue
if there is a value present, otherwisefalse
.Config.Key
key()
Returns the fully-qualified key of the originatingConfig
node.default <U> Optional<U>
map(Function<? super T,? extends U> mapper)
If a value is present, apply the provided mapping function to it, and if the result is non-null, return anOptional
describing the result.default String
name()
Returns the last token of the fully-qualified key for the originatingConfig
node.Supplier<Optional<T>>
optionalSupplier()
Returns aSupplier
of anOptional<T>
of the configuration node.default Optional<T>
or(Supplier<? extends Optional<T>> supplier)
If the underlyingOptional
does not have a value, set it to theOptional
produced by the supplying function.default T
orElse(T other)
Return the value if present, otherwise returnother
.default T
orElseGet(Supplier<? extends T> other)
Return the value if present, otherwise invokeother
and return the result of that invocation.default <X extends Throwable>
TorElseThrow(Supplier<? extends X> exceptionSupplier)
Return the contained value, if present, otherwise throw an exception to be created by the provided supplier.default Stream<T>
stream()
If a value is present, returns a sequentialStream
containing only that value, otherwise returns an emptyStream
.Supplier<T>
supplier()
Returns a supplier of a typed value.Supplier<T>
supplier(T defaultValue)
Returns a supplier of a typed value with a default.
-
-
-
Method Detail
-
key
Config.Key key()
Returns the fully-qualified key of the originatingConfig
node.The fully-qualified key is a sequence of tokens derived from the name of each node along the path from the config root to the current node. Tokens are separated by
.
(the dot character). Seename()
for more information on the format of each token.- Returns:
- current config node key
- See Also:
name()
-
name
default String name()
Returns the last token of the fully-qualified key for the originatingConfig
node.The name of a node is the last token in its fully-qualified key.
The exact format of the name depends on the
Type
of the containing node:- from a
Config.Type.OBJECT
node the token for a child is the name of the object member; - from a
Config.Type.LIST
node the token for a child is a zero-based index of the element, an unsigned base-10 integer value with no leading zeros.
The ABNF syntax of config key is:
config-key = *1( key-token *( "." key-token ) ) key-token = *( unescaped / escaped ) unescaped = %x00-2D / %x2F-7D / %x7F-10FFFF ; %x2E ('.') and %x7E ('~') are excluded from 'unescaped' escaped = "~" ( "0" / "1" ) ; representing '~' and '.', respectively
- Returns:
- current config node key
- See Also:
key()
,Config.Key.name()
- from a
-
asOptional
Optional<T> asOptional() throws ConfigMappingException
Returns a typed value asOptional
. Returns aempty
for nodes without a value. As this class implements all methods ofOptional
, this is only a utility method if an actualOptional
instance is needed.- Returns:
- value as type instance as
Optional
,empty
in case the node does not have a direct value - Throws:
ConfigMappingException
- in case the value cannot be converted to the expected type- See Also:
get()
-
get
default T get() throws MissingValueException, ConfigMappingException
Typed value of the representedConfig
node. ThrowsMissingValueException
if the node isConfig.Type.MISSING
type.- Returns:
- direct value of this node converted to the expected type
- Throws:
MissingValueException
- in case the node isConfig.Type.MISSING
.ConfigMappingException
- in case the value cannot be converted to the expected type
-
as
<N> ConfigValue<N> as(Function<T,N> mapper)
Convert thisConfigValue
to a different type using a mapper function.- Type Parameters:
N
- type of the returnedConfigValue
- Parameters:
mapper
- mapper to map the type of thisConfigValue
to a type of the returnedConfigValue
- Returns:
- a new value with the new type
-
supplier
Supplier<T> supplier()
Returns a supplier of a typed value. The value provided from the supplier is the latest value available. E.g. in case there is a file config source that is being watched and a value is changed, this supplier would return the latest value, whereasget()
would return the original value.Note that
Supplier.get()
can throw aConfigMappingException
orMissingValueException
as theget()
method.- Returns:
- a supplier of a typed value
-
supplier
Supplier<T> supplier(T defaultValue)
Returns a supplier of a typed value with a default. The value provided from the supplier is the latest value available. E.g. in case there is a file config source that is being watched and a value is changed, this supplier would return the latest value, whereasorElse(Object)
would return the original value.Note that
Supplier.get()
can throw aConfigMappingException
as theorElse(Object)
method.- Parameters:
defaultValue
- a value to be returned if the supplied value represents aConfig
node that has no direct value- Returns:
- a supplier of a typed value
-
optionalSupplier
Supplier<Optional<T>> optionalSupplier()
Returns aSupplier
of anOptional<T>
of the configuration node. Supplier returns aempty
if the node does not have a direct value.- Returns:
- a supplier of the value as an
Optional
typed instance,empty
in case the node does not have a direct value - See Also:
asOptional()
,supplier()
-
or
default Optional<T> or(Supplier<? extends Optional<T>> supplier)
If the underlyingOptional
does not have a value, set it to theOptional
produced by the supplying function.- Parameters:
supplier
- the supplying function that produces anOptional
- Returns:
- returns current value using
asOptional()
if present, otherwise value produced by the supplying function. - Throws:
NullPointerException
- if the supplying function isnull
or produces anull
result
-
isPresent
default boolean isPresent()
Returntrue
if there is a value present, otherwisefalse
.Copied from
Optional
. You can get real optional fromasOptional()
.- Returns:
true
if there is a value present, otherwisefalse
- See Also:
Optional.isPresent()
-
ifPresentOrElse
default void ifPresentOrElse(Consumer<T> action, Runnable emptyAction)
If a value is present, performs the given action with the value, otherwise performs the given empty-based action.- Parameters:
action
- the action to be performed, if a value is presentemptyAction
- the empty-based action to be performed, if no value is present- Throws:
NullPointerException
- if a value is present and the given action isnull
, or no value is present and the given empty-based action isnull
.
-
ifPresent
default void ifPresent(Consumer<? super T> consumer)
If a value is present, invoke the specified consumer with the value, otherwise do nothing.Copied from
Optional
. You can get real optional fromasOptional()
.- Parameters:
consumer
- block to be executed if a value is present- Throws:
NullPointerException
- if value is present andconsumer
is null- See Also:
Optional.ifPresent(Consumer)
-
filter
default Optional<T> filter(Predicate<? super T> predicate)
If a value is present, and the value matches the given predicate, return anOptional
describing the value, otherwise return an emptyOptional
.Copied from
Optional
. You can get real optional fromasOptional()
.- Parameters:
predicate
- a predicate to apply to the value, if present- Returns:
- an
Optional
describing the value of thisOptional
if a value is present and the value matches the given predicate, otherwise an emptyOptional
- Throws:
NullPointerException
- if the predicate is null- See Also:
Optional.filter(Predicate)
-
map
default <U> Optional<U> map(Function<? super T,? extends U> mapper)
If a value is present, apply the provided mapping function to it, and if the result is non-null, return anOptional
describing the result. Otherwise return an emptyOptional
.- Type Parameters:
U
- The type of the result of the mapping function- Parameters:
mapper
- a mapping function to apply to the value, if present- Returns:
- an
Optional
describing the result of applying a mapping function to the value of thisOptional
, if a value is present, otherwise an emptyOptional
- Throws:
NullPointerException
- if the mapping function is nullCopied from
Optional
. You can get real optional fromasOptional()
.- See Also:
Optional.map(Function)
-
flatMap
default <U> Optional<U> flatMap(Function<? super T,Optional<U>> mapper)
If a value is present, apply the providedOptional
-bearing mapping function to it, return that result, otherwise return an emptyOptional
. This method is similar tomap(Function)
, but the provided mapper is one whose result is already anOptional
, and if invoked,flatMap
does not wrap it with an additionalOptional
.Copied from
Optional
. You can get real optional fromasOptional()
.- Type Parameters:
U
- The type parameter to theOptional
returned by- Parameters:
mapper
- a mapping function to apply to the value, if present the mapping function- Returns:
- the result of applying an
Optional
-bearing mapping function to the value of thisOptional
, if a value is present, otherwise an emptyOptional
- Throws:
NullPointerException
- if the mapping function is null or returns a null result- See Also:
Optional.flatMap(Function)
-
orElse
default T orElse(T other)
Return the value if present, otherwise returnother
.Copied from
Optional
. You can get real optional fromasOptional()
.- Parameters:
other
- the value to be returned if there is no value present, may be null- Returns:
- the value, if present, otherwise
other
- See Also:
Optional.orElse(Object)
-
orElseGet
default T orElseGet(Supplier<? extends T> other)
Return the value if present, otherwise invokeother
and return the result of that invocation.Copied from
Optional
. You can get real optional fromasOptional()
.- Parameters:
other
- aSupplier
whose result is returned if no value is present- Returns:
- the value if present otherwise the result of
other.get()
- Throws:
NullPointerException
- if value is not present andother
is null- See Also:
Optional.orElseGet(Supplier)
-
orElseThrow
default <X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier) throws X extends Throwable
Return the contained value, if present, otherwise throw an exception to be created by the provided supplier.Copied from
Optional
. You can get real optional fromasOptional()
.- Type Parameters:
X
- Type of the exception to be thrown- Parameters:
exceptionSupplier
- The supplier which will return the exception to be thrown- Returns:
- the present value
- Throws:
X
- if there is no value presentNullPointerException
- if no value is present andexceptionSupplier
is nullX extends Throwable
- See Also:
Optional.orElseThrow(Supplier)
-
-