Skip to content

Commit e0c2c51

Browse files
committed
Replace Guava Optional with Java Optional
1 parent 1f04f51 commit e0c2c51

35 files changed

+211
-230
lines changed

findbugs-exclude-filter.xml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,4 @@
44
<Match>
55
<Class name="~.*model\.Immutable.*" />
66
</Match>
7-
8-
<!-- A workaround for a bug of findbugs when using Guava Optional.fromNullable(...) -->
9-
<Match>
10-
<Class name="com.treasuredata.client.AbstractTDClientBuilder"/>
11-
<Bugs pattern="NP_NULL_PARAM_DEREF"/>
12-
</Match>
137
</FindBugsFilter>

src/main/java/com/treasuredata/client/AbstractTDClientBuilder.java

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@
1818
*/
1919
package com.treasuredata.client;
2020

21-
import com.google.common.base.Optional;
2221
import com.google.common.collect.ImmutableMultimap;
2322
import com.google.common.collect.Multimap;
2423

2524
import java.util.Properties;
25+
import java.util.Optional;
26+
import java.util.stream.Stream;
2627

2728
import static com.treasuredata.client.TDClientConfig.ENV_TD_CLIENT_APIKEY;
2829
import static com.treasuredata.client.TDClientConfig.Type.APIKEY;
@@ -50,13 +51,13 @@
5051
*/
5152
public abstract class AbstractTDClientBuilder<ClientImpl, BuilderImpl extends AbstractTDClientBuilder<ClientImpl, BuilderImpl>>
5253
{
53-
protected Optional<String> endpoint = Optional.absent();
54-
protected Optional<Integer> port = Optional.absent();
54+
protected Optional<String> endpoint = Optional.empty();
55+
protected Optional<Integer> port = Optional.empty();
5556
protected boolean useSSL = true;
56-
protected Optional<String> apiKey = Optional.absent();
57-
protected Optional<String> user = Optional.absent();
58-
protected Optional<String> password = Optional.absent();
59-
protected Optional<ProxyConfig> proxy = Optional.absent();
57+
protected Optional<String> apiKey = Optional.empty();
58+
protected Optional<String> user = Optional.empty();
59+
protected Optional<String> password = Optional.empty();
60+
protected Optional<ProxyConfig> proxy = Optional.empty();
6061
protected BackOffStrategy retryStrategy = BackOffStrategy.FullJitter;
6162
protected int retryLimit = 7;
6263
protected int retryInitialIntervalMillis = 500;
@@ -74,7 +75,7 @@ private static Optional<String> getConfigProperty(Properties p, TDClientConfig.T
7475

7576
private static Optional<String> getConfigProperty(Properties p, String key)
7677
{
77-
return Optional.fromNullable(p.getProperty(key));
78+
return Optional.ofNullable(p.getProperty(key));
7879
}
7980

8081
private static Optional<Integer> getConfigPropertyInt(Properties p, TDClientConfig.Type key)
@@ -94,7 +95,7 @@ private static Optional<Integer> getConfigPropertyInt(Properties p, String key)
9495
}
9596
}
9697
else {
97-
return Optional.absent();
98+
return Optional.empty();
9899
}
99100
}
100101

@@ -115,7 +116,7 @@ private static Optional<Boolean> getConfigPropertyBoolean(Properties p, String k
115116
}
116117
}
117118
else {
118-
return Optional.absent();
119+
return Optional.empty();
119120
}
120121
}
121122

@@ -131,7 +132,7 @@ private static Optional<Double> getConfigPropertyDouble(Properties p, TDClientCo
131132
}
132133
}
133134
else {
134-
return Optional.absent();
135+
return Optional.empty();
135136
}
136137
}
137138

