- All Known Subinterfaces:
Body
,Formdata
,QueryString
,ValueNode
,WebSocketMessage
- 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 Summary
Modifier and TypeMethodDescriptionstatic ValueNode
Creates a sequence/array of values.default boolean
Convert this value to boolean (if possible).default boolean
booleanValue
(boolean defaultValue) Convert this value to boolean (if possible) or fallback to given value when missing.default byte
Convert this value to byte (if possible).default byte
byteValue
(byte defaultValue) Convert this value to byte (if possible) or fallback to given value when missing.static ValueNode
Creates a value that fits better with the given values.static ValueNode
Creates a value that fits better with the given values.default double
Convert this value to double (if possible).default double
doubleValue
(double defaultValue) Convert this value to double (if possible) or fallback to given value when missing.default float
Convert this value to float (if possible).default float
floatValue
(float defaultValue) Convert this value to float (if possible) or fallback to given value when missing.static Formdata
Creates a formdata.static ValueNode
Create a hash/object value using the map values.static ValueNode
Create a hash/object value using the map values.default int
intValue()
Convert this value to int (if possible).default int
intValue
(int defaultValue) Convert this value to int (if possible) or fallback to given value when missing.default boolean
isArray()
True if this value is an array/sequence (not single or hash).default boolean
True for missing values.default boolean
isObject()
True if this is a hash/object value (not single or array).default boolean
True for present values.default boolean
isSingle()
True if this is a single value (not a hash or array).default long
Convert this value to long (if possible).default long
longValue
(long defaultValue) Convert this value to long (if possible) or fallback to given value when missing.static ValueNode
Creates a missing value.name()
Name of this value ornull
.<T> T
Convert this value to the given type.default <T extends Enum<T>>
TtoEnum
(SneakyThrows.Function<String, T> fn) Convert this value to an Enum.default <T extends Enum<T>>
TConvert this value to an Enum.toList()
Get list of values.default <T> List<T>
Get list of the given type.toMap()
Value as single-value map.Value as multi-value map.<T> T
toNullable
(Class<T> type) Convert this value to the given type.Get a value or empty optional.default <T> Optional<T>
toOptional
(Class<T> type) Get a value or empty optional.toSet()
Get set of values.default <T> Set<T>
Get set of the given type.value()
Get string value.static ValueNode
Creates a single value.default <T> T
value
(SneakyThrows.Function<String, T> fn) Convert value using the given function.default String
Convert this value to String (if possible) or fallback to given value when missing.default String
Convert this value to String (if possible) ornull
when missing.
-
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
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
Convert this value to String (if possible) ornull
when missing.- Returns:
- Convert this value to String (if possible) or
null
when missing.
-
value
Convert value using the given function.- Type Parameters:
T
- Target type.- Parameters:
fn
- Function.- Returns:
- Converted value.
-
value
Get string value.- Returns:
- String value.
-
toList
Get list of values.- Returns:
- List of values.
-
toSet
Get set of values.- Returns:
- set of values.
-
toEnum
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
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
Name of this value ornull
.- Returns:
- Name of this value or
null
.
-
toOptional
Get a value or empty optional.- Type Parameters:
T
- Item type.- Parameters:
type
- Item type.- Returns:
- Value or empty optional.
-
toList
Get list of the given type.- Type Parameters:
T
- Item type.- Parameters:
type
- Type to convert.- Returns:
- List of items.
-
toSet
Get set of the given type.- Type Parameters:
T
- Item type.- Parameters:
type
- Type to convert.- Returns:
- Set of items.
-
to
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
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
Value as multi-value map.- Returns:
- Value as multi-value map.
-
toMap
Value as single-value map.- Returns:
- Value as single-value map.
-
missing
Creates a missing value.- Parameters:
name
- Name of missing value.- Returns:
- Missing value.
-
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
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
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
Create a hash/object value using the map values.- Parameters:
ctx
- Current context.values
- Map values.- Returns:
- A hash/object value.
-
formdata
Creates a formdata.- Parameters:
ctx
- Current context.- Returns:
- A hash/object value.
-
headers
Create a hash/object value using the map values.- Parameters:
ctx
- Current context.values
- Map values.- Returns:
- A hash/object value.
-