Skip to content

Commit

Permalink
Replace guava's Optional with java.util's one
Browse files Browse the repository at this point in the history
  • Loading branch information
patrox committed Mar 25, 2016
1 parent 1cb9779 commit 7eb1da4
Show file tree
Hide file tree
Showing 53 changed files with 131 additions and 154 deletions.
4 changes: 2 additions & 2 deletions docs/source/getting-started.rst
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,6 @@ instances from the URI ``/hello-world``, so our resource class looks like this:
package com.example.helloworld.resources;
import com.example.helloworld.api.Saying;
import com.google.common.base.Optional;
import com.codahale.metrics.annotation.Timed;
import javax.ws.rs.GET;
Expand All @@ -417,6 +416,7 @@ instances from the URI ``/hello-world``, so our resource class looks like this:
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import java.util.concurrent.atomic.AtomicLong;
import java.util.Optional;
@Path("/hello-world")
@Produces(MediaType.APPLICATION_JSON)
Expand All @@ -434,7 +434,7 @@ instances from the URI ``/hello-world``, so our resource class looks like this:
@GET
@Timed
public Saying sayHello(@QueryParam("name") Optional<String> name) {
final String value = String.format(template, name.or(defaultName));
final String value = String.format(template, name.orElse(defaultName));
return new Saying(counter.incrementAndGet(), value);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.dropwizard.client;

import com.google.common.base.Optional;
import com.google.common.util.concurrent.MoreExecutors;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
Expand All @@ -18,7 +17,6 @@
import org.glassfish.jersey.client.ClientResponse;
import org.glassfish.jersey.client.spi.AsyncConnectorCallback;
import org.glassfish.jersey.client.spi.Connector;
import org.glassfish.jersey.message.internal.OutboundMessageContext;
import org.glassfish.jersey.message.internal.Statuses;

import javax.ws.rs.ProcessingException;
Expand All @@ -30,6 +28,7 @@
import java.io.OutputStream;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.Future;

import static com.google.common.base.MoreObjects.firstNonNull;
Expand Down Expand Up @@ -131,9 +130,7 @@ private HttpUriRequest buildApacheRequest(ClientRequest jerseyRequest) {
}

final Optional<RequestConfig> requestConfig = addJerseyRequestConfig(jerseyRequest);
if (requestConfig.isPresent()) {
builder.setConfig(requestConfig.get());
}
requestConfig.ifPresent(builder::setConfig);

return builder.build();
}
Expand Down Expand Up @@ -161,7 +158,7 @@ private Optional<RequestConfig> addJerseyRequestConfig(ClientRequest clientReque
return Optional.of(requestConfig.build());
}

return Optional.absent();
return Optional.empty();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
import org.apache.http.impl.conn.SystemDefaultDnsResolver;
import org.apache.http.protocol.HttpContext;

import java.io.IOException;
import java.util.List;

