From: Simon Brandhof Date: Thu, 10 May 2018 20:10:29 +0000 (+0200) Subject: SONAR-10591 remove some Guava functions from org.sonar.server.plugins.ws X-Git-Tag: 7.5~1203 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=e1f1d29b30f82426ec2300b5f59ae9dd206743e3;p=sonarqube.git SONAR-10591 remove some Guava functions from org.sonar.server.plugins.ws --- diff --git a/server/sonar-server/src/main/java/org/sonar/server/plugins/ws/InstallAction.java b/server/sonar-server/src/main/java/org/sonar/server/plugins/ws/InstallAction.java index f52442064a6..01244212cb3 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/plugins/ws/InstallAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/plugins/ws/InstallAction.java @@ -20,9 +20,7 @@ package org.sonar.server.plugins.ws; import com.google.common.base.Optional; -import com.google.common.base.Predicate; -import com.google.common.collect.Iterables; -import javax.annotation.Nullable; +import java.util.Objects; import org.sonar.api.server.ws.Request; import org.sonar.api.server.ws.Response; import org.sonar.api.server.ws.WebService; @@ -41,7 +39,6 @@ import static org.sonar.server.plugins.edition.EditionBundledPlugins.isEditionBu public class InstallAction implements PluginsWsAction { private static final String PARAM_KEY = "key"; - private static final PluginUpdate MISSING_PLUGIN = null; private final UpdateCenterMatrixFactory updateCenterFactory; private final PluginDownloader pluginDownloader; @@ -81,17 +78,19 @@ public class InstallAction implements PluginsWsAction { } private PluginUpdate findAvailablePluginByKey(String key) { - PluginUpdate pluginUpdate = MISSING_PLUGIN; + PluginUpdate pluginUpdate = null; Optional updateCenter = updateCenterFactory.getUpdateCenter(false); if (updateCenter.isPresent()) { - pluginUpdate = Iterables.find( - updateCenter.get().findAvailablePlugins(), - hasKey(key), - MISSING_PLUGIN); + pluginUpdate = updateCenter.get().findAvailablePlugins() + .stream() + .filter(Objects::nonNull) + .filter(u -> key.equals(u.getPlugin().getKey())) + .findFirst() + .orElse(null); } - if (pluginUpdate == MISSING_PLUGIN) { + if (pluginUpdate == null) { throw new IllegalArgumentException( format("No plugin with key '%s' or plugin '%s' is already installed in latest version", key, key)); } @@ -103,21 +102,4 @@ public class InstallAction implements PluginsWsAction { return pluginUpdate; } - - private static PluginKeyPredicate hasKey(String key) { - return new PluginKeyPredicate(key); - } - - private static class PluginKeyPredicate implements Predicate { - private final String key; - - public PluginKeyPredicate(String key) { - this.key = key; - } - - @Override - public boolean apply(@Nullable PluginUpdate input) { - return input != null && key.equals(input.getPlugin().getKey()); - } - } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/plugins/ws/PluginUpdateAggregator.java b/server/sonar-server/src/main/java/org/sonar/server/plugins/ws/PluginUpdateAggregator.java index 37ac3016405..5bc214eeefb 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/plugins/ws/PluginUpdateAggregator.java +++ b/server/sonar-server/src/main/java/org/sonar/server/plugins/ws/PluginUpdateAggregator.java @@ -20,19 +20,16 @@ package org.sonar.server.plugins.ws; import com.google.common.annotations.VisibleForTesting; -import com.google.common.base.Function; import com.google.common.collect.ImmutableList; import com.google.common.collect.Lists; import com.google.common.collect.Maps; -import org.sonar.updatecenter.common.Plugin; -import org.sonar.updatecenter.common.PluginUpdate; - -import javax.annotation.Nonnull; -import javax.annotation.Nullable; import java.util.Collection; import java.util.Collections; import java.util.List; import java.util.Map; +import javax.annotation.Nullable; +import org.sonar.updatecenter.common.Plugin; +import org.sonar.updatecenter.common.PluginUpdate; import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; @@ -57,16 +54,7 @@ public class PluginUpdateAggregator { builder.add(pluginUpdate); } - return Lists.newArrayList(transform(builders.values(), BuilderToPluginUpdateAggregate.INSTANCE)); - } - - private enum BuilderToPluginUpdateAggregate implements Function { - INSTANCE; - - @Override - public PluginUpdateAggregate apply(@Nonnull PluginUpdateAggregateBuilder input) { - return input.build(); - } + return Lists.newArrayList(transform(builders.values(), PluginUpdateAggregateBuilder::build)); } @VisibleForTesting diff --git a/server/sonar-server/src/main/java/org/sonar/server/plugins/ws/PluginWSCommons.java b/server/sonar-server/src/main/java/org/sonar/server/plugins/ws/PluginWSCommons.java index 6bbad94e3b3..239f191a77a 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/plugins/ws/PluginWSCommons.java +++ b/server/sonar-server/src/main/java/org/sonar/server/plugins/ws/PluginWSCommons.java @@ -20,7 +20,6 @@ package org.sonar.server.plugins.ws; import com.google.common.annotations.VisibleForTesting; -import com.google.common.base.Function; import com.google.common.base.Optional; import com.google.common.collect.ImmutableMap; import com.google.common.collect.Maps; @@ -29,7 +28,6 @@ import java.util.Collections; import java.util.Comparator; import java.util.List; import javax.annotation.CheckForNull; -import javax.annotation.Nonnull; import javax.annotation.Nullable; import org.sonar.api.utils.text.JsonWriter; import org.sonar.core.platform.PluginInfo; @@ -83,11 +81,11 @@ public class PluginWSCommons { .comparing((java.util.function.Function) installedPluginFile -> installedPluginFile.getPluginInfo().getName()) .thenComparing(f -> f.getPluginInfo().getKey()); public static final Comparator NAME_KEY_PLUGIN_ORDERING = Ordering.from(CASE_INSENSITIVE_ORDER) - .onResultOf(PluginToName.INSTANCE) + .onResultOf(Plugin::getName) .compound( - Ordering.from(CASE_INSENSITIVE_ORDER).onResultOf(PluginToKeyFunction.INSTANCE)); + Ordering.from(CASE_INSENSITIVE_ORDER).onResultOf(Artifact::getKey)); public static final Comparator NAME_KEY_PLUGIN_UPDATE_ORDERING = Ordering.from(NAME_KEY_PLUGIN_ORDERING) - .onResultOf(PluginUpdateToPlugin.INSTANCE); + .onResultOf(PluginUpdate::getPlugin); private PluginWSCommons() { // prevent instantiation @@ -199,7 +197,7 @@ public class PluginWSCommons { jsonWriter.name(ARRAY_REQUIRES).beginArray(); Release release = pluginUpdate.getRelease(); - for (Plugin child : filter(transform(release.getOutgoingDependencies(), ReleaseToArtifact.INSTANCE), Plugin.class)) { + for (Plugin child : filter(transform(release.getOutgoingDependencies(), Release::getArtifact), Plugin.class)) { jsonWriter.beginObject(); jsonWriter.prop(PROPERTY_KEY, child.getKey()); jsonWriter.prop(PROPERTY_NAME, child.getName()); @@ -237,40 +235,6 @@ public class PluginWSCommons { } } - enum PluginToKeyFunction implements Function { - INSTANCE; - - @Override - public String apply(@Nonnull Plugin input) { - return input.getKey(); - } - } - - private enum ReleaseToArtifact implements Function { - INSTANCE; - @Override - public Artifact apply(@Nonnull Release input) { - return input.getArtifact(); - } - - } - - private enum PluginUpdateToPlugin implements Function { - INSTANCE; - @Override - public Plugin apply(@Nonnull PluginUpdate input) { - return input.getPlugin(); - } - } - - private enum PluginToName implements Function { - INSTANCE; - @Override - public String apply(@Nonnull Plugin input) { - return input.getName(); - } - } - @CheckForNull static String categoryOrNull(@Nullable Plugin plugin) { return plugin != null ? plugin.getCategory() : null; @@ -283,6 +247,6 @@ public class PluginWSCommons { static ImmutableMap compatiblePluginsByKey(UpdateCenterMatrixFactory updateCenterMatrixFactory) { List compatiblePlugins = compatiblePlugins(updateCenterMatrixFactory); - return Maps.uniqueIndex(compatiblePlugins, PluginToKeyFunction.INSTANCE); + return Maps.uniqueIndex(compatiblePlugins, Artifact::getKey); } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/plugins/ws/UpdatesAction.java b/server/sonar-server/src/main/java/org/sonar/server/plugins/ws/UpdatesAction.java index 58e3e8ab022..5307e38d61f 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/plugins/ws/UpdatesAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/plugins/ws/UpdatesAction.java @@ -19,14 +19,12 @@ */ package org.sonar.server.plugins.ws; -import com.google.common.base.Function; import com.google.common.base.Optional; import com.google.common.collect.ImmutableSortedSet; import com.google.common.collect.Ordering; import com.google.common.io.Resources; import java.util.Collection; import java.util.List; -import javax.annotation.Nonnull; import org.sonar.api.server.ws.Request; import org.sonar.api.server.ws.Response; import org.sonar.api.server.ws.WebService; @@ -48,7 +46,7 @@ public class UpdatesAction implements PluginsWsAction { private static final String ARRAY_UPDATES = "updates"; private static final Ordering NAME_KEY_PLUGIN_UPGRADE_AGGREGATE_ORDERING = Ordering.from(PluginWSCommons.NAME_KEY_PLUGIN_ORDERING) - .onResultOf(PluginUpdateAggregateToPlugin.INSTANCE); + .onResultOf(PluginUpdateAggregate::getPlugin); private static final Ordering PLUGIN_UPDATE_BY_VERSION_ORDERING = Ordering.natural() .onResultOf(input -> input.getRelease().getVersion().toString()); @@ -136,13 +134,4 @@ public class UpdatesAction implements PluginsWsAction { NAME_KEY_PLUGIN_UPGRADE_AGGREGATE_ORDERING, aggregator.aggregate(pluginUpdates)); } - - private enum PluginUpdateAggregateToPlugin implements Function { - INSTANCE; - - @Override - public Plugin apply(@Nonnull PluginUpdateAggregate input) { - return input.getPlugin(); - } - } }