CorePluginsRepository: Convert to streams and lambdas

Change-Id: If26858a4c3ba6e814d19252702af0dab95e037b6
diff --git a/src/main/java/com/googlesource/gerrit/plugins/manager/repository/CorePluginsRepository.java b/src/main/java/com/googlesource/gerrit/plugins/manager/repository/CorePluginsRepository.java
index d766281..5a3dc70 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/manager/repository/CorePluginsRepository.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/manager/repository/CorePluginsRepository.java
@@ -14,9 +14,12 @@
 
 package com.googlesource.gerrit.plugins.manager.repository;
 
-import com.google.common.base.Function;
-import com.google.common.base.Predicate;
-import com.google.common.collect.FluentIterable;
+import static com.google.common.collect.ImmutableList.toImmutableList;
+import static java.util.Comparator.comparing;
+import static java.util.Objects.requireNonNull;
+
+import com.google.common.collect.ImmutableList;
+import com.google.gerrit.common.Nullable;
 import com.google.gerrit.common.Version;
 import com.google.gerrit.server.config.SitePaths;
 import com.google.inject.Inject;
@@ -25,9 +28,7 @@
 import java.net.URISyntaxException;
 import java.nio.file.Path;
 import java.nio.file.Paths;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.Comparator;
+import java.util.Objects;
 import java.util.jar.Attributes;
 import java.util.jar.JarEntry;
 import java.util.jar.JarFile;
@@ -49,107 +50,80 @@
     this.pluginsDescriptions = pd;
   }
 
-  static class SelectPluginsFromJar implements Predicate<JarEntry> {
-    @Override
-    public boolean apply(JarEntry entry) {
-      String entryName = entry.getName();
-      return (entryName.startsWith("WEB-INF/plugins") && entryName.endsWith(".jar"));
-    }
-  }
-
-  class ExtractPluginInfoFromJarEntry implements Function<JarEntry, PluginInfo> {
-    private String gerritWarFilename;
-
-    public ExtractPluginInfoFromJarEntry(String gerritWarFilename) {
-      this.gerritWarFilename = gerritWarFilename;
-    }
-
-    @Override
-    public PluginInfo apply(JarEntry entry) {
-      try {
-        Path entryName = Paths.get(entry.getName());
-        URI pluginUrl = new URI("jar:file:" + gerritWarFilename + "!/" + entry.getName());
-        try (JarInputStream pluginJar = new JarInputStream(pluginUrl.toURL().openStream())) {
-          Manifest manifestJarEntry = getManifestEntry(pluginJar);
-          if (manifestJarEntry != null) {
-            Attributes pluginAttributes = manifestJarEntry.getMainAttributes();
-            String pluginName = pluginAttributes.getValue("Gerrit-PluginName");
-            return new PluginInfo(
-                pluginName,
-                pluginsDescriptions.get(pluginName).orElse(""),
-                pluginAttributes.getValue("Implementation-Version"),
-                "",
-                pluginUrl.toString());
-          }
+  @Nullable
+  private PluginInfo extractPluginInfoFromJarEntry(JarEntry entry) {
+    try {
+      Path entryName = Paths.get(entry.getName());
+      URI pluginUrl =
+          new URI("jar:file:" + requireNonNull(site.gerrit_war) + "!/" + entry.getName());
+      try (JarInputStream pluginJar = new JarInputStream(pluginUrl.toURL().openStream())) {
+        Manifest manifestJarEntry = getManifestEntry(pluginJar);
+        if (manifestJarEntry != null) {
+          Attributes pluginAttributes = manifestJarEntry.getMainAttributes();
+          String pluginName = pluginAttributes.getValue("Gerrit-PluginName");
           return new PluginInfo(
-              dropSuffix(entryName.getFileName().toString(), ".jar"),
-              "",
-              "",
+              pluginName,
+              pluginsDescriptions.get(pluginName).orElse(""),
+              pluginAttributes.getValue("Implementation-Version"),
               "",
               pluginUrl.toString());
-        } catch (IOException e) {
-          log.error("Unable to open plugin " + pluginUrl, e);
-          return null;
         }
-      } catch (URISyntaxException e) {
-        log.error("Invalid plugin filename", e);
+        return new PluginInfo(
+            dropSuffix(entryName.getFileName().toString(), ".jar"),
+            "",
+            "",
+            "",
+            pluginUrl.toString());
+      } catch (IOException e) {
+        log.error("Unable to open plugin " + pluginUrl, e);
         return null;
       }
-    }
-
-    private String dropSuffix(String string, String suffix) {
-      return string.endsWith(suffix)
-          ? string.substring(0, string.length() - suffix.length())
-          : string;
-    }
-
-    private Manifest getManifestEntry(JarInputStream pluginJar) throws IOException {
-      for (JarEntry entry = pluginJar.getNextJarEntry();
-          entry != null;
-          entry = pluginJar.getNextJarEntry()) {
-        if (entry.getName().equals("META-INF/MANIFEST.MF")) {
-          return new Manifest(pluginJar);
-        }
-      }
+    } catch (URISyntaxException e) {
+      log.error("Invalid plugin filename", e);
       return null;
     }
   }
 
+  private String dropSuffix(String string, String suffix) {
+    return string.endsWith(suffix)
+        ? string.substring(0, string.length() - suffix.length())
+        : string;
+  }
+
+  @Nullable
+  private static Manifest getManifestEntry(JarInputStream pluginJar) throws IOException {
+    for (JarEntry entry = pluginJar.getNextJarEntry();
+        entry != null;
+        entry = pluginJar.getNextJarEntry()) {
+      if (entry.getName().equals("META-INF/MANIFEST.MF")) {
+        return new Manifest(pluginJar);
+      }
+    }
+    return null;
+  }
+
   @Override
-  public Collection<PluginInfo> list(String gerritVersion) throws IOException {
+  public ImmutableList<PluginInfo> list(String gerritVersion) throws IOException {
     if (!gerritVersion.equals(GERRIT_VERSION)) {
       log.warn(
           "No core plugins available for version {} which is different than "
               + "the current running Gerrit",
           gerritVersion);
-      return Collections.emptyList();
+      return ImmutableList.of();
     }
 
-    final Path gerritWarPath = site.gerrit_war;
-    if (gerritWarPath == null) {
+    if (site.gerrit_war == null) {
       log.warn("Core plugins not available on non-war Gerrit distributions");
-      return Collections.emptyList();
+      return ImmutableList.of();
     }
 
-    try (JarFile gerritWar = new JarFile(gerritWarPath.toFile())) {
-
-      return FluentIterable.from(Collections.list(gerritWar.entries()))
-          .filter(new SelectPluginsFromJar())
-          .transform(new ExtractPluginInfoFromJarEntry(gerritWarPath.toString()))
-          .filter(
-              new Predicate<PluginInfo>() {
-                @Override
-                public boolean apply(PluginInfo pluginInfo) {
-                  return pluginInfo != null;
-                }
-              })
-          .toSortedList(
-              new Comparator<PluginInfo>() {
-                @Override
-                public int compare(PluginInfo a, PluginInfo b) {
-                  return a.name.compareTo(b.name);
-                }
-              });
+    try (JarFile gerritWar = new JarFile(site.gerrit_war.toFile())) {
+      return gerritWar.stream()
+          .filter(e -> e.getName().startsWith("WEB-INF/plugins") && e.getName().endsWith(".jar"))
+          .map(this::extractPluginInfoFromJarEntry)
+          .filter(Objects::nonNull)
+          .sorted(comparing(p -> p.name))
+          .collect(toImmutableList());
     }
   }
 }