Module io.jooby
Package io.jooby

Interface Value

All Known Subinterfaces:
Body, Formdata, QueryString, ValueNode, WebSocketMessage

public interface Value
Unified API for HTTP value. This API plays two role:

- unify access to HTTP values, like query, path, form and header parameter - works as validation API, because it is able to check for required and type-safe values

The value API is composed by three types:

- Single value - Object value - Sequence of values (array)

Single value are can be converted to string, int, boolean, enum like values. Object value is a key:value structure (like a hash). Sequence of values are index based structure.

All these 3 types are modeled into a single Value class. At any time you can treat a value as 1) single, 2) hash or 3) array of them.

Since:
2.0.0
Author:
edgar
  • Method Details

    • longValue

      default long longValue()
      Convert this value to long (if possible).
      Returns:
      Long value.
    • longValue

      default long longValue(long defaultValue)
      Convert this value to long (if possible) or fallback to given value when missing.
      Parameters:
      defaultValue - Default value.
      Returns:
      Convert this value to long (if possible) or fallback to given value when missing.
    • intValue

      default int intValue()
      Convert this value to int (if possible).
      Returns:
      Int value.
    • intValue

      default int intValue(int defaultValue)
      Convert this value to int (if possible) or fallback to given value when missing.
      Parameters:
      defaultValue - Default value.
      Returns:
      Convert this value to int (if possible) or fallback to given value when missing.
    • byteValue

      default byte byteValue()
      Convert this value to byte (if possible).
      Returns:
      Convert this value to byte (if possible).
    • byteValue

      default byte byteValue(byte defaultValue)
      Convert this value to byte (if possible) or fallback to given value when missing.
      Parameters:
      defaultValue - Default value.
      Returns:
      Convert this value to byte (if possible) or fallback to given value when missing.
    • floatValue

      default float floatValue()
      Convert this value to float (if possible).
      Returns:
      Convert this value to float (if possible).
    • floatValue

      default float floatValue(float defaultValue)
      Convert this value to float (if possible) or fallback to given value when missing.
      Parameters:
      defaultValue - Default value.
      Returns:
      Convert this value to float (if possible) or fallback to given value when missing.
    • doubleValue

      default double doubleValue()
      Convert this value to double (if possible).
      Returns:
      Convert this value to double (if possible).
    • doubleValue

      default double doubleValue(double defaultValue)
      Convert this value to double (if possible) or fallback to given value when missing.
      Parameters:
      defaultValue - Default value.
      Returns:
      Convert this value to double (if possible) or fallback to given value when missing.
    • booleanValue

      default boolean booleanValue()
      Convert this value to boolean (if possible).
      Returns:
      Convert this value to boolean (if possible).
    • booleanValue

      default boolean booleanValue(boolean defaultValue)
      Convert this value to boolean (if possible) or fallback to given value when missing.
      Parameters:
      defaultValue - Default value.
      Returns:
      Convert this value to boolean (if possible) or fallback to given value when missing.
    • value

      @NonNull default String value(@NonNull String defaultValue)
      Convert this value to String (if possible) or fallback to given value when missing.
      Parameters:
      defaultValue - Default value.
      Returns:
      Convert this value to String (if possible) or fallback to given value when missing.
    • valueOrNull

      @Nullable default String valueOrNull()
      Convert this value to String (if possible) or null when missing.
      Returns:
      Convert this value to String (if possible) or null when missing.
    • value

      @NonNull default <T> T value(@NonNull SneakyThrows.Function<String,T> fn)
      Convert value using the given function.
      Type Parameters:
      T - Target type.
      Parameters:
      fn - Function.
      Returns:
      Converted value.
    • value

      @NonNull String value()
      Get string value.
      Returns:
      String value.
    • toList

      @NonNull List<String> toList()
      Get list of values.
      Returns:
      List of values.
    • toSet

      @NonNull Set<String> toSet()
      Get set of values.
      Returns:
      set of values.
    • toEnum

      @NonNull default <T extends Enum<T>> T toEnum(@NonNull SneakyThrows.Function<String,T> fn)
      Convert this value to an Enum.
      Type Parameters:
      T - Enum type.
      Parameters:
      fn - Mapping function.
      Returns:
      Enum.
    • toEnum

      @NonNull default <T extends Enum<T>> T toEnum(@NonNull SneakyThrows.Function<String,T> fn, @NonNull Function<String,String> nameProvider)
      Convert this value to an Enum.
      Type Parameters:
      T - Enum type.
      Parameters:
      fn - Mapping function.
      nameProvider - Enum name provider.
      Returns:
      Enum.
    • toOptional

      @NonNull default Optional<String> toOptional()
      Get a value or empty optional.
      Returns:
      Value or empty optional.
    • isSingle

      default boolean isSingle()
      True if this is a single value (not a hash or array).
      Returns:
      True if this is a single value (not a hash or array).
    • isMissing

      default boolean isMissing()
      True for missing values.
      Returns:
      True for missing values.
    • isPresent

      default boolean isPresent()
      True for present values.
      Returns:
      True for present values.
    • isArray

      default boolean isArray()
      True if this value is an array/sequence (not single or hash).
      Returns:
      True if this value is an array/sequence.
    • isObject

      default boolean isObject()
      True if this is a hash/object value (not single or array).
      Returns:
      True if this is a hash/object value (not single or array).
    • name

      @Nullable String name()
      Name of this value or null.
      Returns:
      Name of this value or null.
    • toOptional

      @NonNull default <T> Optional<T> toOptional(@NonNull Class<T> type)
      Get a value or empty optional.
      Type Parameters:
      T - Item type.
      Parameters:
      type - Item type.
      Returns:
      Value or empty optional.
    • toList

      @NonNull default <T> List<T> toList(@NonNull Class<T> type)
      Get list of the given type.
      Type Parameters:
      T - Item type.
      Parameters:
      type - Type to convert.
      Returns:
      List of items.
    • toSet

      @NonNull default <T> Set<T> toSet(@NonNull Class<T> type)
      Get set of the given type.
      Type Parameters:
      T - Item type.
      Parameters:
      type - Type to convert.
      Returns:
      Set of items.
    • to

      @NonNull <T> T to(@NonNull Class<T> type)
      Convert this value to the given type. Support values are single-value, array-value and object-value. Object-value can be converted to a JavaBean type.
      Type Parameters:
      T - Element type.
      Parameters:
      type - Type to convert.
      Returns:
      Instance of the type.
    • toNullable

      @Nullable <T> T toNullable(@NonNull Class<T> type)
      Convert this value to the given type. Support values are single-value, array-value and object-value. Object-value can be converted to a JavaBean type.
      Type Parameters:
      T - Element type.
      Parameters:
      type - Type to convert.
      Returns:
      Instance of the type or null.
    • toMultimap

      @NonNull Map<String,List<String>> toMultimap()
      Value as multi-value map.
      Returns:
      Value as multi-value map.
    • toMap

      @NonNull default Map<String,String> toMap()
      Value as single-value map.
      Returns:
      Value as single-value map.
    • missing

      @NonNull static ValueNode missing(@NonNull String name)
      Creates a missing value.
      Parameters:
      name - Name of missing value.
      Returns:
      Missing value.
    • value

      @NonNull static ValueNode value(@NonNull Context ctx, @NonNull String name, @NonNull String value)
      Creates a single value.
      Parameters:
      ctx - Current context.
      name - Name of value.
      value - Value.
      Returns:
      Single value.
    • array

      @NonNull static ValueNode array(@NonNull Context ctx, @NonNull String name, @NonNull List<String> values)
      Creates a sequence/array of values.
      Parameters:
      ctx - Current context.
      name - Name of array.
      values - Field values.
      Returns:
      Array value.
    • create

      @NonNull static ValueNode create(Context ctx, @NonNull String name, @Nullable List<String> values)
      Creates a value that fits better with the given values.

      - For null/empty values. It produces a missing value. - For single element (size==1). It produces a single value - For multi-value elements (size>1). It produces an array value.

      Parameters:
      ctx - Current context.
      name - Field name.
      values - Field values.
      Returns:
      A value.
    • create

      @NonNull static ValueNode create(Context ctx, @NonNull String name, @Nullable String value)
      Creates a value that fits better with the given values.

      - For null/empty values. It produces a missing value. - For single element (size==1). It produces a single value

      Parameters:
      ctx - Current context.
      name - Field name.
      value - Field values.
      Returns:
      A value.
    • hash

      @NonNull static ValueNode hash(Context ctx, @NonNull Map<String,Collection<String>> values)
      Create a hash/object value using the map values.
      Parameters:
      ctx - Current context.
      values - Map values.
      Returns:
      A hash/object value.
    • formdata

      @NonNull static Formdata formdata(Context ctx)
      Creates a formdata.
      Parameters:
      ctx - Current context.
      Returns:
      A hash/object value.
    • headers

      @NonNull static ValueNode headers(Context ctx, @NonNull Map<String,Collection<String>> values)
      Create a hash/object value using the map values.
      Parameters:
      ctx - Current context.
      values - Map values.
      Returns:
      A hash/object value.