Skip to content

Commit

Permalink
Replace Guava Optional with Java Optional
Browse files Browse the repository at this point in the history
  • Loading branch information
exoego committed May 2, 2022
1 parent 1f04f51 commit e0c2c51
Show file tree
Hide file tree
Showing 35 changed files with 211 additions and 230 deletions.
6 changes: 0 additions & 6 deletions findbugs-exclude-filter.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,4 @@
<Match>
<Class name="~.*model\.Immutable.*" />
</Match>

<!-- A workaround for a bug of findbugs when using Guava Optional.fromNullable(...) -->
<Match>
<Class name="com.treasuredata.client.AbstractTDClientBuilder"/>
<Bugs pattern="NP_NULL_PARAM_DEREF"/>
</Match>
</FindBugsFilter>
76 changes: 39 additions & 37 deletions src/main/java/com/treasuredata/client/AbstractTDClientBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@
*/
package com.treasuredata.client;

import com.google.common.base.Optional;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.Multimap;

import java.util.Properties;
import java.util.Optional;
import java.util.stream.Stream;

import static com.treasuredata.client.TDClientConfig.ENV_TD_CLIENT_APIKEY;
import static com.treasuredata.client.TDClientConfig.Type.APIKEY;
Expand Down Expand Up @@ -50,13 +51,13 @@
*/
public abstract class AbstractTDClientBuilder<ClientImpl, BuilderImpl extends AbstractTDClientBuilder<ClientImpl, BuilderImpl>>
{
protected Optional<String> endpoint = Optional.absent();
protected Optional<Integer> port = Optional.absent();
protected Optional<String> endpoint = Optional.empty();
protected Optional<Integer> port = Optional.empty();
protected boolean useSSL = true;
protected Optional<String> apiKey = Optional.absent();
protected Optional<String> user = Optional.absent();
protected Optional<String> password = Optional.absent();
protected Optional<ProxyConfig> proxy = Optional.absent();
protected Optional<String> apiKey = Optional.empty();
protected Optional<String> user = Optional.empty();
protected Optional<String> password = Optional.empty();
protected Optional<ProxyConfig> proxy = Optional.empty();
protected BackOffStrategy retryStrategy = BackOffStrategy.FullJitter;
protected int retryLimit = 7;
protected int retryInitialIntervalMillis = 500;
Expand All @@ -74,7 +75,7 @@ private static Optional<String> getConfigProperty(Properties p, TDClientConfig.T

private static Optional<String> getConfigProperty(Properties p, String key)
{
return Optional.fromNullable(p.getProperty(key));
return Optional.ofNullable(p.getProperty(key));
}

private static Optional<Integer> getConfigPropertyInt(Properties p, TDClientConfig.Type key)
Expand All @@ -94,7 +95,7 @@ private static Optional<Integer> getConfigPropertyInt(Properties p, String key)
}
}
else {
return Optional.absent();
return Optional.empty();
}
}

Expand All @@ -115,7 +116,7 @@ private static Optional<Boolean> getConfigPropertyBoolean(Properties p, String k
}
}
else {
return Optional.absent();
return Optional.empty();
}
}

Expand All @@ -131,7 +132,7 @@ private static Optional<Double> getConfigPropertyDouble(Properties p, TDClientCo
}
}
else {
return Optional.absent();
return Optional.empty();
}
}

Expand Down Expand Up @@ -168,24 +169,25 @@ protected AbstractTDClientBuilder(boolean loadTDConf)
*/
public BuilderImpl setProperties(Properties p)
{
this.endpoint = getConfigProperty(p, API_ENDPOINT)
.or(getConfigProperty(p, "endpoint"))
.or(endpoint);
this.port = getConfigPropertyInt(p, API_PORT)
.or(getConfigPropertyInt(p, "port"))
.or(port);
this.useSSL = getConfigPropertyBoolean(p, USESSL)
.or(getConfigPropertyBoolean(p, "usessl"))
.or(useSSL);
this.apiKey = getConfigProperty(p, APIKEY)
.or(getConfigProperty(p, "apikey"))
.or(apiKey);
this.user = getConfigProperty(p, USER)
.or(getConfigProperty(p, "user"))
.or(user);
this.password = getConfigProperty(p, PASSOWRD)
.or(getConfigProperty(p, "password"))
.or(password);
this.endpoint = Stream.of(getConfigProperty(p, API_ENDPOINT), getConfigProperty(p, "endpoint"), endpoint)
.flatMap((opt) -> opt.map(Stream::of).orElseGet(Stream::empty))
.findFirst();
this.port = Stream.of(getConfigPropertyInt(p, API_PORT), getConfigPropertyInt(p, "port"), port)
.flatMap((opt) -> opt.map(Stream::of).orElseGet(Stream::empty))
.findFirst();
this.useSSL = Stream.of(getConfigPropertyBoolean(p, USESSL), getConfigPropertyBoolean(p, "usessl"))
.flatMap((opt) -> opt.map(Stream::of).orElseGet(Stream::empty))
.findFirst()
.orElse(useSSL);
this.apiKey = Stream.of(getConfigProperty(p, APIKEY), getConfigProperty(p, "apikey"), apiKey)
.flatMap((opt) -> opt.map(Stream::of).orElseGet(Stream::empty))
.findFirst();
this.user = Stream.of(getConfigProperty(p, USER), getConfigProperty(p, "user"), user)
.flatMap((opt) -> opt.map(Stream::of).orElseGet(Stream::empty))
.findFirst();
this.password = Stream.of(getConfigProperty(p, PASSOWRD), getConfigProperty(p, "password"), password)
.flatMap((opt) -> opt.map(Stream::of).orElseGet(Stream::empty))
.findFirst();

// proxy
boolean hasProxy = false;
Expand Down Expand Up @@ -222,16 +224,16 @@ public BuilderImpl setProperties(Properties p)
hasProxy = true;
proxyConfig.setPassword(proxyPassword.get());
}
this.proxy = Optional.fromNullable(hasProxy ? proxyConfig.createProxyConfig() : null);
this.proxy = Optional.ofNullable(hasProxy ? proxyConfig.createProxyConfig() : null);

