From 324be00ea9fb1e77eb78eef1abdafbb1b4beca53 Mon Sep 17 00:00:00 2001 From: =?utf8?q?S=C3=A9bastien=20Lesaint?= Date: Wed, 1 Jun 2016 13:59:11 +0200 Subject: [PATCH] fix quality flaws: use lamdbas and methods references --- .../qualitygate/QualityGateServiceImpl.java | 11 +++----- .../step/AbstractComputationSteps.java | 11 +++----- .../step/BuildComponentTreeStep.java | 19 ++++--------- .../computation/step/PersistEventsStep.java | 18 +++++-------- .../migrations/PlatformDatabaseMigration.java | 7 +---- .../server/plugins/ws/UpdatesAction.java | 15 ++++------- .../sonar/server/ruby/PlatformRubyBridge.java | 27 +++++-------------- 7 files changed, 30 insertions(+), 78 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/qualitygate/QualityGateServiceImpl.java b/server/sonar-server/src/main/java/org/sonar/server/computation/qualitygate/QualityGateServiceImpl.java index 5d6e7156063..5f1a463bd3e 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/qualitygate/QualityGateServiceImpl.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/qualitygate/QualityGateServiceImpl.java @@ -21,7 +21,6 @@ package org.sonar.server.computation.qualitygate; import com.google.common.base.Function; import com.google.common.base.Optional; -import javax.annotation.Nonnull; import org.sonar.db.qualitygate.QualityGateConditionDao; import org.sonar.db.qualitygate.QualityGateConditionDto; import org.sonar.db.qualitygate.QualityGateDao; @@ -39,13 +38,9 @@ public class QualityGateServiceImpl implements QualityGateService { public QualityGateServiceImpl(QualityGateDao qualityGateDao, QualityGateConditionDao conditionDao, final MetricRepository metricRepository) { this.qualityGateDao = qualityGateDao; this.conditionDao = conditionDao; - this.conditionDtoToBean = new Function() { - @Override - @Nonnull - public Condition apply(@Nonnull QualityGateConditionDto input) { - Metric metric = metricRepository.getById(input.getMetricId()); - return new Condition(metric, input.getOperator(), input.getErrorThreshold(), input.getWarningThreshold(), input.getPeriod()); - } + this.conditionDtoToBean = (QualityGateConditionDto input) -> { + Metric metric = metricRepository.getById(input.getMetricId()); + return new Condition(metric, input.getOperator(), input.getErrorThreshold(), input.getWarningThreshold(), input.getPeriod()); }; } diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/step/AbstractComputationSteps.java b/server/sonar-server/src/main/java/org/sonar/server/computation/step/AbstractComputationSteps.java index 84fedd70bba..3ccddf5736e 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/step/AbstractComputationSteps.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/step/AbstractComputationSteps.java @@ -19,9 +19,7 @@ */ package org.sonar.server.computation.step; -import com.google.common.base.Function; import com.google.common.collect.Iterables; -import javax.annotation.Nonnull; import org.sonar.core.platform.ContainerPopulator; /** @@ -37,15 +35,14 @@ public abstract class AbstractComputationSteps implements ComputationSteps { @Override public Iterable instances() { - return Iterables.transform(orderedStepClasses(), new Function, ComputationStep>() { - @Override - public ComputationStep apply(@Nonnull Class input) { + return Iterables.transform( + orderedStepClasses(), + input -> { ComputationStep computationStepType = container.getComponentByType(input); if (computationStepType == null) { throw new IllegalStateException(String.format("Component not found: %s", input)); } return computationStepType; - } - }); + }); } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/step/BuildComponentTreeStep.java b/server/sonar-server/src/main/java/org/sonar/server/computation/step/BuildComponentTreeStep.java index c5281b479c4..1805581d6fb 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/step/BuildComponentTreeStep.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/step/BuildComponentTreeStep.java @@ -19,10 +19,8 @@ */ package org.sonar.server.computation.step; -import com.google.common.base.Function; import com.google.common.collect.Iterables; import javax.annotation.CheckForNull; -import javax.annotation.Nonnull; import javax.annotation.Nullable; import org.sonar.core.component.ComponentKeys; import org.sonar.db.DbClient; @@ -83,11 +81,10 @@ public class BuildComponentTreeStep implements ComputationStep { @CheckForNull private static Snapshot toSnapshot(@Nullable SnapshotDto snapshotDto) { - return snapshotDto == null ? null : - new Snapshot.Builder() - .setId(snapshotDto.getId()) - .setCreatedAt(snapshotDto.getCreatedAt()) - .build(); + return snapshotDto == null ? null : new Snapshot.Builder() + .setId(snapshotDto.getId()) + .setCreatedAt(snapshotDto.getCreatedAt()) + .build(); } private class ComponentRootBuilder { @@ -134,13 +131,7 @@ public class BuildComponentTreeStep implements ComputationStep { private Iterable buildChildren(ScannerReport.Component component, final String latestModuleKey) { return Iterables.transform( component.getChildRefList(), - new Function() { - @Override - public Component apply(@Nonnull Integer componentRef) { - return buildComponent(reportReader.readComponent(componentRef), latestModuleKey); - } - } - ); + componentRef -> buildComponent(reportReader.readComponent(componentRef), latestModuleKey)); } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/step/PersistEventsStep.java b/server/sonar-server/src/main/java/org/sonar/server/computation/step/PersistEventsStep.java index fba1c3da2ff..0a2d3db39d0 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/step/PersistEventsStep.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/step/PersistEventsStep.java @@ -20,7 +20,6 @@ package org.sonar.server.computation.step; import com.google.common.base.Function; -import javax.annotation.Nonnull; import org.sonar.api.utils.System2; import org.sonar.db.DbClient; import org.sonar.db.DbSession; @@ -76,17 +75,12 @@ public class PersistEventsStep implements ComputationStep { saveVersionEvent(session, component, analysisDate); } - private void processEvents(DbSession session, final Component component, final Long analysisDate) { - Function eventToEventDto = new Function() { - @Override - public EventDto apply(@Nonnull Event event) { - return newBaseEvent(component, analysisDate) - .setName(event.getName()) - .setCategory(convertCategory(event.getCategory())) - .setDescription(event.getDescription()) - .setData(event.getData()); - } - }; + private void processEvents(DbSession session, Component component, Long analysisDate) { + Function eventToEventDto = event -> newBaseEvent(component, analysisDate) + .setName(event.getName()) + .setCategory(convertCategory(event.getCategory())) + .setDescription(event.getDescription()) + .setData(event.getData()); // FIXME bulk insert for (EventDto batchEventDto : transform(eventRepository.getEvents(component), eventToEventDto)) { dbClient.eventDao().insert(session, batchEventDto); diff --git a/server/sonar-server/src/main/java/org/sonar/server/db/migrations/PlatformDatabaseMigration.java b/server/sonar-server/src/main/java/org/sonar/server/db/migrations/PlatformDatabaseMigration.java index d4420506492..b6a89fe0395 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/db/migrations/PlatformDatabaseMigration.java +++ b/server/sonar-server/src/main/java/org/sonar/server/db/migrations/PlatformDatabaseMigration.java @@ -95,12 +95,7 @@ public class PlatformDatabaseMigration implements DatabaseMigration { } running.set(true); - executorService.execute(new Runnable() { - @Override - public void run() { - doDatabaseMigration(); - } - }); + executorService.execute(this::doDatabaseMigration); } private void doDatabaseMigration() { 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 c2dd55ee00d..152c564ac02 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 @@ -46,14 +46,10 @@ public class UpdatesAction implements PluginsWsAction { private static final String ARRAY_PLUGINS = "plugins"; 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); - private static final Ordering PLUGIN_UPDATE_BY_VERSION_ORDERING = Ordering.natural().onResultOf(new Function() { - @Override - public String apply(@Nonnull PluginUpdate input) { - return input.getRelease().getVersion().toString(); - } - }); + private static final Ordering NAME_KEY_PLUGIN_UPGRADE_AGGREGATE_ORDERING = Ordering.from(PluginWSCommons.NAME_KEY_PLUGIN_ORDERING) + .onResultOf(PluginUpdateAggregateToPlugin.INSTANCE); + private static final Ordering PLUGIN_UPDATE_BY_VERSION_ORDERING = Ordering.natural() + .onResultOf(input -> input.getRelease().getVersion().toString()); private final UpdateCenterMatrixFactory updateCenterMatrixFactory; private final PluginWSCommons pluginWSCommons; @@ -135,8 +131,7 @@ public class UpdatesAction implements PluginsWsAction { // aggregates updates of the same plugin to a single object and sort these objects by plugin name then key return ImmutableSortedSet.copyOf( NAME_KEY_PLUGIN_UPGRADE_AGGREGATE_ORDERING, - aggregator.aggregate(pluginUpdates) - ); + aggregator.aggregate(pluginUpdates)); } private enum PluginUpdateAggregateToPlugin implements Function { diff --git a/server/sonar-server/src/main/java/org/sonar/server/ruby/PlatformRubyBridge.java b/server/sonar-server/src/main/java/org/sonar/server/ruby/PlatformRubyBridge.java index 2ee69ddb9cf..33d81ddd27b 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/ruby/PlatformRubyBridge.java +++ b/server/sonar-server/src/main/java/org/sonar/server/ruby/PlatformRubyBridge.java @@ -42,44 +42,29 @@ public class PlatformRubyBridge implements RubyBridge { @Override public RubyDatabaseMigration databaseMigration() { - final CallDatabaseVersionUpgrade callDatabaseVersionUpgrade = parseMethodScriptToInterface( + CallDatabaseVersionUpgrade callDatabaseVersionUpgrade = parseMethodScriptToInterface( CALL_UPGRADE_AND_START_RB_FILENAME, CallDatabaseVersionUpgrade.class ); - return new RubyDatabaseMigration() { - @Override - public void trigger() { - callDatabaseVersionUpgrade.callUpgrade(); - } - }; + return callDatabaseVersionUpgrade::callUpgrade; } @Override public RubyRailsRoutes railsRoutes() { - final CallLoadJavaWebServices callLoadJavaWebServices = parseMethodScriptToInterface( + CallLoadJavaWebServices callLoadJavaWebServices = parseMethodScriptToInterface( CALL_LOAD_JAVA_WEB_SERVICES_RB_FILENAME, CallLoadJavaWebServices.class ); - return new RubyRailsRoutes() { - @Override - public void recreate() { - callLoadJavaWebServices.callLoadJavaWebServices(); - } - }; + return callLoadJavaWebServices::callLoadJavaWebServices; } @Override public RubyMetricCache metricCache() { - final CallInvalidateMetricCache callInvalidateMetricCache = parseMethodScriptToInterface( + CallInvalidateMetricCache callInvalidateMetricCache = parseMethodScriptToInterface( CALL_INVALIDATE_METRIC_CACHE_RB_FILENAME, CallInvalidateMetricCache.class ); - return new RubyMetricCache() { - @Override - public void invalidate() { - callInvalidateMetricCache.callInvalidate(); - } - }; + return callInvalidateMetricCache::callInvalidate; } /** -- 2.39.5