@@ -168,24 +169,25 @@ protected AbstractTDClientBuilder(boolean loadTDConf)
168169
*/
169170
public BuilderImpl setProperties(Properties p)
170171
{
171-
this.endpoint = getConfigProperty(p, API_ENDPOINT)
172-
.or(getConfigProperty(p, "endpoint"))
173-
.or(endpoint);
174-
this.port = getConfigPropertyInt(p, API_PORT)
175-
.or(getConfigPropertyInt(p, "port"))
176-
.or(port);
177-
this.useSSL = getConfigPropertyBoolean(p, USESSL)
178-
.or(getConfigPropertyBoolean(p, "usessl"))
179-
.or(useSSL);
180-
this.apiKey = getConfigProperty(p, APIKEY)
181-
.or(getConfigProperty(p, "apikey"))
182-
.or(apiKey);
183-
this.user = getConfigProperty(p, USER)
184-
.or(getConfigProperty(p, "user"))
185-
.or(user);
186-
this.password = getConfigProperty(p, PASSOWRD)
187-
.or(getConfigProperty(p, "password"))
188-
.or(password);
172+
this.endpoint = Stream.of(getConfigProperty(p, API_ENDPOINT), getConfigProperty(p, "endpoint"), endpoint)
173+
.flatMap((opt) -> opt.map(Stream::of).orElseGet(Stream::empty))
174+
.findFirst();
175+
this.port = Stream.of(getConfigPropertyInt(p, API_PORT), getConfigPropertyInt(p, "port"), port)
176+
.flatMap((opt) -> opt.map(Stream::of).orElseGet(Stream::empty))
177+
.findFirst();
178+
this.useSSL = Stream.of(getConfigPropertyBoolean(p, USESSL), getConfigPropertyBoolean(p, "usessl"))
179+
.flatMap((opt) -> opt.map(Stream::of).orElseGet(Stream::empty))
180+
.findFirst()
181+
.orElse(useSSL);
182+
this.apiKey = Stream.of(getConfigProperty(p, APIKEY), getConfigProperty(p, "apikey"), apiKey)
183+
.flatMap((opt) -> opt.map(Stream::of).orElseGet(Stream::empty))
184+
.findFirst();
185+
this.user = Stream.of(getConfigProperty(p, USER), getConfigProperty(p, "user"), user)
186+
.flatMap((opt) -> opt.map(Stream::of).orElseGet(Stream::empty))
187+
.findFirst();
188+
this.password = Stream.of(getConfigProperty(p, PASSOWRD), getConfigProperty(p, "password"), password)
189+
.flatMap((opt) -> opt.map(Stream::of).orElseGet(Stream::empty))
190+
.findFirst();
189191

190192
// proxy
191193
boolean hasProxy = false;
@@ -222,16 +224,16 @@ public BuilderImpl setProperties(Properties p)
222224
hasProxy = true;
223225
proxyConfig.setPassword(proxyPassword.get());
224226
}
225-
this.proxy = Optional.fromNullable(hasProxy ? proxyConfig.createProxyConfig() : null);
227+
this.proxy = Optional.ofNullable(hasProxy ? proxyConfig.createProxyConfig() : null);
226228

227229
// http client parameter
228-
this.retryLimit = getConfigPropertyInt(p, RETRY_LIMIT).or(retryLimit);
229-
this.retryInitialIntervalMillis = getConfigPropertyInt(p, RETRY_INITIAL_INTERVAL_MILLIS).or(retryInitialIntervalMillis);
230-
this.retryMaxIntervalMillis = getConfigPropertyInt(p, RETRY_MAX_INTERVAL_MILLIS).or(retryMaxIntervalMillis);
231-
this.retryMultiplier = getConfigPropertyDouble(p, RETRY_MULTIPLIER).or(retryMultiplier);
232-
this.connectTimeoutMillis = getConfigPropertyInt(p, CONNECT_TIMEOUT_MILLIS).or(connectTimeoutMillis);
233-
this.readTimeoutMillis = getConfigPropertyInt(p, READ_TIMEOUT_MILLIS).or(readTimeoutMillis);
234-
this.connectionPoolSize = getConfigPropertyInt(p, CONNECTION_POOL_SIZE).or(connectionPoolSize);
230+
this.retryLimit = getConfigPropertyInt(p, RETRY_LIMIT).orElse(retryLimit);
231+
this.retryInitialIntervalMillis = getConfigPropertyInt(p, RETRY_INITIAL_INTERVAL_MILLIS).orElse(retryInitialIntervalMillis);
232+
this.retryMaxIntervalMillis = getConfigPropertyInt(p, RETRY_MAX_INTERVAL_MILLIS).orElse(retryMaxIntervalMillis);
233+
this.retryMultiplier = getConfigPropertyDouble(p, RETRY_MULTIPLIER).orElse(retryMultiplier);
234+
this.connectTimeoutMillis = getConfigPropertyInt(p, CONNECT_TIMEOUT_MILLIS).orElse(connectTimeoutMillis);
235+
this.readTimeoutMillis = getConfigPropertyInt(p, READ_TIMEOUT_MILLIS).orElse(readTimeoutMillis);
236+
this.connectionPoolSize = getConfigPropertyInt(p, CONNECTION_POOL_SIZE).orElse(connectionPoolSize);
235237

236238
return self();
237239
}

src/main/java/com/treasuredata/client/ProxyConfig.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,9 @@
1818
*/
1919
package com.treasuredata.client;
2020

21-
import com.google.common.base.Optional;
22-
2321
import java.net.URI;
2422
import java.net.URISyntaxException;
23+
import java.util.Optional;
2524

