Package com.linecorp.armeria.common
Interface QueryParams
public interface QueryParams
Immutable HTTP query parameters.
Building a new
Building a new
Specifying a non-
Using
Using
Building a new QueryParams
You can use the QueryParams.of()
factory methods or
the QueryParamsBuilder
to build a new QueryParams
from scratch:
// Using of()
QueryParams paramsWithOf = QueryParams.of("the_string", "fourty-two",
"the_number", 42);
// Using builder()
QueryParams paramsWithBuilder =
QueryParams.builder()
.add("the_string", "forty-two")
.add("the_number", 42)
.build();
assert paramsWithOf.equals(paramsWithBuilder);
Building a new QueryParams
from an existing one
You can use toBuilder()
or withMutations(Consumer)
to build
a new QueryParams
derived from an existing one:
QueryParams params = QueryParams.of("name1", "value0");
// Using toBuilder()
QueryParams paramsWithToBuilder = params.toBuilder()
.set("name1", "value1")
.add("name2", "value2")
.build();
// Using withMutations()
QueryParams paramsWithMutations = params.withMutations(builder -> {
builder.set("name1", "value1");
builder.add("name2", "value2");
});
assert paramsWithToBuilder.equals(paramsWithMutations);
// Note that the original parameters remain unmodified.
assert !params.equals(paramsWithToBuilder);
assert !params.equals(paramsWithMutations);
Specifying a non-String
parameter value
Certain parameter values are better represented as a Java object, such as Integer
,
MediaType
, Instant
or Date
, than as a String
. Armeria's query parameters
API allows you to specify a Java object of well-known type as a parameter value by converting it into
an HTTP-friendly String
representation:
Number
,CharSequence
andMediaType
- Converted via
toString()
- e.g.
"42"
,"string"
,"text/plain; charset=utf-8"
- Converted via
CacheControl
- Converted via
asHeaderValue()
- e.g.
"no-cache, no-store, must-revalidate"
- Converted via
Instant
,TemporalAccessor
,Date
andCalendar
- Converted into a time and date string as specified in RFC1123
- e.g.
Sun, 27 Nov 2016 19:37:15 UTC
- All other types
- Converted via
toString()
- Converted via
Using QueryParams.of()
factory methods
QueryParams params = QueryParams.of("the-number", 42,
"the-media-type", MediaType.JSON_UTF_8,
"the-date", Instant.now());
Using QueryParamsBuilder
QueryParams params =
QueryParams.builder()
.setObject("the-number", 42)
.setObject("the-media-type", MediaType.JSON_UTF_8)
.setObject("the-date", Instant.now())
.build();
Specifying value type explicitly
You might prefer type-safe setters for more efficiency and less ambiguity:
QueryParams params =
QueryParams.builder()
.setInt("the-number", 42)
.set("the-media-type", MediaType.JSON_UTF_8.toString())
.setTimeMillis("the-date", System.currentTimeMillis())
.build();
-
Method Summary
Modifier and Type Method Description default StringBuilder
appendQueryString(StringBuilder buf)
Encodes all parameter entries into a query string, as defined in 4.10.22.6, HTML5 W3C Recommendation, and appends it into the specifiedStringBuilder
.static QueryParamsBuilder
builder()
Returns a new empty builder.boolean
contains(String name)
Returnstrue
if a parameter with thename
exists,false
otherwise.boolean
contains(String name, String value)
Returnstrue
if a parameter with thename
andvalue
exists.boolean
containsDouble(String name, double value)
Returnstrue
if a parameter with thename
andvalue
exists.boolean
containsFloat(String name, float value)
Returnstrue
if a parameter with thename
andvalue
exists.boolean
containsInt(String name, int value)
Returnstrue
if a parameter with thename
andvalue
exists.boolean
containsLong(String name, long value)
Returnstrue
if a parameter with thename
andvalue
exists.boolean
containsObject(String name, Object value)
Returnstrue
if a parameter with thename
andvalue
exists.boolean
containsTimeMillis(String name, long value)
Returnstrue
if a parameter with thename
andvalue
exists.void
forEach(BiConsumer<String,String> action)
Invokes the specifiedaction
for all parameter entries.void
forEachValue(String name, Consumer<String> action)
Invokes the specifiedaction
for all values of the parameters with the specifiedname
.static QueryParams
fromQueryString(String queryString)
Decodes the specified query string into aQueryParams
, as defined in 4.10.22.6, HTML5 W3C Recommendation.static QueryParams
fromQueryString(String queryString, boolean semicolonAsSeparator)
Decodes the specified query string into aQueryParams
, as defined in 4.10.22.6, HTML5 W3C Recommendation.static QueryParams
fromQueryString(String queryString, int maxParams)
Decodes the specified query string into aQueryParams
, as defined in 4.10.22.6, HTML5 W3C Recommendation.static QueryParams
fromQueryString(String queryString, int maxParams, boolean semicolonAsSeparator)
Decodes the specified query string into aQueryParams
, as defined in 4.10.22.6, HTML5 W3C Recommendation.String
get(String name)
Returns the value of a parameter with the specifiedname
.String
get(String name, String defaultValue)
Returns the value of a parameter with the specifiedname
.List<String>
getAll(String name)
Returns all values for the parameter with the specified name.Double
getDouble(String name)
Returns thedouble
value of a parameter with the specifiedname
.double
getDouble(String name, double defaultValue)
Returns thedouble
value of a parameter with the specifiedname
.Float
getFloat(String name)
Returns thefloat
value of a parameter with the specifiedname
.float
getFloat(String name, float defaultValue)
Returns thefloat
value of a parameter with the specifiedname
.Integer
getInt(String name)
Returns theint
value of a parameter with the specifiedname
.int
getInt(String name, int defaultValue)
Returns theint
value of a parameter with the specifiedname
.Long
getLong(String name)
Returns thelong
value of a parameter with the specifiedname
.long
getLong(String name, long defaultValue)
Returns thelong
value of a parameter with the specifiedname
.Long
getTimeMillis(String name)
Returns the value of a parameter with the specifiedname
in milliseconds.long
getTimeMillis(String name, long defaultValue)
Returns the value of a parameter with the specifiedname
in milliseconds.boolean
isEmpty()
Returnstrue
if this parameters does not contain any entries.Iterator<Map.Entry<String,String>>
iterator()
Returns anIterator
that yields all parameter entries.Set<String>
names()
Returns aSet
of all parameter names.static QueryParams
of()
Returns an emptyQueryParams
.static QueryParams
of(String name, Object value)
Returns a newQueryParams
with the specified parameter.static QueryParams
of(String name1, Object value1, String name2, Object value2)
Returns a newQueryParams
with the specified parameters.static QueryParams
of(String name1, Object value1, String name2, Object value2, String name3, Object value3)
Returns a newQueryParams
with the specified parameters.static QueryParams
of(String name1, Object value1, String name2, Object value2, String name3, Object value3, String name4, Object value4)
Returns a newQueryParams
with the specified parameters.static QueryParams
of(String name, String value)
Returns a newQueryParams
with the specified parameter.static QueryParams
of(String name1, String value1, String name2, String value2)
Returns a newQueryParams
with the specified parameters.static QueryParams
of(String name1, String value1, String name2, String value2, String name3, String value3)
Returns a newQueryParams
with the specified parameters.static QueryParams
of(String name1, String value1, String name2, String value2, String name3, String value3, String name4, String value4)
Returns a newQueryParams
with the specified parameters.int
size()
Returns the number of parameters.default Stream<Map.Entry<String,String>>
stream()
Returns aStream
that yields all parameter entries.QueryParamsBuilder
toBuilder()
Returns a new builder created from the entries of this parameters.default String
toQueryString()
Encodes all parameter entries into a query string, as defined in 4.10.22.6, HTML5 W3C Recommendation.Iterator<String>
valueIterator(String name)
Returns anIterator
that yields all values of the parameters with the specifiedname
.default Stream<String>
valueStream(String name)
Returns aStream
that yields all values of the parameters with the specifiedname
.default QueryParams
withMutations(Consumer<QueryParamsBuilder> mutator)
Returns new parameters which is the result from the mutation by the specifiedConsumer
.
-
Method Details
-
builder
Returns a new empty builder. -
of
Returns an emptyQueryParams
. -
of
Returns a newQueryParams
with the specified parameter. -
of
Returns a newQueryParams
with the specified parameter. The value is converted into aString
as explained in Specifying a non-String parameter value. -
of
Returns a newQueryParams
with the specified parameters. -
of
Returns a newQueryParams
with the specified parameters. The values are converted intoString
s as explained in Specifying a non-String parameter value. -
of
static QueryParams of(String name1, String value1, String name2, String value2, String name3, String value3)Returns a newQueryParams
with the specified parameters. -
of
static QueryParams of(String name1, Object value1, String name2, Object value2, String name3, Object value3)Returns a newQueryParams
with the specified parameters. The values are converted intoString
s as explained in Specifying a non-String parameter value. -
of
static QueryParams of(String name1, String value1, String name2, String value2, String name3, String value3, String name4, String value4)Returns a newQueryParams
with the specified parameters. -
of
static QueryParams of(String name1, Object value1, String name2, Object value2, String name3, Object value3, String name4, Object value4)Returns a newQueryParams
with the specified parameters. The values are converted intoString
s as explained in Specifying a non-String parameter value. -
fromQueryString
Decodes the specified query string into aQueryParams
, as defined in 4.10.22.6, HTML5 W3C Recommendation.- Parameters:
queryString
- the query string without leading question mark ('?'
).- Returns:
- the decoded
QueryParams
. An emptyQueryParams
is returned ifqueryString
isnull
.
-
fromQueryString
Decodes the specified query string into aQueryParams
, as defined in 4.10.22.6, HTML5 W3C Recommendation.- Parameters:
queryString
- the query string without leading question mark ('?'
).maxParams
- the max number of parameters to decode. If thequeryString
contains more parameters than this value, the extra parameters will not be decoded.- Returns:
- the decoded
QueryParams
. An emptyQueryParams
is returned ifqueryString
isnull
.
-
fromQueryString
Decodes the specified query string into aQueryParams
, as defined in 4.10.22.6, HTML5 W3C Recommendation.- Parameters:
queryString
- the query string without leading question mark ('?'
).semicolonAsSeparator
- whether to treat a semicolon (';'
) as a separator as well as an ampersand ('&'
). Note that HTML5 expects you to use only ampersand as a separator. Enable this flag only when you need to interop with a legacy system.- Returns:
- the decoded
QueryParams
. An emptyQueryParams
is returned ifqueryString
isnull
.
-
fromQueryString
static QueryParams fromQueryString(@Nullable String queryString, int maxParams, boolean semicolonAsSeparator)Decodes the specified query string into aQueryParams
, as defined in 4.10.22.6, HTML5 W3C Recommendation.- Parameters:
queryString
- the query string without leading question mark ('?'
).maxParams
- the max number of parameters to decode. If thequeryString
contains more parameters than this value, the extra parameters will not be decoded.semicolonAsSeparator
- whether to treat a semicolon (';'
) as a separator as well as an ampersand ('&'
). Note that HTML5 expects you to use only ampersand as a separator. Enable this flag only when you need to interop with a legacy system.- Returns:
- the decoded
QueryParams
. An emptyQueryParams
is returned ifqueryString
isnull
.
-
toBuilder
QueryParamsBuilder toBuilder()Returns a new builder created from the entries of this parameters.- See Also:
withMutations(Consumer)
-
withMutations
Returns new parameters which is the result from the mutation by the specifiedConsumer
. This method is a shortcut for:builder = toBuilder(); mutator.accept(builder); return builder.build();
- See Also:
toBuilder()
-
get
Returns the value of a parameter with the specifiedname
. If there are more than one value for the specifiedname
, the first value in insertion order is returned.- Parameters:
name
- the parameter name- Returns:
- the first parameter value if found, or
null
if there is no such parameter
-
get
Returns the value of a parameter with the specifiedname
. If there are more than one value for the specifiedname
, the first value in insertion order is returned.- Parameters:
name
- the parameter namedefaultValue
- the default value- Returns:
- the first parameter value, or
defaultValue
if there is no such parameter
-
getAll
Returns all values for the parameter with the specified name. The returnedList
can't be modified. -
getInt
Returns theint
value of a parameter with the specifiedname
. If there are more than one value for the specifiedname
, the first value in insertion order is returned.- Parameters:
name
- the parameter name- Returns:
- the
int
value of the first value in insertion order ornull
if there is no such parameter or it can't be converted toint
.
-
getInt
Returns theint
value of a parameter with the specifiedname
. If there are more than one value for the specifiedname
, the first value in insertion order is returned.- Parameters:
name
- the parameter namedefaultValue
- the default value- Returns:
- the
int
value of the first value in insertion order ordefaultValue
if there is no such parameter or it can't be converted toint
.
-
getLong
Returns thelong
value of a parameter with the specifiedname
. If there are more than one value for the specifiedname
, the first value in insertion order is returned.- Parameters:
name
- the parameter name- Returns:
- the
long
value of the first value in insertion order ornull
if there is no such parameter or it can't be converted tolong
.
-
getLong
Returns thelong
value of a parameter with the specifiedname
. If there are more than one value for the specifiedname
, the first value in insertion order is returned.- Parameters:
name
- the parameter namedefaultValue
- the default value- Returns:
- the
long
value of the first value in insertion order ordefaultValue
if there is no such parameter or it can't be converted tolong
.
-
getFloat
Returns thefloat
value of a parameter with the specifiedname
. If there are more than one value for the specifiedname
, the first value in insertion order is returned.- Parameters:
name
- the parameter name- Returns:
- the
float
value of the first value in insertion order ornull
if there is no such parameter or it can't be converted tofloat
.
-
getFloat
Returns thefloat
value of a parameter with the specifiedname
. If there are more than one value for the specifiedname
, the first value in insertion order is returned.- Parameters:
name
- the parameter namedefaultValue
- the default value- Returns:
- the
float
value of the first value in insertion order ordefaultValue
if there is no such parameter or it can't be converted tofloat
.
-
getDouble
Returns thedouble
value of a parameter with the specifiedname
. If there are more than one value for the specifiedname
, the first value in insertion order is returned.- Parameters:
name
- the parameter name- Returns:
- the
double
value of the first value in insertion order ornull
if there is no such parameter or it can't be converted todouble
.
-
getDouble
Returns thedouble
value of a parameter with the specifiedname
. If there are more than one value for the specifiedname
, the first value in insertion order is returned.- Parameters:
name
- the parameter namedefaultValue
- the default value- Returns:
- the
double
value of the first value in insertion order ordefaultValue
if there is no such parameter or it can't be converted todouble
.
-
getTimeMillis
Returns the value of a parameter with the specifiedname
in milliseconds. If there are more than one value for the specifiedname
, the first value in insertion order is returned.- Parameters:
name
- the parameter name- Returns:
- the milliseconds value of the first value in insertion order or
null
if there is no such parameter or it can't be converted to milliseconds.
-
getTimeMillis
Returns the value of a parameter with the specifiedname
in milliseconds. If there are more than one value for the specifiedname
, the first value in insertion order is returned.- Parameters:
name
- the parameter namedefaultValue
- the default value- Returns:
- the milliseconds value of the first value in insertion order or
defaultValue
if there is no such parameter or it can't be converted to milliseconds.
-
contains
Returnstrue
if a parameter with thename
exists,false
otherwise.- Parameters:
name
- the parameter name
-
contains
Returnstrue
if a parameter with thename
andvalue
exists.- Parameters:
name
- the parameter namevalue
- the parameter value to find
-
containsObject
Returnstrue
if a parameter with thename
andvalue
exists.- Parameters:
name
- the parameter namevalue
- the parameter value- Returns:
true
if the parameter exists.false
otherwise
-
containsInt
Returnstrue
if a parameter with thename
andvalue
exists.- Parameters:
name
- the parameter namevalue
- the parameter value- Returns:
true
if the parameter exists.false
otherwise
-
containsLong
Returnstrue
if a parameter with thename
andvalue
exists.- Parameters:
name
- the parameter namevalue
- the parameter value- Returns:
true
if the parameter exists.false
otherwise
-
containsFloat
Returnstrue
if a parameter with thename
andvalue
exists.- Parameters:
name
- the parameter namevalue
- the parameter value- Returns:
true
if the parameter exists.false
otherwise
-
containsDouble
Returnstrue
if a parameter with thename
andvalue
exists.- Parameters:
name
- the parameter namevalue
- the parameter value- Returns:
true
if the parameter exists.false
otherwise
-
containsTimeMillis
Returnstrue
if a parameter with thename
andvalue
exists.- Parameters:
name
- the parameter namevalue
- the parameter value- Returns:
true
if the parameter exists.false
otherwise
-
size
int size()Returns the number of parameters. -
isEmpty
boolean isEmpty()Returnstrue
if this parameters does not contain any entries. -
names
-
iterator
Returns anIterator
that yields all parameter entries. -
valueIterator
Returns anIterator
that yields all values of the parameters with the specifiedname
. -
forEach
Invokes the specifiedaction
for all parameter entries. -
forEachValue
Invokes the specifiedaction
for all values of the parameters with the specifiedname
. -
stream
Returns aStream
that yields all parameter entries. -
valueStream
Returns aStream
that yields all values of the parameters with the specifiedname
. -
toQueryString
Encodes all parameter entries into a query string, as defined in 4.10.22.6, HTML5 W3C Recommendation.- Returns:
- the encoded query string.
-
appendQueryString
Encodes all parameter entries into a query string, as defined in 4.10.22.6, HTML5 W3C Recommendation, and appends it into the specifiedStringBuilder
.- Returns:
- the specified
StringBuilder
for method chaining.
-