Map<String, String> for HTTP request parameters that preserves multiple values
per key (e.g. repeated query parameters such as match[]=foo&match[]=bar).
Each key maps to a non-empty ordered list of values. The standard Map interface
operates on the last value in that list: get(Object) returns the last value for a key, or null if absent,
and put(String, String) sets a key to a single value (stored as a one-element list, so getAll(String) returns
a singleton list after a put).
Use getAll(String) to retrieve all values for a repeated key.
The value lists returned by getAll(String) are always non-empty and immutable, so
List.getFirst() and List.getLast() are always safe to call on a non-null result.
-
Nested Class Summary
Nested classes/interfaces inherited from class java.util.AbstractMap
AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K, V> -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()booleancontainsKey(Object key) static RequestParamscopyOf(RequestParams source) Returns an independent copy ofsource, preserving all multi-values.static RequestParamsempty()Returns an emptyRequestParamsinstance.entrySet()Returns an entry set where each entry's value is the last value for that key.static RequestParamsParses the query string from aURIinto aRequestParams, preserving all values for repeated parameters.static RequestParamsfromQueryString(String queryString) Parses a URL-encoded query string into aRequestParams, preserving all values for repeated parameters (e.g.static RequestParamsfromSingleValues(Map<String, String> singleValues) Creates aRequestParamsfrom a plain single-value map.static RequestParamsParses the query string from a URI string into aRequestParams, preserving all values for repeated parameters.Returns the last value associated withkey, ornullif absent.Returns all values forkeyin the order they were added, or an empty list if the key is absent.keySet()Setskeyto a singlevalue, replacing any previous values for that key.voidputAll(RequestParams source) Copies all entries fromsourceinto this map, preserving all multi-values.requireSingle(String key) Returns the single value forkey, ornullif absent.intsize()Methods inherited from class java.util.AbstractMap
clone, containsValue, equals, hashCode, isEmpty, putAll, toString, valuesMethods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
-
Method Details
-
empty
Returns an emptyRequestParamsinstance. -
fromQueryString
Parses a URL-encoded query string into aRequestParams, preserving all values for repeated parameters (e.g.match[]=foo&match[]=bar→["foo", "bar"]).- Parameters:
queryString- the raw query string (the part after?, without the?itself)- Returns:
- a
RequestParamsfrom parameter name to all its values, in encounter order - Throws:
IllegalArgumentException- if the query string cannot be decoded
-
fromUri
Parses the query string from a URI string into aRequestParams, preserving all values for repeated parameters. Returns an empty map if the URI contains no?.- Parameters:
uri- a URI string, e.g./index/_search?pretty&size=10- Returns:
- a
RequestParamsfrom parameter name to all its values, in encounter order - Throws:
IllegalArgumentException- if the query string cannot be decoded
-
from
Parses the query string from aURIinto aRequestParams, preserving all values for repeated parameters. Returns an empty map if the URI has no query.- Parameters:
uri- the URI whose raw query string is parsed- Returns:
- a
RequestParamsfrom parameter name to all its values, in encounter order - Throws:
IllegalArgumentException- if the query string cannot be decoded
-
fromSingleValues
Creates aRequestParamsfrom a plain single-value map. Each value is wrapped in a singleton list so thatgetAll(String)returns a one-element list rather than an empty one.- Parameters:
singleValues- a map whose values are treated as the sole value for each key
-
copyOf
Returns an independent copy ofsource, preserving all multi-values.- Parameters:
source- theRequestParamsto copy- Returns:
- a new mutable
RequestParamswith the same contents assource
-
putAll
Copies all entries fromsourceinto this map, preserving all multi-values. Unlike the inheritedAbstractMap.putAll(Map), which only sees the last value per key, this overload copies every value in each key's list. -
getAll
Returns all values forkeyin the order they were added, or an empty list if the key is absent.- Parameters:
key- the parameter name- Returns:
- an unmodifiable non-empty list of all values, or an empty list if absent; never
null
-
requireSingle
Returns the single value forkey, ornullif absent.- Parameters:
key- the parameter name- Returns:
- the single value, or
nullif absent - Throws:
RestRequest.BadParameterException- if the key has multiple values
-
get
Returns the last value associated withkey, ornullif absent.When a query parameter appears multiple times (e.g.
a=1&a=2), this method returns the last value ("2"). UsegetAll(String)to retrieve all values, orrequireSingle(String)to assert that only one value is present. -
put
Setskeyto a singlevalue, replacing any previous values for that key. The value is stored as a one-element immutable list, sogetAll(String)will return a singleton list andList.getFirst()/List.getLast()remain safe to call. -
size
public int size() -
remove
-
clear
public void clear() -
containsKey
- Specified by:
containsKeyin interfaceMap<String,String> - Overrides:
containsKeyin classAbstractMap<String,String>
-
keySet
-
entrySet
Returns an entry set where each entry's value is the last value for that key. Supports removal via the iterator, which removes the entire key from the underlying map.
-