Interface OptionalValue<T>

Type Parameters:
T - type of the value
All Superinterfaces:
Value<T>
All Known Subinterfaces:
ConfigValue<T>, ConfigValue<T>

public interface OptionalValue<T> extends Value<T>
A typed value with support for mapping (conversion) to other types.
  • Method Details

    • create

      static <T> OptionalValue<T> create(MapperManager mapperManager, String name, Class<T> type, String... qualifiers)
      Create an empty value. Empty value is not backed by data and all of its methods consider it is empty.
      Type Parameters:
      T - type of the value
      Parameters:
      mapperManager - mapper manager to use for mapping types
      name - name of the value
      type - type of the value, to correctly handle mapping exceptions
      qualifiers - qualifiers of the mapper
      Returns:
      an empty value
    • create

      static <T> OptionalValue<T> create(MapperManager mapperManager, String name, GenericType<T> type, String... qualifiers)
      Create an empty value. Empty value is not backed by data and all of its methods consider it is empty.
      Type Parameters:
      T - type of the value
      Parameters:
      mapperManager - mapper manager to use for mapping types
      name - name of the value
      type - type of the value, to correctly handle mapping exceptions
      qualifiers - qualifiers of the mapper
      Returns:
      an empty value
    • create

      static <T> OptionalValue<T> create(MapperManager mapperManager, String name, T value, String... qualifiers)
      Create a value backed by data. The type of the value is "guessed" from the instance provided.
      Type Parameters:
      T - type of the value
      Parameters:
      mapperManager - mapper manager to use for mapping types
      name - name of the value
      value - value, must not be null
      qualifiers - qualifiers of the mapper
      Returns:
      a value backed by data
    • create

      static <T> OptionalValue<T> create(MapperManager mapperManager, String name, T value, GenericType<T> type, String... qualifiers)
      Create a value backed by data.
      Type Parameters:
      T - type of the value
      Parameters:
      mapperManager - mapper manager to use for mapping types
      name - name of the value
      value - value, must not be null
      type - a more precise type that could be guessed form an instance
      qualifiers - qualifiers of the mapper
      Returns:
      a value backed by data
    • as

      <N> OptionalValue<N> as(Class<N> type)
      Description copied from interface: Value
      Convert this value to a different type using a mapper.
      Specified by:
      as in interface Value<T>
      Type Parameters:
      N - type we expect
      Parameters:
      type - type to convert to
      Returns:
      converted value
    • as

      <N> OptionalValue<N> as(GenericType<N> type)
      Description copied from interface: Value
      Convert this value to a different type using a mapper.
      Specified by:
      as in interface Value<T>
      Type Parameters:
      N - type we expect
      Parameters:
      type - type to convert to
      Returns:
      converted value
    • as

      <N> OptionalValue<N> as(Function<? super T,? extends N> mapper)
      Description copied from interface: Value
      Convert this Value to a different type using a mapper function.
      Specified by:
      as in interface Value<T>
      Type Parameters:
      N - type of the returned Value
      Parameters:
      mapper - mapper to map the type of this Value to a type of the returned Value
      Returns:
      a new value with the new type
    • get

      T get()
      Typed value.
      Specified by:
      get in interface Value<T>
      Returns:
      direct value converted to the expected type
      Throws:
      NoSuchElementException - or appropriate module specific exception if the value is not present
    • or

      default Optional<T> or(Supplier<? extends Optional<T>> supplier)
      If the underlying Optional does not have a value, set it to the Optional produced by the supplying function.
      Parameters:
      supplier - the supplying function that produces an Optional
      Returns:
      returns current value using Value.asOptional() if present, otherwise value produced by the supplying function.
      Throws:
      NullPointerException - if the supplying function is null or produces a null result
    • isPresent

      default boolean isPresent()
      Return true if there is a value present, otherwise false.

      Copied from Optional. You can get real optional from Value.asOptional().

      Returns:
      true if there is a value present, otherwise false
      See Also:
    • isEmpty

      default boolean isEmpty()
      Return false if there is a value present, otherwise true.

      Copied from Optional. You can get real optional from Value.asOptional().

      Returns:
      false if there is a value present, otherwise true
      See Also:
    • 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 present
      emptyAction - the empty-based action to be performed, if no value is present
      Throws:
      NullPointerException - if a value is present and the given action is null, or no value is present and the given empty-based action is null.
    • 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 from Value.asOptional().

      Parameters:
      consumer - block to be executed if a value is present
      Throws:
      NullPointerException - if value is present and consumer is null
      See Also:
    • 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 an Optional describing the result. Otherwise return an empty Optional.
      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 this Optional, if a value is present, otherwise an empty Optional
      Throws:
      NullPointerException - if the mapping function is null

      Copied from Optional. You can get real optional from Value.asOptional().

      See Also:
    • orElse

      default T orElse(T other)
      Return the value if present, otherwise return other.

      Copied from Optional. You can get real optional from Value.asOptional().

      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:
    • orElseGet

      default T orElseGet(Supplier<? extends T> other)
      Return the value if present, otherwise invoke other and return the result of that invocation.

      Copied from Optional. You can get real optional from Value.asOptional().

      Parameters:
      other - a Supplier 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 and other is null
      See Also:
    • orElseThrow

      default <X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier) throws X
      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 from Value.asOptional().

      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 present
      NullPointerException - if no value is present and exceptionSupplier is null
      See Also:
    • orElseThrow

      default T orElseThrow()
      If a value is present, returns the value, otherwise throws NoSuchElementException.
      Returns:
      the non-null value described by this value
      Throws:
      NoSuchElementException - if no value is present
    • asBoolean

      default OptionalValue<Boolean> asBoolean()
      Description copied from interface: Value
      Boolean typed value.
      Specified by:
      asBoolean in interface Value<T>
      Returns:
      typed value
    • asString

      default OptionalValue<String> asString()
      Description copied from interface: Value
      String typed value.
      Specified by:
      asString in interface Value<T>
      Returns:
      typed value
    • asInt

      default OptionalValue<Integer> asInt()
      Description copied from interface: Value
      Integer typed value.
      Specified by:
      asInt in interface Value<T>
      Returns:
      typed value
    • asLong

      default OptionalValue<Long> asLong()
      Description copied from interface: Value
      Long typed value.
      Specified by:
      asLong in interface Value<T>
      Returns:
      typed value
    • asDouble

      default OptionalValue<Double> asDouble()
      Description copied from interface: Value
      Double typed value.
      Specified by:
      asDouble in interface Value<T>
      Returns:
      typed value