diff options
author | Simon Brandhof <simon.brandhof@sonarsource.com> | 2017-01-23 22:11:32 +0100 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@sonarsource.com> | 2017-01-24 09:39:30 +0100 |
commit | 25dcd7210f298f750a3cba2ef2a57322fd470cfc (patch) | |
tree | 36c4e40e60b76c1151b3223b2ff9e73db81abde8 /server | |
parent | 978f6416fe18e9f8a507ad8fe523598129a49684 (diff) | |
download | sonarqube-25dcd7210f298f750a3cba2ef2a57322fd470cfc.tar.gz sonarqube-25dcd7210f298f750a3cba2ef2a57322fd470cfc.zip |
Replace some Guava usages by Java 8 functions
Diffstat (limited to 'server')
4 files changed, 36 insertions, 88 deletions
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<ComponentDto> refComponent, List<MeasureDto> measures, List<MetricDto> metrics, List<WsMeasures.Period> periods) { ComponentWsResponse.Builder response = ComponentWsResponse.newBuilder(); - Map<Integer, MetricDto> metricsById = Maps.uniqueIndex(metrics, MetricDtoFunctions.toId()); + Map<Integer, MetricDto> metricsById = Maps.uniqueIndex(metrics, MetricDto::getId); Map<MetricDto, MeasureDto> measuresByMetric = new HashMap<>(); for (MeasureDto measure : measures) { MetricDto metric = metricsById.get(measure.getMetricId()); @@ -199,7 +198,7 @@ public class ComponentAction implements MeasuresWsAction { private List<MetricDto> searchMetrics(DbSession dbSession, ComponentWsRequest request) { List<MetricDto> metrics = dbClient.metricDao().selectByKeys(dbSession, request.getMetricKeys()); if (metrics.size() < request.getMetricKeys().size()) { - List<String> foundMetricKeys = Lists.transform(metrics, MetricDtoFunctions.toKey()); + List<String> foundMetricKeys = Lists.transform(metrics, MetricDto::getKey); Set<String> missingMetricKeys = Sets.difference( new LinkedHashSet<>(request.getMetricKeys()), new LinkedHashSet<>(foundMetricKeys)); @@ -240,11 +239,11 @@ public class ComponentAction implements MeasuresWsAction { return; } - List<MetricDtoWithBestValue> metricWithBestValueList = from(metrics) + List<MetricDtoWithBestValue> metricWithBestValueList = metrics.stream() .filter(MetricDtoFunctions.isOptimizedForBestValue()) - .transform(new MetricDtoToMetricDtoWithBestValueFunction(periods)) - .toList(); - Map<Integer, MeasureDto> measuresByMetricId = Maps.uniqueIndex(measures, MeasureDtoFunctions.toMetricId()); + .map(new MetricDtoToMetricDtoWithBestValueFunction(periods)) + .collect(Collectors.toList(metrics.size())); + Map<Integer, MeasureDto> 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<SnapshotDto> baseSnapshot = dbClient.snapshotDao().selectLastAnalysisByRootComponentUuid(dbSession, baseComponent.projectUuid()); + Optional<SnapshotDto> baseSnapshot = dbClient.snapshotDao().selectLastAnalysisByRootComponentUuid(dbSession, baseComponent.projectUuid()); if (!baseSnapshot.isPresent()) { return ComponentTreeData.builder() .setBaseComponent(baseComponent) @@ -141,10 +141,10 @@ public class ComponentTreeDataLoader { } private Map<String, ComponentDto> searchReferenceComponentsById(DbSession dbSession, List<ComponentDto> components) { - List<String> referenceComponentUUids = from(components) - .transform(ComponentDto::getCopyResourceUuid) - .filter(Predicates.<String>notNull()) - .toList(); + List<String> 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<String> metricKeys = requireNonNull(request.getMetricKeys()); List<MetricDto> metrics = dbClient.metricDao().selectByKeys(dbSession, metricKeys); if (metrics.size() < metricKeys.size()) { - List<String> foundMetricKeys = Lists.transform(metrics, MetricDtoFunctions.toKey()); + List<String> foundMetricKeys = Lists.transform(metrics, MetricDto::getKey); Set<String> missingMetricKeys = Sets.difference( new LinkedHashSet<>(metricKeys), new LinkedHashSet<>(foundMetricKeys)); @@ -179,7 +179,7 @@ public class ComponentTreeDataLoader { private Table<String, MetricDto, MeasureDto> searchMeasuresByComponentUuidAndMetric(DbSession dbSession, ComponentDto baseComponent, ComponentTreeQuery componentTreeQuery, List<ComponentDto> components, List<MetricDto> metrics, List<WsMeasures.Period> periods, @Nullable Long developerId) { - Map<Integer, MetricDto> metricsById = Maps.uniqueIndex(metrics, MetricDtoFunctions.toId()); + Map<Integer, MetricDto> 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<String, MetricDto, MeasureDto> measuresByComponentUuidAndMetric, List<ComponentDto> components, List<MetricDto> metrics, List<WsMeasures.Period> periods) { - List<MetricDtoWithBestValue> metricDtosWithBestValueMeasure = from(metrics) + List<MetricDtoWithBestValue> 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<ComponentDto> componentsEligibleForBestValue = from(components).filter(IsFileComponent.INSTANCE).toList(); - for (ComponentDto component : componentsEligibleForBestValue) { + Stream<ComponentDto> 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<ComponentDto> filterComponents(List<ComponentDto> components, @@ -235,14 +235,14 @@ public class ComponentTreeDataLoader { return components; } - final String metricKeyToSort = wsRequest.getMetricSort(); - Optional<MetricDto> metricToSort = from(metrics).firstMatch(new MatchMetricKey(metricKeyToSort)); + String metricKeyToSort = wsRequest.getMetricSort(); + Optional<MetricDto> 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<ComponentDto> paginateComponents(List<ComponentDto> 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<Integer> periodIndexes; MetricDtoToMetricDtoWithBestValue(List<WsMeasures.Period> 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<WsMeasures.Period, Integer> { - INSTANCE; - - @Override - public Integer apply(@Nonnull WsMeasures.Period input) { - return input.getIndex(); - } - } - - private static class MatchMetricKey implements Predicate<MetricDto> { - 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<String, MetricDto> metricsByKey = Maps.uniqueIndex(metrics, MetricDtoFunctions.toKey()); + Map<String, MetricDto> 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<String, MetricDto> metricsByKey = Maps.uniqueIndex(metrics, MetricDtoFunctions.toKey()); + Map<String, MetricDto> 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<String> 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<MetricDto, MeasureDto> buildBestMeasure(ComponentDto component, Collection<WsMeasures.Period> 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<ComponentDto> isEligibleForBestValue() { - return component -> QUALIFIERS_ELIGIBLE_FOR_BEST_VALUE.contains(component.qualifier()); - } - static class MetricDtoToMetricDtoWithBestValueFunction implements Function<MetricDto, MetricDtoWithBestValue> { private final List<Integer> periodIndexes; |