2625
/**
2726
* Proxy configuration to access TD API
@@ -102,8 +101,8 @@ public static class ProxyConfigBuilder
102101
private String host = "localhost";
103102
private int port = 8080;
104103
private boolean useSSL = false;
105-
private Optional<String> user = Optional.absent();
106-
private Optional<String> password = Optional.absent();
104+
private Optional<String> user = Optional.empty();
105+
private Optional<String> password = Optional.empty();
107106

108107
public ProxyConfigBuilder()
109108
{

src/main/java/com/treasuredata/client/TDApiRequest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
*/
1919
package com.treasuredata.client;
2020

21-
import com.google.common.base.Optional;
2221
import com.google.common.base.Throwables;
2322
import com.google.common.collect.ImmutableMap;
2423
import com.google.common.collect.ImmutableMultimap;
@@ -32,6 +31,7 @@
3231
import java.net.URLEncoder;
3332
import java.util.HashMap;
3433
import java.util.Map;
34+
import java.util.Optional;
3535

3636
import static com.google.common.base.Preconditions.checkNotNull;
3737

@@ -142,12 +142,12 @@ public static class Builder
142142
private String path;
143143
private Map<String, String> queryParams;
144144
private ImmutableMultimap.Builder<String, String> headerParams;
145-
private Optional<String> postJson = Optional.absent();
146-
private Optional<File> file = Optional.absent();
147-
private Optional<byte[]> content = Optional.absent();
145+
private Optional<String> postJson = Optional.empty();
146+
private Optional<File> file = Optional.empty();
147+
private Optional<byte[]> content = Optional.empty();
148148
private int contentOffset;
149149
private int contentLength;
150-
private Optional<Boolean> followRedirects = Optional.absent();
150+
private Optional<Boolean> followRedirects = Optional.empty();
151151