/**
Expand Down Expand Up @@ -345,7 +344,7 @@ public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
*/
protected String createUserAgent(String name) {
final String defaultUserAgent = environmentName == null ? name : String.format("%s (%s)", environmentName, name);
return configuration.getUserAgent().or(defaultUserAgent);
return configuration.getUserAgent().orElse(defaultUserAgent);
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.dropwizard.client;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Optional;
import io.dropwizard.client.proxy.ProxyConfiguration;
import io.dropwizard.client.ssl.TlsConfiguration;
import io.dropwizard.util.Duration;
Expand All @@ -12,6 +11,7 @@
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.util.Optional;

/**
* The configuration class used by {@link HttpClientBuilder}.
Expand Down Expand Up @@ -50,7 +50,7 @@ public class HttpClientConfiguration {

@NotNull
@UnwrapValidatedValue(false)
private Optional<String> userAgent = Optional.absent();
private Optional<String> userAgent = Optional.empty();

@Valid
@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.codahale.metrics.httpclient.HttpClientMetricNameStrategies;
import com.codahale.metrics.httpclient.InstrumentedHttpClientConnectionManager;
import com.codahale.metrics.httpclient.InstrumentedHttpRequestExecutor;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableList;
import io.dropwizard.client.proxy.AuthConfiguration;
import io.dropwizard.client.proxy.ProxyConfiguration;
Expand Down Expand Up @@ -64,6 +63,7 @@
import java.net.SocketAddress;
import java.net.URI;
import java.util.List;
import java.util.Optional;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.*;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
import com.codahale.metrics.MetricRegistry;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Optional;
import com.google.common.io.CharStreams;
import com.sun.net.httpserver.Headers;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import io.dropwizard.jackson.Jackson;
import org.glassfish.jersey.filter.LoggingFilter;
Expand All @@ -25,6 +23,7 @@
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;
import java.util.Optional;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.zip.GZIPInputStream;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,31 @@
package io.dropwizard.client;

import static org.assertj.core.api.Assertions.assertThat;

import java.net.URI;
import java.util.concurrent.Executors;

import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.Path;

import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Test;

import com.codahale.metrics.MetricRegistry;
import com.google.common.base.Optional;
import com.google.common.io.Resources;

import io.dropwizard.Application;
import io.dropwizard.Configuration;
import io.dropwizard.jackson.Jackson;
import io.dropwizard.setup.Environment;
import io.dropwizard.testing.junit.DropwizardAppRule;
import io.dropwizard.util.Duration;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Test;

import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.Path;
import java.net.URI;
import java.util.Optional;
import java.util.concurrent.Executors;

import static org.assertj.core.api.Assertions.assertThat;

public class JerseyIgnoreRequestUserAgentHeaderFilterTest {
@ClassRule
public static DropwizardAppRule<Configuration> APP_RULE =
new DropwizardAppRule<>(TestApplication.class, Resources.getResource("yaml/jerseyIgnoreRequestUserAgentHeaderFilterTest.yml").getPath());

private final URI testUri = URI.create("http://localhost:" + APP_RULE.getLocalPort());
private JerseyClientBuilder clientBuilder;
private JerseyClientConfiguration clientConfiguration;
Expand All @@ -42,7 +39,7 @@ public void setup() {
.using(clientConfiguration)
.using(Executors.newSingleThreadExecutor(), Jackson.newObjectMapper());
}

@Test
public void clientIsSetRequestIsNotSet() {
clientConfiguration.setUserAgent(Optional.of("ClientUserAgentHeaderValue"));
Expand All @@ -53,7 +50,7 @@ public void clientIsSetRequestIsNotSet() {
.get(String.class)
).isEqualTo("ClientUserAgentHeaderValue");
}

@Test
public void clientIsNotSetRequestIsSet() {
assertThat(
Expand All @@ -65,7 +62,7 @@ public void clientIsNotSetRequestIsSet() {

@Test
public void clientIsNotSetRequestIsNotSet() {
assertThat(false);
assertThat(false);
assertThat(
clientBuilder.build("ClientName").target(testUri + "/user_agent")
.request()
Expand Down Expand Up @@ -102,4 +99,4 @@ public void run(Configuration configuration, Environment environment) throws Exc
environment.jersey().register(TestResource.class);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.dropwizard.client.ssl;

import com.google.common.base.Optional;
import io.dropwizard.Application;
import io.dropwizard.Configuration;
import io.dropwizard.client.JerseyClientBuilder;
Expand All @@ -24,6 +23,7 @@
import javax.ws.rs.core.Response;
import java.io.File;
import java.net.SocketException;
import java.util.Optional;

import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;
Expand Down
2 changes: 1 addition & 1 deletion dropwizard-core/src/main/java/io/dropwizard/cli/Cli.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ private static boolean isFlag(String[][] flags, String[] arguments) {
private ArgumentParser buildParser(JarLocation location) {
final String usage = "java -jar " + location;
final ArgumentParser p = ArgumentParsers.newArgumentParser(usage, false);
p.version(location.getVersion().or(
p.version(location.getVersion().orElse(
"No application version detected. Add a Implementation-Version " +
"entry to your JAR's manifest to enable this."));
addHelp(p);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Joiner;
import com.google.common.base.Optional;
import com.google.common.io.Resources;
import io.dropwizard.jersey.errors.EarlyEofExceptionMapper;
import io.dropwizard.jersey.errors.LoggingExceptionMapper;
Expand Down Expand Up @@ -54,6 +53,7 @@
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.EnumSet;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.BlockingQueue;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -255,7 +255,7 @@ public abstract class AbstractServerFactory implements ServerFactory {
@NotNull
private Set<String> allowedMethods = AllowedMethodsFilter.DEFAULT_ALLOWED_METHODS;

private Optional<String> jerseyRootPath = Optional.absent();
private Optional<String> jerseyRootPath = Optional.empty();

@JsonIgnore
@ValidationMethod(message = "must have a smaller minThreads than maxThreads")
Expand Down Expand Up @@ -448,7 +448,7 @@ public Optional<String> getJerseyRootPath() {

@JsonProperty("rootPath")
public void setJerseyRootPath(String jerseyRootPath) {
this.jerseyRootPath = Optional.fromNullable(jerseyRootPath);
this.jerseyRootPath = Optional.ofNullable(jerseyRootPath);
}

protected Handler createAdminServlet(Server server,
Expand Down
4 changes: 2 additions & 2 deletions dropwizard-core/src/test/java/io/dropwizard/cli/CliTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.dropwizard.cli;

import com.google.common.base.Optional;
import io.dropwizard.Application;
import io.dropwizard.Configuration;
import io.dropwizard.setup.Bootstrap;
Expand All @@ -14,6 +13,7 @@

import java.io.ByteArrayOutputStream;
import java.util.Locale;
import java.util.Optional;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.any;
Expand Down Expand Up @@ -89,7 +89,7 @@ public void handlesLongVersionCommands() throws Exception {

@Test
public void handlesMissingVersions() throws Exception {
when(location.getVersion()).thenReturn(Optional.<String>absent());
when(location.getVersion()).thenReturn(Optional.empty());
final Cli newCli = new Cli(location, bootstrap, stdOut, stdErr);

assertThat(newCli.run("--version"))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.dropwizard.cli;

import com.google.common.base.Optional;
import io.dropwizard.Application;
import io.dropwizard.Configuration;
import io.dropwizard.setup.Bootstrap;
Expand All @@ -12,10 +11,9 @@
import org.junit.Test;

import java.io.ByteArrayOutputStream;
import java.util.Optional;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.codahale.metrics.MetricRegistry;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Optional;
import com.google.common.primitives.Ints;
import io.dropwizard.util.Duration;
import io.dropwizard.validation.MinDuration;
Expand All @@ -18,6 +17,7 @@
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import java.util.Optional;
import java.util.concurrent.TimeUnit;

/**
Expand Down Expand Up @@ -390,7 +390,7 @@ public int get() {
@MinDuration(1)
private Duration validationInterval = Duration.seconds(30);

private Optional<String> validatorClassName = Optional.absent();
private Optional<String> validatorClassName = Optional.empty();

private boolean removeAbandoned = false;

Expand Down Expand Up @@ -682,7 +682,7 @@ public void setLogValidationErrors(boolean log) {

@JsonProperty
public Optional<Duration> getMaxConnectionAge() {
return Optional.fromNullable(maxConnectionAge);
return Optional.ofNullable(maxConnectionAge);
}

@JsonProperty
Expand Down Expand Up @@ -753,7 +753,7 @@ public void setValidationInterval(Duration validationInterval) {
@Override
@JsonProperty
public Optional<Duration> getValidationQueryTimeout() {
return Optional.fromNullable(validationQueryTimeout);
return Optional.ofNullable(validationQueryTimeout);
}

@JsonProperty
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.dropwizard.db;

import com.codahale.metrics.MetricRegistry;
import com.google.common.base.Optional;
import java.util.Optional;
import io.dropwizard.util.Duration;

import java.util.Map;
Expand Down
Loading

0 comments on commit 7eb1da4

Please sign in to comment.