public interface QueryParams
QueryParamsYou 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);
QueryParams from an existing oneYou 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);
String parameter valueCertain 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 and MediaType
toString()"42", "string", "text/plain; charset=utf-8"CacheControl
asHeaderValue()"no-cache, no-store, must-revalidate"Instant, TemporalAccessor, Date and Calendar
Sun, 27 Nov 2016 19:37:15 UTCtoString()QueryParams.of() factory methods
QueryParams params = QueryParams.of("the-number", 42,
"the-media-type", MediaType.JSON_UTF_8,
"the-date", Instant.now());
QueryParamsBuilder
QueryParams params =
QueryParams.builder()
.setObject("the-number", 42)
.setObject("the-media-type", MediaType.JSON_UTF_8)
.setObject("the-date", Instant.now())
.build();
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();
| Modifier and Type | Method and 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 specified
StringBuilder. |
static QueryParamsBuilder |
builder()
Returns a new empty builder.
|
boolean |
contains(String name)
Returns
true if a parameter with the name exists, false otherwise. |
boolean |
contains(String name,
String value)
Returns
true if a parameter with the name and value exists. |
boolean |
containsDouble(String name,
double value)
Returns
true if a parameter with the name and value exists. |
boolean |
containsFloat(String name,
float value)
Returns
true if a parameter with the name and value exists. |
boolean |
containsInt(String name,
int value)
Returns
true if a parameter with the name and value exists. |
boolean |
containsLong(String name,
long value)
Returns
true if a parameter with the name and value exists. |
boolean |
containsObject(String name,
Object value)
Returns
true if a parameter with the name and value exists. |
boolean |
containsTimeMillis(String name,
long value)
Returns
true if a parameter with the name and value exists. |
void |
forEach(BiConsumer<String,String> action)
Invokes the specified
action for all parameter entries. |
void |
forEachValue(String name,
Consumer<String> action)
Invokes the specified
action for all values of the parameters with the specified name. |
static QueryParams |
fromQueryString(String queryString)
Decodes the specified query string into a
QueryParams, as defined in
4.10.22.6,
HTML5 W3C Recommendation. |
static QueryParams |
fromQueryString(String queryString,
boolean semicolonAsSeparator)
Decodes the specified query string into a
QueryParams, as defined in
4.10.22.6,
HTML5 W3C Recommendation. |
static QueryParams |
fromQueryString(String queryString,
int maxParams)
Decodes the specified query string into a
QueryParams, 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 a
QueryParams, as defined in
4.10.22.6,
HTML5 W3C Recommendation. |
String |
get(String name)
Returns the value of a parameter with the specified
name. |
String |
get(String name,
String defaultValue)
Returns the value of a parameter with the specified
name. |
List<String> |
getAll(String name)
Returns all values for the parameter with the specified name.
|
Double |
getDouble(String name)
Returns the
double value of a parameter with the specified name. |
double |
getDouble(String name,
double defaultValue)
Returns the
double value of a parameter with the specified name. |
Float |
getFloat(String name)
Returns the
float value of a parameter with the specified name. |
float |
getFloat(String name,
float defaultValue)
Returns the
float value of a parameter with the specified name. |
Integer |
getInt(String name)
Returns the
int value of a parameter with the specified name. |
int |
getInt(String name,
int defaultValue)
Returns the
int value of a parameter with the specified name. |
Long |
getLong(String name)
Returns the
long value of a parameter with the specified name. |
long |
getLong(String name,
long defaultValue)
Returns the
long value of a parameter with the specified name. |
Long |
getTimeMillis(String name)
Returns the value of a parameter with the specified
name in milliseconds. |
long |
getTimeMillis(String name,
long defaultValue)
Returns the value of a parameter with the specified
name in milliseconds. |
boolean |
isEmpty()
Returns
true if this parameters does not contain any entries. |
Iterator<Map.Entry<String,String>> |
iterator()
Returns an
Iterator that yields all parameter entries. |
Set<String> |
names()
Returns a
Set of all parameter names. |
static QueryParams |
of()
Returns an empty
QueryParams. |
static QueryParams |
of(String name,
Object value)
Returns a new
QueryParams with the specified parameter. |
static QueryParams |
of(String name1,
Object value1,
String name2,
Object value2)
Returns a new
QueryParams with the specified parameters. |
static QueryParams |
of(String name1,
Object value1,
String name2,
Object value2,
String name3,
Object value3)
Returns a new
QueryParams 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 new
QueryParams with the specified parameters. |
static QueryParams |
of(String name,
String value)
Returns a new
QueryParams with the specified parameter. |
static QueryParams |
of(String name1,
String value1,
String name2,
String value2)
Returns a new
QueryParams with the specified parameters. |
static QueryParams |
of(String name1,
String value1,
String name2,
String value2,
String name3,
String value3)
Returns a new
QueryParams 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 new
QueryParams with the specified parameters. |
int |
size()
Returns the number of parameters.
|
default Stream<Map.Entry<String,String>> |
stream()
Returns a
Stream 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 an
Iterator that yields all values of the parameters with the specified name. |
default Stream<String> |
valueStream(String name)
Returns a
Stream that yields all values of the parameters with the specified name. |
default QueryParams |
withMutations(Consumer<QueryParamsBuilder> mutator)
Returns a new parameters which is the result from the mutation by the specified
Consumer. |
forEach, spliteratorstatic QueryParamsBuilder builder()
static QueryParams of()
QueryParams.static QueryParams of(String name, String value)
QueryParams with the specified parameter.static QueryParams of(String name, Object value)
QueryParams with the specified parameter. The value is converted into
a String as explained in Specifying a non-String parameter value.static QueryParams of(String name1, String value1, String name2, String value2)
QueryParams with the specified parameters.static QueryParams of(String name1, Object value1, String name2, Object value2)
QueryParams with the specified parameters. The values are converted into
Strings as explained in Specifying a non-String parameter value.static QueryParams of(String name1, String value1, String name2, String value2, String name3, String value3)
QueryParams with the specified parameters.static QueryParams of(String name1, Object value1, String name2, Object value2, String name3, Object value3)
QueryParams with the specified parameters. The values are converted into
Strings as explained in Specifying a non-String parameter value.static QueryParams of(String name1, String value1, String name2, String value2, String name3, String value3, String name4, String value4)
QueryParams with the specified parameters.static QueryParams of(String name1, Object value1, String name2, Object value2, String name3, Object value3, String name4, Object value4)
QueryParams with the specified parameters. The values are converted into
Strings as explained in Specifying a non-String parameter value.static QueryParams fromQueryString(String queryString)
QueryParams, as defined in
4.10.22.6,
HTML5 W3C Recommendation.queryString - the query string with or without leading question mark ('?').QueryParams. An empty QueryParams is returned
if queryString is null.static QueryParams fromQueryString(String queryString, int maxParams)
QueryParams, as defined in
4.10.22.6,
HTML5 W3C Recommendation.queryString - the query string with or without leading question mark ('?').maxParams - the max number of parameters to decode. If the queryString contains
more parameters than this value, the extra parameters will not be decoded.QueryParams. An empty QueryParams is returned
if queryString is null.static QueryParams fromQueryString(String queryString, boolean semicolonAsSeparator)
QueryParams, as defined in
4.10.22.6,
HTML5 W3C Recommendation.queryString - the query string with or 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.QueryParams. An empty QueryParams is returned
if queryString is null.static QueryParams fromQueryString(String queryString, int maxParams, boolean semicolonAsSeparator)
QueryParams, as defined in
4.10.22.6,
HTML5 W3C Recommendation.queryString - the query string with or without leading question mark ('?').maxParams - the max number of parameters to decode. If the queryString 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.QueryParams. An empty QueryParams is returned
if queryString is null.QueryParamsBuilder toBuilder()
withMutations(Consumer)default QueryParams withMutations(Consumer<QueryParamsBuilder> mutator)
Consumer.
This method is a shortcut for:
builder = toBuilder();
mutator.accept(builder);
return builder.build();
toBuilder()String get(String name)
name. If there are more than one value for
the specified name, the first value in insertion order is returned.name - the parameter namenull if there is no such parameterString get(String name, String defaultValue)
name. If there are more than one value for
the specified name, the first value in insertion order is returned.name - the parameter namedefaultValue - the default valuedefaultValue if there is no such parameterList<String> getAll(String name)
List can't be
modified.Integer getInt(String name)
int value of a parameter with the specified name. If there are more than one
value for the specified name, the first value in insertion order is returned.name - the parameter nameint value of the first value in insertion order or null if there is no such
parameter or it can't be converted to int.int getInt(String name, int defaultValue)
int value of a parameter with the specified name. If there are more than one
value for the specified name, the first value in insertion order is returned.name - the parameter namedefaultValue - the default valueint value of the first value in insertion order or defaultValue if there is
no such parameter or it can't be converted to int.Long getLong(String name)
long value of a parameter with the specified name. If there are more than
one value for the specified name, the first value in insertion order is returned.name - the parameter namelong value of the first value in insertion order or null if there is no such
parameter or it can't be converted to long.long getLong(String name, long defaultValue)
long value of a parameter with the specified name. If there are more than
one value for the specified name, the first value in insertion order is returned.name - the parameter namedefaultValue - the default valuelong value of the first value in insertion order or defaultValue if there is
no such parameter or it can't be converted to long.Float getFloat(String name)
float value of a parameter with the specified name. If there are more than
one value for the specified name, the first value in insertion order is returned.name - the parameter namefloat value of the first value in insertion order or null if there is no
such parameter or it can't be converted to float.float getFloat(String name, float defaultValue)
float value of a parameter with the specified name. If there are more than
one value for the specified name, the first value in insertion order is returned.name - the parameter namedefaultValue - the default valuefloat value of the first value in insertion order or defaultValue if there
is no such parameter or it can't be converted to float.Double getDouble(String name)
double value of a parameter with the specified name. If there are more than
one value for the specified name, the first value in insertion order is returned.name - the parameter namedouble value of the first value in insertion order or null if there is no
such parameter or it can't be converted to double.double getDouble(String name, double defaultValue)
double value of a parameter with the specified name. If there are more than
one value for the specified name, the first value in insertion order is returned.name - the parameter namedefaultValue - the default valuedouble value of the first value in insertion order or defaultValue if there
is no such parameter or it can't be converted to double.Long getTimeMillis(String name)
name in milliseconds. If there are more than
one value for the specified name, the first value in insertion order is returned.name - the parameter namenull if there is no such
parameter or it can't be converted to milliseconds.long getTimeMillis(String name, long defaultValue)
name in milliseconds. If there are more than
one value for the specified name, the first value in insertion order is returned.name - the parameter namedefaultValue - the default valuedefaultValue if there is
no such parameter or it can't be converted to milliseconds.boolean contains(String name)
true if a parameter with the name exists, false otherwise.name - the parameter nameboolean contains(String name, String value)
true if a parameter with the name and value exists.name - the parameter namevalue - the parameter value to findboolean containsObject(String name, Object value)
true if a parameter with the name and value exists.name - the parameter namevalue - the parameter valuetrue if the parameter exists. false otherwiseboolean containsInt(String name, int value)
true if a parameter with the name and value exists.name - the parameter namevalue - the parameter valuetrue if the parameter exists. false otherwiseboolean containsLong(String name, long value)
true if a parameter with the name and value exists.name - the parameter namevalue - the parameter valuetrue if the parameter exists. false otherwiseboolean containsFloat(String name, float value)
true if a parameter with the name and value exists.name - the parameter namevalue - the parameter valuetrue if the parameter exists. false otherwiseboolean containsDouble(String name, double value)
true if a parameter with the name and value exists.name - the parameter namevalue - the parameter valuetrue if the parameter exists. false otherwiseboolean containsTimeMillis(String name, long value)
true if a parameter with the name and value exists.name - the parameter namevalue - the parameter valuetrue if the parameter exists. false otherwiseint size()
boolean isEmpty()
true if this parameters does not contain any entries.Iterator<Map.Entry<String,String>> iterator()
Iterator that yields all parameter entries.Iterator<String> valueIterator(String name)
Iterator that yields all values of the parameters with the specified name.void forEach(BiConsumer<String,String> action)
action for all parameter entries.void forEachValue(String name, Consumer<String> action)
action for all values of the parameters with the specified name.default Stream<Map.Entry<String,String>> stream()
Stream that yields all parameter entries.default Stream<String> valueStream(String name)
Stream that yields all values of the parameters with the specified name.default String toQueryString()
default StringBuilder appendQueryString(StringBuilder buf)
StringBuilder.StringBuilder for method chaining.Copyright © 2020 LeanCloud. All rights reserved.