// http client parameter
this.retryLimit = getConfigPropertyInt(p, RETRY_LIMIT).or(retryLimit);
this.retryInitialIntervalMillis = getConfigPropertyInt(p, RETRY_INITIAL_INTERVAL_MILLIS).or(retryInitialIntervalMillis);
this.retryMaxIntervalMillis = getConfigPropertyInt(p, RETRY_MAX_INTERVAL_MILLIS).or(retryMaxIntervalMillis);
this.retryMultiplier = getConfigPropertyDouble(p, RETRY_MULTIPLIER).or(retryMultiplier);
this.connectTimeoutMillis = getConfigPropertyInt(p, CONNECT_TIMEOUT_MILLIS).or(connectTimeoutMillis);
this.readTimeoutMillis = getConfigPropertyInt(p, READ_TIMEOUT_MILLIS).or(readTimeoutMillis);
this.connectionPoolSize = getConfigPropertyInt(p, CONNECTION_POOL_SIZE).or(connectionPoolSize);
this.retryLimit = getConfigPropertyInt(p, RETRY_LIMIT).orElse(retryLimit);
this.retryInitialIntervalMillis = getConfigPropertyInt(p, RETRY_INITIAL_INTERVAL_MILLIS).orElse(retryInitialIntervalMillis);
this.retryMaxIntervalMillis = getConfigPropertyInt(p, RETRY_MAX_INTERVAL_MILLIS).orElse(retryMaxIntervalMillis);
this.retryMultiplier = getConfigPropertyDouble(p, RETRY_MULTIPLIER).orElse(retryMultiplier);
this.connectTimeoutMillis = getConfigPropertyInt(p, CONNECT_TIMEOUT_MILLIS).orElse(connectTimeoutMillis);
this.readTimeoutMillis = getConfigPropertyInt(p, READ_TIMEOUT_MILLIS).orElse(readTimeoutMillis);
this.connectionPoolSize = getConfigPropertyInt(p, CONNECTION_POOL_SIZE).orElse(connectionPoolSize);

return self();
}
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/com/treasuredata/client/ProxyConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@
*/
package com.treasuredata.client;

import com.google.common.base.Optional;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.Optional;

/**
* Proxy configuration to access TD API
Expand Down Expand Up @@ -102,8 +101,8 @@ public static class ProxyConfigBuilder
private String host = "localhost";
private int port = 8080;
private boolean useSSL = false;
private Optional<String> user = Optional.absent();
private Optional<String> password = Optional.absent();
private Optional<String> user = Optional.empty();
private Optional<String> password = Optional.empty();

public ProxyConfigBuilder()
{
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/com/treasuredata/client/TDApiRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
*/
package com.treasuredata.client;

import com.google.common.base.Optional;
import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMultimap;
Expand All @@ -32,6 +31,7 @@
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

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

Expand Down Expand Up @@ -142,12 +142,12 @@ public static class Builder
private String path;
private Map<String, String> queryParams;
private ImmutableMultimap.Builder<String, String> headerParams;
private Optional<String> postJson = Optional.absent();
private Optional<File> file = Optional.absent();
private Optional<byte[]> content = Optional.absent();
private Optional<String> postJson = Optional.empty();
private Optional<File> file = Optional.empty();
private Optional<byte[]> content = Optional.empty();
private int contentOffset;
private int contentLength;
private Optional<Boolean> followRedirects = Optional.absent();
private Optional<Boolean> followRedirects = Optional.empty();