152152
Builder(TDHttpMethod method, String path)
153153
{

src/main/java/com/treasuredata/client/TDClient.java

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import com.fasterxml.jackson.databind.node.ObjectNode;
2525
import com.google.common.annotations.VisibleForTesting;
2626
import com.google.common.base.Function;
27-
import com.google.common.base.Optional;
2827
import com.google.common.base.Throwables;
2928
import com.google.common.collect.ImmutableList;
3029
import com.google.common.collect.ImmutableMap;
@@ -76,6 +75,7 @@
7675
import java.util.HashMap;
7776
import java.util.List;
7877
import java.util.Map;
78+
import java.util.Optional;
7979
import java.util.Properties;
8080
import java.util.regex.Pattern;
8181

@@ -237,13 +237,13 @@ protected <ResultType> ResultType doPost(String path, Map<String, String> queryP
237237
protected <ResultType> ResultType doPost(String path, Class<ResultType> resultTypeClass)
238238
throws TDClientException
239239
{
240-
return this.<ResultType>doPost(path, ImmutableMap.<String, String>of(), Optional.<String>absent(), resultTypeClass);
240+
return this.<ResultType>doPost(path, ImmutableMap.<String, String>of(), Optional.empty(), resultTypeClass);
241241
}
242242

243243
protected <ResultType> ResultType doPost(String path, Map<String, String> queryParam, Class<ResultType> resultTypeClass)
244244
throws TDClientException
245245
{
246-
return this.<ResultType>doPost(path, queryParam, Optional.<String>absent(), resultTypeClass);
246+
return this.<ResultType>doPost(path, queryParam, Optional.empty(), resultTypeClass);
247247
}
248248

249249
protected <ResultType> ResultType doPut(String path, Map<String, String> queryParam, File file, Class<ResultType> resultTypeClass)
@@ -343,7 +343,7 @@ public TDUserList listUsers()
343343
public String serverStatus()
344344
{
345345
// No API key is requried for server_status
346-
return httpClient.call(TDApiRequest.Builder.GET("/v3/system/server_status").build(), Optional.<String>absent());
346+
return httpClient.call(TDApiRequest.Builder.GET("/v3/system/server_status").build(), Optional.empty());
347347
}
348348

349349
@Override
@@ -653,14 +653,10 @@ public String submit(TDJobRequest jobRequest)
653653
doPost(
654654
buildUrl("/v3/job/issue", jobRequest.getType().getType(), jobRequest.getDatabase()),
655655
queryParam,
656-
jobRequest.getConfig().transform(new Function<ObjectNode, String>()
657-
{
658-
public String apply(ObjectNode config)
659-
{
656+
jobRequest.getConfig().map((config) -> {
660657
ObjectNode body = config.objectNode();
661658
body.set("config", config);
662659
return body.toString();
663-
}
664660
}),
665661
TDJobSubmitResult.class);
666662
return result.getJobId();
@@ -780,13 +776,13 @@ public void performBulkImportSession(String sessionName, Optional<String> poolNa
780776
@Override
781777
public void performBulkImportSession(String sessionName, TDJob.Priority priority)
782778
{
783-
performBulkImportSession(sessionName, Optional.absent(), priority);
779+
performBulkImportSession(sessionName, Optional.empty(), priority);
784780
}
785781

786782
@Override
787783
public void performBulkImportSession(String sessionName, Optional<String> poolName, TDJob.Priority priority)
788784
{
789-
Optional<String> jsonBody = Optional.absent();
785+
Optional<String> jsonBody = Optional.empty();
790786
if (poolName.isPresent()) {
791787
jsonBody = Optional.of(JSONObject.toJSONString(ImmutableMap.of("pool_name", poolName.get())));
792788
}
@@ -1061,7 +1057,7 @@ public Optional<TDTableDistribution> tableDistribution(String databaseName, Stri
10611057
return Optional.of(distribution);
10621058
}
10631059
catch (TDClientHttpNotFoundException e) {
1064-
return Optional.absent();
1060+
return Optional.empty();
10651061
}
10661062
}
10671063

src/main/java/com/treasuredata/client/TDClientApi.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@
1818
*/
1919
package com.treasuredata.client;
2020

21+
import java.util.Optional;
22+
2123
import com.google.common.base.Function;
22-
import com.google.common.base.Optional;
2324
import com.google.common.collect.Multimap;
2425
import com.treasuredata.client.model.TDApiKey;
2526
import com.treasuredata.client.model.TDBulkImportSession;

src/main/java/com/treasuredata/client/TDClientConfig.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
package com.treasuredata.client;
2020

2121
import com.fasterxml.jackson.annotation.JsonCreator;
22-
import com.google.common.base.Optional;
2322
import com.google.common.collect.ImmutableList;
2423
import com.google.common.collect.Multimap;
2524
import com.google.common.io.Files;
@@ -33,6 +32,7 @@
3332
import java.net.URISyntaxException;
3433
import java.nio.charset.StandardCharsets;
3534
import java.util.List;
35+
import java.util.Optional;
3636
import java.util.Properties;
3737

3838
/**
@@ -124,7 +124,7 @@ public static List<Type> knownProperties()
124124
int connectionPoolSize,
125125
Multimap<String, String> headers)
126126
{
127-
this.endpoint = endpoint.or("api.treasuredata.com");
127+
this.endpoint = endpoint.orElse("api.treasuredata.com");
128128
this.port = port;
129129
this.useSSL = useSSL;
130130
this.apiKey = apiKey;

src/main/java/com/treasuredata/client/TDClientException.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919
package com.treasuredata.client;
2020

21-
import com.google.common.base.Optional;
21+
import java.util.Optional;
2222

2323
import static com.google.common.base.Preconditions.checkNotNull;
2424

@@ -65,7 +65,7 @@ private static final String formatErrorMessage(ErrorType errorType, String messa
6565

6666
public TDClientException(ErrorType errorType, String message, Optional<Exception> cause)
6767
{
68-
super(formatErrorMessage(errorType, message, cause), cause.orNull());
68+
super(formatErrorMessage(errorType, message, cause), cause.orElse(null));
6969
checkNotNull(errorType, "errorType is null");
7070
checkNotNull(cause, "cause is null");
7171
this.errorType = errorType;
@@ -84,7 +84,7 @@ public TDClientException(ErrorType errorType, Exception cause)
8484

8585
public TDClientException(ErrorType errorType, String message)
8686
{
87-
this(errorType, message, Optional.<Exception>absent());
87+
this(errorType, message, Optional.empty());
8888
}
8989

9090
public ErrorType getErrorType()

src/main/java/com/treasuredata/client/TDClientHttpConflictException.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919
package com.treasuredata.client;
2020

21-
import com.google.common.base.Optional;
21+
import java.util.Optional;
2222

2323
/**
2424
* On 409 conflict error (e.g., database already exists)
@@ -42,6 +42,6 @@ public TDClientHttpConflictException(String errorMessage, String conflictsWith)
4242

4343
public Optional<String> getConflictsWith()
4444
{
45-
return Optional.fromNullable(conflictsWith);
45+
return Optional.ofNullable(conflictsWith);
4646
}
4747
}

src/main/java/com/treasuredata/client/TDClientHttpException.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@
1818
*/
1919
package com.treasuredata.client;
2020

21-
import com.google.common.base.Optional;
22-
2321
import java.util.Date;
22+
import java.util.Optional;
2423

2524
/**
2625
* Exception class for reporting http server status code
@@ -47,7 +46,7 @@ public int getStatusCode()
4746
public Optional<Date> getRetryAfter()
4847
{
4948
if (retryAfter == -1) {
50-
return Optional.absent();
49+
return Optional.empty();
5150
}
5251
else {
5352
return Optional.of(new Date(retryAfter));

0 commit comments

Comments
 (0)