Switch from Guava Optionals to Java 8 Optionals

This was a surprisingly involved change. Some of the difficulties included
java.util.Optional purposely not being Serializable (so I had to move a
few Optionals in mapreduce classes to @Nullable) and having to add the Truth
Java8 extension library for assertion support.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=171863777
This commit is contained in:
mcilwain 2017-10-11 13:09:26 -07:00 committed by jianglai
parent 184b2b56ac
commit c0f8da0c6e
581 changed files with 1325 additions and 932 deletions

View file

@ -19,7 +19,6 @@ import static com.google.common.base.Verify.verify;
import static javax.servlet.http.HttpServletResponse.SC_FORBIDDEN;
import static javax.servlet.http.HttpServletResponse.SC_NOT_FOUND;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableMap;
import com.google.common.net.MediaType;
import com.google.common.primitives.Ints;
@ -28,6 +27,7 @@ import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Optional;
import javax.annotation.PostConstruct;
import javax.servlet.ServletConfig;
import javax.servlet.http.HttpServlet;
@ -86,7 +86,7 @@ public final class StaticResourceServlet extends HttpServlet {
return holder;
}
private Optional<FileServer> fileServer = Optional.absent();
private Optional<FileServer> fileServer = Optional.empty();
@Override
@PostConstruct
@ -124,12 +124,12 @@ public final class StaticResourceServlet extends HttpServlet {
if (!Files.exists(file)) {
logger.infofmt("Not found: %s (%s)", req.getRequestURI(), file);
rsp.sendError(SC_NOT_FOUND, "Not found");
return Optional.absent();
return Optional.empty();
}
if (Files.isDirectory(file)) {
logger.infofmt("Directory listing forbidden: %s (%s)", req.getRequestURI(), file);
rsp.sendError(SC_FORBIDDEN, "No directory listing");
return Optional.absent();
return Optional.empty();
}
rsp.setContentType(
MIMES_BY_EXTENSION
@ -140,8 +140,9 @@ public final class StaticResourceServlet extends HttpServlet {
}
void doGet(HttpServletRequest req, HttpServletResponse rsp) throws IOException {
for (Path file : doHead(req, rsp).asSet()) {
rsp.getOutputStream().write(Files.readAllBytes(file));
Optional<Path> file = doHead(req, rsp);
if (file.isPresent()) {
rsp.getOutputStream().write(Files.readAllBytes(file.get()));
}
}
}