This repository was archived by the owner on Jun 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathRestUriBuilder.java
83 lines (72 loc) · 2.82 KB
/
RestUriBuilder.java
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package io.rocketbase.toggl.api;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import java.util.*;
public class RestUriBuilder {
private String host;
private String protocol = "https";
private StringBuilder path;
private Map<String, List<String>> parameters = new HashMap<String, List<String>>();
public RestUriBuilder host(String host) {
Preconditions.checkNotNull(host);
this.host = host;
return this;
}
public RestUriBuilder protocol(String protocol) {
Preconditions.checkNotNull(protocol);
this.protocol = protocol;
return this;
}
public RestUriBuilder path(String path) {
Preconditions.checkNotNull(path);
this.path = new StringBuilder();
this.path.append(path);
return this;
}
public RestUriBuilder appendPath(String path) {
Preconditions.checkNotNull(path);
this.path.append(path);
return this;
}
public RestUriBuilder addParameter(String key, Object value) {
return addParameters(key, Collections.singletonList(value));
}
public RestUriBuilder addParameters(String key, Collection<?> value) {
Preconditions.checkNotNull(key);
Preconditions.checkNotNull(value);
Iterable<String> stringValues = Iterables.transform(value, new Function<Object, String>() {
public String apply(Object o) {
return String.valueOf(o);
}
});
this.parameters.put(key, Lists.newArrayList(stringValues));
return this;
}
public String build() {
StringBuilder builder = new StringBuilder();
builder.append(protocol);
builder.append("://");
Preconditions.checkNotNull(host);
Preconditions.checkNotNull(path);
builder.append(host);
builder.append(path);
if (!parameters.isEmpty()) {
builder.append("?");
Joiner.on("&")
.appendTo(builder, Iterables.transform(parameters.entrySet(), new Function<Map.Entry<String, List<String>>, String>() {
public String apply(final Map.Entry<String, List<String>> stringStringEntry) {
return Joiner.on("&")
.join(Lists.transform(stringStringEntry.getValue(), new Function<String, String>() {
public String apply(String s) {
return stringStringEntry.getKey() + "=" + s;
}
}));
}
}));
}
return builder.toString();
}
}