1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
package ai.vespa.hosted.cd;
import java.util.Map;
import java.util.stream.Stream;
import static java.util.Map.copyOf;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toUnmodifiableMap;
/**
* An immutable query to send to a Vespa {@link Endpoint}, to receive a {@link Search}.
*
* @author jonmv
*/
public class Query {
private final String rawQuery;
private final Map<String, String> parameters;
private Query(String rawQuery, Map<String, String> parameters) {
this.rawQuery = rawQuery;
this.parameters = parameters;
}
/** Creates a query with the given raw query part. */
public static Query ofRaw(String rawQuery) {
if (rawQuery.isBlank())
throw new IllegalArgumentException("Query can not be blank.");
return new Query(rawQuery,
Stream.of(rawQuery.split("&"))
.map(pair -> pair.split("="))
.collect(toUnmodifiableMap(pair -> pair[0], pair -> pair[1])));
}
/** Creates a query with the given name-value pairs. */
public static Query ofParameters(Map<String, String> parameters) {
if (parameters.isEmpty())
throw new IllegalArgumentException("Parameters can not be empty.");
return new Query(parameters.entrySet().stream()
.map(entry -> entry.getKey() + "=" + entry.getValue())
.collect(joining("&")),
copyOf(parameters));
}
/** Returns a copy of this with the given name-value pair added, potentially overriding any current value. */
public Query withParameter(String name, String value) {
return ofParameters(Stream.concat(parameters.entrySet().stream().filter(entry -> ! entry.getKey().equals(name)),
Stream.of(Map.entry(name, value)))
.collect(toUnmodifiableMap(Map.Entry::getKey, Map.Entry::getValue)));
}
/** Returns the raw string representation of this query. */
public String rawQuery() { return rawQuery; }
/** Returns the parameters of this query. */
public Map<String, String> parameters() { return parameters; }
}
|