Builder(TDHttpMethod method, String path)
{
Expand Down
20 changes: 8 additions & 12 deletions src/main/java/com/treasuredata/client/TDClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
Expand Down Expand Up @@ -76,6 +75,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -237,13 +237,13 @@ protected <ResultType> ResultType doPost(String path, Map<String, String> queryP
protected <ResultType> ResultType doPost(String path, Class<ResultType> resultTypeClass)
throws TDClientException
{
return this.<ResultType>doPost(path, ImmutableMap.<String, String>of(), Optional.<String>absent(), resultTypeClass);
return this.<ResultType>doPost(path, ImmutableMap.<String, String>of(), Optional.empty(), resultTypeClass);
}

protected <ResultType> ResultType doPost(String path, Map<String, String> queryParam, Class<ResultType> resultTypeClass)
throws TDClientException
{
return this.<ResultType>doPost(path, queryParam, Optional.<String>absent(), resultTypeClass);
return this.<ResultType>doPost(path, queryParam, Optional.empty(), resultTypeClass);
}

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

@Override
Expand Down Expand Up @@ -653,14 +653,10 @@ public String submit(TDJobRequest jobRequest)
doPost(
buildUrl("/v3/job/issue", jobRequest.getType().getType(), jobRequest.getDatabase()),
queryParam,
jobRequest.getConfig().transform(new Function<ObjectNode, String>()
{
public String apply(ObjectNode config)
{
jobRequest.getConfig().map((config) -> {
ObjectNode body = config.objectNode();
body.set("config", config);
return body.toString();
}
}),
TDJobSubmitResult.class);
return result.getJobId();
Expand Down Expand Up @@ -780,13 +776,13 @@ public void performBulkImportSession(String sessionName, Optional<String> poolNa
@Override
public void performBulkImportSession(String sessionName, TDJob.Priority priority)
{
performBulkImportSession(sessionName, Optional.absent(), priority);
performBulkImportSession(sessionName, Optional.empty(), priority);
}

@Override
public void performBulkImportSession(String sessionName, Optional<String> poolName, TDJob.Priority priority)
{
Optional<String> jsonBody = Optional.absent();
Optional<String> jsonBody = Optional.empty();
if (poolName.isPresent()) {
jsonBody = Optional.of(JSONObject.toJSONString(ImmutableMap.of("pool_name", poolName.get())));
}
Expand Down Expand Up @@ -1061,7 +1057,7 @@ public Optional<TDTableDistribution> tableDistribution(String databaseName, Stri
return Optional.of(distribution);
}
catch (TDClientHttpNotFoundException e) {
return Optional.absent();
return Optional.empty();
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/treasuredata/client/TDClientApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
*/
package com.treasuredata.client;

import java.util.Optional;

import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.collect.Multimap;
import com.treasuredata.client.model.TDApiKey;
import com.treasuredata.client.model.TDBulkImportSession;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/treasuredata/client/TDClientConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package com.treasuredata.client;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Multimap;
import com.google.common.io.Files;
Expand All @@ -33,6 +32,7 @@
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Optional;
import java.util.Properties;

/**
Expand Down Expand Up @@ -124,7 +124,7 @@ public static List<Type> knownProperties()
int connectionPoolSize,
Multimap<String, String> headers)
{
this.endpoint = endpoint.or("api.treasuredata.com");
this.endpoint = endpoint.orElse("api.treasuredata.com");
this.port = port;
this.useSSL = useSSL;
this.apiKey = apiKey;
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/treasuredata/client/TDClientException.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
package com.treasuredata.client;

import com.google.common.base.Optional;
import java.util.Optional;

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

Expand Down Expand Up @@ -65,7 +65,7 @@ private static final String formatErrorMessage(ErrorType errorType, String messa

public TDClientException(ErrorType errorType, String message, Optional<Exception> cause)
{
super(formatErrorMessage(errorType, message, cause), cause.orNull());
super(formatErrorMessage(errorType, message, cause), cause.orElse(null));
checkNotNull(errorType, "errorType is null");
checkNotNull(cause, "cause is null");
this.errorType = errorType;
Expand All @@ -84,7 +84,7 @@ public TDClientException(ErrorType errorType, Exception cause)

public TDClientException(ErrorType errorType, String message)
{
this(errorType, message, Optional.<Exception>absent());
this(errorType, message, Optional.empty());
}

public ErrorType getErrorType()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
package com.treasuredata.client;

import com.google.common.base.Optional;
import java.util.Optional;

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

public Optional<String> getConflictsWith()
{
return Optional.fromNullable(conflictsWith);
return Optional.ofNullable(conflictsWith);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@
*/
package com.treasuredata.client;

import com.google.common.base.Optional;

import java.util.Date;
import java.util.Optional;

/**
* Exception class for reporting http server status code
Expand All @@ -47,7 +46,7 @@ public int getStatusCode()
public Optional<Date> getRetryAfter()
{
if (retryAfter == -1) {
return Optional.absent();
return Optional.empty();
}
else {
return Optional.of(new Date(retryAfter));
Expand Down
Loading

0 comments on commit e0c2c51

Please sign in to comment.