From 25dcd7210f298f750a3cba2ef2a57322fd470cfc Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Mon, 23 Jan 2017 22:11:32 +0100 Subject: [PATCH] Replace some Guava usages by Java 8 functions --- .../server/measure/ws/ComponentAction.java | 15 ++-- .../measure/ws/ComponentTreeDataLoader.java | 74 +++++++------------ .../server/measure/ws/ComponentTreeSort.java | 5 +- .../measure/ws/MetricDtoWithBestValue.java | 30 +------- .../sonar/db/measure/MeasureDtoFunctions.java | 42 ----------- .../sonar/db/metric/MetricDtoFunctions.java | 30 +------- .../db/component/ComponentLinkDaoTest.java | 6 +- .../db/metric/MetricDtoFunctionsTest.java | 26 +------ 8 files changed, 45 insertions(+), 183 deletions(-) delete mode 100644 sonar-db/src/main/java/org/sonar/db/measure/MeasureDtoFunctions.java diff --git a/server/sonar-server/src/main/java/org/sonar/server/measure/ws/ComponentAction.java b/server/sonar-server/src/main/java/org/sonar/server/measure/ws/ComponentAction.java index f3ff13a3669..dfd151a35cd 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/measure/ws/ComponentAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/measure/ws/ComponentAction.java @@ -38,12 +38,12 @@ import org.sonar.api.server.ws.Response; import org.sonar.api.server.ws.WebService; import org.sonar.api.web.UserRole; import org.sonar.core.permission.GlobalPermissions; +import org.sonar.core.util.stream.Collectors; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.component.ComponentDto; import org.sonar.db.component.SnapshotDto; import org.sonar.db.measure.MeasureDto; -import org.sonar.db.measure.MeasureDtoFunctions; import org.sonar.db.measure.MeasureQuery; import org.sonar.db.metric.MetricDto; import org.sonar.db.metric.MetricDtoFunctions; @@ -56,7 +56,6 @@ import org.sonarqube.ws.WsMeasures.ComponentWsResponse; import org.sonarqube.ws.client.measure.ComponentWsRequest; import static com.google.common.base.MoreObjects.firstNonNull; -import static com.google.common.collect.FluentIterable.from; import static java.lang.String.format; import static java.util.Collections.emptyList; import static java.util.Collections.emptyMap; @@ -169,7 +168,7 @@ public class ComponentAction implements MeasuresWsAction { private static ComponentWsResponse buildResponse(ComponentWsRequest request, ComponentDto component, Optional refComponent, List measures, List metrics, List periods) { ComponentWsResponse.Builder response = ComponentWsResponse.newBuilder(); - Map metricsById = Maps.uniqueIndex(metrics, MetricDtoFunctions.toId()); + Map metricsById = Maps.uniqueIndex(metrics, MetricDto::getId); Map measuresByMetric = new HashMap<>(); for (MeasureDto measure : measures) { MetricDto metric = metricsById.get(measure.getMetricId()); @@ -199,7 +198,7 @@ public class ComponentAction implements MeasuresWsAction { private List searchMetrics(DbSession dbSession, ComponentWsRequest request) { List metrics = dbClient.metricDao().selectByKeys(dbSession, request.getMetricKeys()); if (metrics.size() < request.getMetricKeys().size()) { - List foundMetricKeys = Lists.transform(metrics, MetricDtoFunctions.toKey()); + List foundMetricKeys = Lists.transform(metrics, MetricDto::getKey); Set missingMetricKeys = Sets.difference( new LinkedHashSet<>(request.getMetricKeys()), new LinkedHashSet<>(foundMetricKeys)); @@ -240,11 +239,11 @@ public class ComponentAction implements MeasuresWsAction { return; } - List metricWithBestValueList = from(metrics) + List metricWithBestValueList = metrics.stream() .filter(MetricDtoFunctions.isOptimizedForBestValue()) - .transform(new MetricDtoToMetricDtoWithBestValueFunction(periods)) - .toList(); - Map measuresByMetricId = Maps.uniqueIndex(measures, MeasureDtoFunctions.toMetricId()); + .map(new MetricDtoToMetricDtoWithBestValueFunction(periods)) + .collect(Collectors.toList(metrics.size())); + Map measuresByMetricId = Maps.uniqueIndex(measures, MeasureDto::getMetricId); for (MetricDtoWithBestValue metricWithBestValue : metricWithBestValueList) { if (measuresByMetricId.get(metricWithBestValue.getMetric().getId()) == null) { diff --git a/server/sonar-server/src/main/java/org/sonar/server/measure/ws/ComponentTreeDataLoader.java b/server/sonar-server/src/main/java/org/sonar/server/measure/ws/ComponentTreeDataLoader.java index 201b1f2a6a9..911e31500bf 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/measure/ws/ComponentTreeDataLoader.java +++ b/server/sonar-server/src/main/java/org/sonar/server/measure/ws/ComponentTreeDataLoader.java @@ -19,11 +19,7 @@ */ package org.sonar.server.measure.ws; -import com.google.common.base.Function; import com.google.common.base.Joiner; -import com.google.common.base.Optional; -import com.google.common.base.Predicate; -import com.google.common.base.Predicates; import com.google.common.collect.FluentIterable; import com.google.common.collect.HashBasedTable; import com.google.common.collect.Lists; @@ -37,14 +33,19 @@ import java.util.HashSet; import java.util.LinkedHashSet; import java.util.List; import java.util.Map; +import java.util.Objects; +import java.util.Optional; import java.util.Set; -import java.util.stream.Collectors; +import java.util.function.Function; +import java.util.function.Predicate; +import java.util.stream.Stream; import javax.annotation.CheckForNull; import javax.annotation.Nonnull; import javax.annotation.Nullable; import org.sonar.api.resources.Qualifiers; import org.sonar.api.resources.ResourceTypes; import org.sonar.api.web.UserRole; +import org.sonar.core.util.stream.Collectors; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.component.ComponentDto; @@ -62,7 +63,6 @@ import org.sonarqube.ws.client.measure.ComponentTreeWsRequest; import static com.google.common.base.MoreObjects.firstNonNull; import static com.google.common.base.Preconditions.checkState; -import static com.google.common.collect.FluentIterable.from; import static com.google.common.collect.Sets.newHashSet; import static java.lang.String.format; import static java.util.Collections.emptyMap; @@ -96,7 +96,7 @@ public class ComponentTreeDataLoader { try { ComponentDto baseComponent = componentFinder.getByUuidOrKey(dbSession, wsRequest.getBaseComponentId(), wsRequest.getBaseComponentKey(), BASE_COMPONENT_ID_AND_KEY); checkPermissions(baseComponent); - java.util.Optional baseSnapshot = dbClient.snapshotDao().selectLastAnalysisByRootComponentUuid(dbSession, baseComponent.projectUuid()); + Optional baseSnapshot = dbClient.snapshotDao().selectLastAnalysisByRootComponentUuid(dbSession, baseComponent.projectUuid()); if (!baseSnapshot.isPresent()) { return ComponentTreeData.builder() .setBaseComponent(baseComponent) @@ -141,10 +141,10 @@ public class ComponentTreeDataLoader { } private Map searchReferenceComponentsById(DbSession dbSession, List components) { - List referenceComponentUUids = from(components) - .transform(ComponentDto::getCopyResourceUuid) - .filter(Predicates.notNull()) - .toList(); + List referenceComponentUUids = components.stream() + .map(ComponentDto::getCopyResourceUuid) + .filter(Objects::nonNull) + .collect(Collectors.toList(components.size())); if (referenceComponentUUids.isEmpty()) { return emptyMap(); } @@ -165,7 +165,7 @@ public class ComponentTreeDataLoader { List metricKeys = requireNonNull(request.getMetricKeys()); List metrics = dbClient.metricDao().selectByKeys(dbSession, metricKeys); if (metrics.size() < metricKeys.size()) { - List foundMetricKeys = Lists.transform(metrics, MetricDtoFunctions.toKey()); + List foundMetricKeys = Lists.transform(metrics, MetricDto::getKey); Set missingMetricKeys = Sets.difference( new LinkedHashSet<>(metricKeys), new LinkedHashSet<>(foundMetricKeys)); @@ -179,7 +179,7 @@ public class ComponentTreeDataLoader { private Table searchMeasuresByComponentUuidAndMetric(DbSession dbSession, ComponentDto baseComponent, ComponentTreeQuery componentTreeQuery, List components, List metrics, List periods, @Nullable Long developerId) { - Map metricsById = Maps.uniqueIndex(metrics, MetricDtoFunctions.toId()); + Map metricsById = Maps.uniqueIndex(metrics, MetricDto::getId); MeasureTreeQuery measureQuery = MeasureTreeQuery.builder() .setStrategy(MeasureTreeQuery.Strategy.valueOf(componentTreeQuery.getStrategy().name())) .setNameOrKeyQuery(componentTreeQuery.getNameOrKeyQuery()) @@ -211,22 +211,22 @@ public class ComponentTreeDataLoader { */ private static void addBestValuesToMeasures(Table measuresByComponentUuidAndMetric, List components, List metrics, List periods) { - List metricDtosWithBestValueMeasure = from(metrics) + List metricDtosWithBestValueMeasure = metrics.stream() .filter(MetricDtoFunctions.isOptimizedForBestValue()) - .transform(new MetricDtoToMetricDtoWithBestValue(periods)) - .toList(); + .map(new MetricDtoToMetricDtoWithBestValue(periods)) + .collect(Collectors.toList(metrics.size())); if (metricDtosWithBestValueMeasure.isEmpty()) { return; } - List componentsEligibleForBestValue = from(components).filter(IsFileComponent.INSTANCE).toList(); - for (ComponentDto component : componentsEligibleForBestValue) { + Stream componentsEligibleForBestValue = components.stream().filter(IsFileComponent.INSTANCE); + componentsEligibleForBestValue.forEach(component -> { for (MetricDtoWithBestValue metricWithBestValue : metricDtosWithBestValueMeasure) { if (measuresByComponentUuidAndMetric.get(component.uuid(), metricWithBestValue.getMetric()) == null) { measuresByComponentUuidAndMetric.put(component.uuid(), metricWithBestValue.getMetric(), metricWithBestValue.getBestValue()); } } - } + }); } private static List filterComponents(List components, @@ -235,14 +235,14 @@ public class ComponentTreeDataLoader { return components; } - final String metricKeyToSort = wsRequest.getMetricSort(); - Optional metricToSort = from(metrics).firstMatch(new MatchMetricKey(metricKeyToSort)); + String metricKeyToSort = wsRequest.getMetricSort(); + Optional metricToSort = metrics.stream().filter(m -> metricKeyToSort.equals(m.getKey())).findFirst(); checkState(metricToSort.isPresent(), "Metric '%s' not found", metricKeyToSort, wsRequest.getMetricKeys()); return components .stream() .filter(new HasMeasure(measuresByComponentUuidAndMetric, metricToSort.get(), wsRequest)) - .collect(Collectors.toList()); + .collect(Collectors.toList(components.size())); } private static boolean componentWithMeasuresOnly(ComponentTreeWsRequest wsRequest) { @@ -255,10 +255,10 @@ public class ComponentTreeDataLoader { } private static List paginateComponents(List components, ComponentTreeWsRequest wsRequest) { - return from(components) + return components.stream() .skip(offset(wsRequest.getPage(), wsRequest.getPageSize())) .limit(wsRequest.getPageSize()) - .toList(); + .collect(Collectors.toList(wsRequest.getPageSize())); } @CheckForNull @@ -310,7 +310,7 @@ public class ComponentTreeDataLoader { INSTANCE; @Override - public boolean apply(@Nonnull ComponentDto input) { + public boolean test(@Nonnull ComponentDto input) { return QUALIFIERS_ELIGIBLE_FOR_BEST_VALUE.contains(input.qualifier()); } } @@ -319,7 +319,7 @@ public class ComponentTreeDataLoader { private final List periodIndexes; MetricDtoToMetricDtoWithBestValue(List periods) { - this.periodIndexes = Lists.transform(periods, WsPeriodToIndex.INSTANCE); + this.periodIndexes = Lists.transform(periods, WsMeasures.Period::getIndex); } @Override @@ -327,26 +327,4 @@ public class ComponentTreeDataLoader { return new MetricDtoWithBestValue(input, periodIndexes); } } - - private enum WsPeriodToIndex implements Function { - INSTANCE; - - @Override - public Integer apply(@Nonnull WsMeasures.Period input) { - return input.getIndex(); - } - } - - private static class MatchMetricKey implements Predicate { - private final String metricKeyToSort; - - private MatchMetricKey(String metricKeyToSort) { - this.metricKeyToSort = metricKeyToSort; - } - - @Override - public boolean apply(@Nonnull MetricDto input) { - return input.getKey().equals(metricKeyToSort); - } - } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/measure/ws/ComponentTreeSort.java b/server/sonar-server/src/main/java/org/sonar/server/measure/ws/ComponentTreeSort.java index be1d15aaf97..6167295a3e6 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/measure/ws/ComponentTreeSort.java +++ b/server/sonar-server/src/main/java/org/sonar/server/measure/ws/ComponentTreeSort.java @@ -35,7 +35,6 @@ import org.sonar.api.measures.Metric.ValueType; import org.sonar.db.component.ComponentDto; import org.sonar.db.measure.MeasureDto; import org.sonar.db.metric.MetricDto; -import org.sonar.db.metric.MetricDtoFunctions; import org.sonar.server.exceptions.BadRequestException; import org.sonarqube.ws.client.measure.ComponentTreeWsRequest; @@ -120,7 +119,7 @@ public class ComponentTreeSort { if (wsRequest.getMetricSort() == null) { return componentNameOrdering(wsRequest.getAsc()); } - Map metricsByKey = Maps.uniqueIndex(metrics, MetricDtoFunctions.toKey()); + Map metricsByKey = Maps.uniqueIndex(metrics, MetricDto::getKey); MetricDto metric = metricsByKey.get(wsRequest.getMetricSort()); boolean isAscending = wsRequest.getAsc(); @@ -141,7 +140,7 @@ public class ComponentTreeSort { if (wsRequest.getMetricSort() == null || wsRequest.getMetricPeriodSort() == null) { return componentNameOrdering(wsRequest.getAsc()); } - Map metricsByKey = Maps.uniqueIndex(metrics, MetricDtoFunctions.toKey()); + Map metricsByKey = Maps.uniqueIndex(metrics, MetricDto::getKey); MetricDto metric = metricsByKey.get(wsRequest.getMetricSort()); ValueType metricValueType = ValueType.valueOf(metric.getValueType()); diff --git a/server/sonar-server/src/main/java/org/sonar/server/measure/ws/MetricDtoWithBestValue.java b/server/sonar-server/src/main/java/org/sonar/server/measure/ws/MetricDtoWithBestValue.java index 1f73dcb0109..bef88a5bd8c 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/measure/ws/MetricDtoWithBestValue.java +++ b/server/sonar-server/src/main/java/org/sonar/server/measure/ws/MetricDtoWithBestValue.java @@ -19,24 +19,17 @@ */ package org.sonar.server.measure.ws; -import com.google.common.base.Function; -import com.google.common.collect.ImmutableSortedSet; -import java.util.Collection; import java.util.List; import java.util.Locale; -import java.util.Set; -import java.util.function.Predicate; +import java.util.function.Function; import java.util.stream.Collectors; import javax.annotation.Nonnull; -import org.sonar.api.resources.Qualifiers; -import org.sonar.db.component.ComponentDto; import org.sonar.db.measure.MeasureDto; import org.sonar.db.metric.MetricDto; import org.sonarqube.ws.WsMeasures; class MetricDtoWithBestValue { private static final String LOWER_CASE_NEW_METRIC_PREFIX = "new_"; - private static final Set QUALIFIERS_ELIGIBLE_FOR_BEST_VALUE = ImmutableSortedSet.of(Qualifiers.FILE, Qualifiers.UNIT_TEST_FILE); private final MetricDto metric; private final MeasureDto bestValue; @@ -64,27 +57,6 @@ class MetricDtoWithBestValue { return bestValue; } - static java.util.function.Function buildBestMeasure(ComponentDto component, Collection periods) { - return metric -> { - MeasureDto measure = new MeasureDto() - .setMetricId(metric.getId()) - .setComponentUuid(component.uuid()); - boolean isNewTypeMetric = metric.getKey().toLowerCase(Locale.ENGLISH).startsWith(LOWER_CASE_NEW_METRIC_PREFIX); - if (isNewTypeMetric) { - periods.stream() - .map(WsMeasures.Period::getIndex) - .forEach(index -> measure.setVariation(index, 0.0d)); - } else { - measure.setValue(metric.getBestValue()); - } - return measure; - }; - } - - static Predicate isEligibleForBestValue() { - return component -> QUALIFIERS_ELIGIBLE_FOR_BEST_VALUE.contains(component.qualifier()); - } - static class MetricDtoToMetricDtoWithBestValueFunction implements Function { private final List periodIndexes; diff --git a/sonar-db/src/main/java/org/sonar/db/measure/MeasureDtoFunctions.java b/sonar-db/src/main/java/org/sonar/db/measure/MeasureDtoFunctions.java deleted file mode 100644 index 6a189b51c92..00000000000 --- a/sonar-db/src/main/java/org/sonar/db/measure/MeasureDtoFunctions.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.db.measure; - -import com.google.common.base.Function; -import javax.annotation.Nonnull; - -public class MeasureDtoFunctions { - private MeasureDtoFunctions() { - // prevent instantiation - } - - public static Function toMetricId() { - return ToMetricId.INSTANCE; - } - - private enum ToMetricId implements Function { - INSTANCE; - - @Override - public Integer apply(@Nonnull MeasureDto input) { - return input.getMetricId(); - } - } -} diff --git a/sonar-db/src/main/java/org/sonar/db/metric/MetricDtoFunctions.java b/sonar-db/src/main/java/org/sonar/db/metric/MetricDtoFunctions.java index f2111f7066f..f19fed07105 100644 --- a/sonar-db/src/main/java/org/sonar/db/metric/MetricDtoFunctions.java +++ b/sonar-db/src/main/java/org/sonar/db/metric/MetricDtoFunctions.java @@ -19,8 +19,7 @@ */ package org.sonar.db.metric; -import com.google.common.base.Function; -import com.google.common.base.Predicate; +import java.util.function.Predicate; import javax.annotation.Nonnull; /** @@ -31,41 +30,16 @@ public class MetricDtoFunctions { // prevents instantiation } - public static Function toId() { - return ToId.INSTANCE; - } - - public static Function toKey() { - return ToKey.INSTANCE; - } public static Predicate isOptimizedForBestValue() { return IsMetricOptimizedForBestValue.INSTANCE; } - private enum ToId implements Function { - INSTANCE; - - @Override - public Integer apply(@Nonnull MetricDto input) { - return input.getId(); - } - } - - private enum ToKey implements Function { - INSTANCE; - - @Override - public String apply(@Nonnull MetricDto input) { - return input.getKey(); - } - } - private enum IsMetricOptimizedForBestValue implements Predicate { INSTANCE; @Override - public boolean apply(@Nonnull MetricDto input) { + public boolean test(@Nonnull MetricDto input) { return input.isOptimizedBestValue() && input.getBestValue() != null; } } diff --git a/sonar-db/src/test/java/org/sonar/db/component/ComponentLinkDaoTest.java b/sonar-db/src/test/java/org/sonar/db/component/ComponentLinkDaoTest.java index c24788ceabe..45ce6d1eed1 100644 --- a/sonar-db/src/test/java/org/sonar/db/component/ComponentLinkDaoTest.java +++ b/sonar-db/src/test/java/org/sonar/db/component/ComponentLinkDaoTest.java @@ -39,10 +39,10 @@ public class ComponentLinkDaoTest { @Rule public DbTester db = DbTester.create(System2.INSTANCE); - DbClient dbClient = db.getDbClient(); - DbSession dbSession = db.getSession(); + private DbClient dbClient = db.getDbClient(); + private DbSession dbSession = db.getSession(); - ComponentLinkDao underTest = db.getDbClient().componentLinkDao(); + private ComponentLinkDao underTest = db.getDbClient().componentLinkDao(); @Test public void select_by_component_uuid() { diff --git a/sonar-db/src/test/java/org/sonar/db/metric/MetricDtoFunctionsTest.java b/sonar-db/src/test/java/org/sonar/db/metric/MetricDtoFunctionsTest.java index 386b6b604c6..69b676fc886 100644 --- a/sonar-db/src/test/java/org/sonar/db/metric/MetricDtoFunctionsTest.java +++ b/sonar-db/src/test/java/org/sonar/db/metric/MetricDtoFunctionsTest.java @@ -26,25 +26,7 @@ import static org.assertj.core.api.Assertions.assertThat; public class MetricDtoFunctionsTest { - MetricDto metric; - - @Test - public void toId() { - metric = new MetricDto().setId(42); - - Integer result = MetricDtoFunctions.toId().apply(metric); - - assertThat(result).isEqualTo(42); - } - - @Test - public void toKey() { - metric = new MetricDto().setKey("my-metric-key"); - - String result = MetricDtoFunctions.toKey().apply(metric); - - assertThat(result).isEqualTo("my-metric-key"); - } + private MetricDto metric; @Test public void isOptimizedForBestValue_at_true() { @@ -52,7 +34,7 @@ public class MetricDtoFunctionsTest { .setBestValue(42.0d) .setOptimizedBestValue(true); - boolean result = MetricDtoFunctions.isOptimizedForBestValue().apply(metric); + boolean result = MetricDtoFunctions.isOptimizedForBestValue().test(metric); assertThat(result).isTrue(); } @@ -63,7 +45,7 @@ public class MetricDtoFunctionsTest { .setBestValue(null) .setOptimizedBestValue(true); - boolean result = MetricDtoFunctions.isOptimizedForBestValue().apply(metric); + boolean result = MetricDtoFunctions.isOptimizedForBestValue().test(metric); assertThat(result).isFalse(); } @@ -74,7 +56,7 @@ public class MetricDtoFunctionsTest { .setBestValue(42.0d) .setOptimizedBestValue(false); - boolean result = MetricDtoFunctions.isOptimizedForBestValue().apply(metric); + boolean result = MetricDtoFunctions.isOptimizedForBestValue().test(metric); assertThat(result).isFalse(); } -- 2.39.5