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;
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;
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());
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));
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) {
*/
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;
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;
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;
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)
}
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();
}
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));
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())
*/
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,
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) {
}
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
INSTANCE;
@Override
- public boolean apply(@Nonnull ComponentDto input) {
+ public boolean test(@Nonnull ComponentDto input) {
return QUALIFIERS_ELIGIBLE_FOR_BEST_VALUE.contains(input.qualifier());
}
}
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
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);
- }
- }
}
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;
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();
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());
*/
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;
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;
+++ /dev/null
-/*
- * 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<MeasureDto, Integer> toMetricId() {
- return ToMetricId.INSTANCE;
- }
-
- private enum ToMetricId implements Function<MeasureDto, Integer> {
- INSTANCE;
-
- @Override
- public Integer apply(@Nonnull MeasureDto input) {
- return input.getMetricId();
- }
- }
-}
*/
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;
/**
// prevents instantiation
}
- public static Function<MetricDto, Integer> toId() {
- return ToId.INSTANCE;
- }
-
- public static Function<MetricDto, String> toKey() {
- return ToKey.INSTANCE;
- }
public static Predicate<MetricDto> isOptimizedForBestValue() {
return IsMetricOptimizedForBestValue.INSTANCE;
}
- private enum ToId implements Function<MetricDto, Integer> {
- INSTANCE;
-
- @Override
- public Integer apply(@Nonnull MetricDto input) {
- return input.getId();
- }
- }
-
- private enum ToKey implements Function<MetricDto, String> {
- INSTANCE;
-
- @Override
- public String apply(@Nonnull MetricDto input) {
- return input.getKey();
- }
- }
-
private enum IsMetricOptimizedForBestValue implements Predicate<MetricDto> {
INSTANCE;
@Override
- public boolean apply(@Nonnull MetricDto input) {
+ public boolean test(@Nonnull MetricDto input) {
return input.isOptimizedBestValue() && input.getBestValue() != null;
}
}
@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() {
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() {
.setBestValue(42.0d)
.setOptimizedBestValue(true);
- boolean result = MetricDtoFunctions.isOptimizedForBestValue().apply(metric);
+ boolean result = MetricDtoFunctions.isOptimizedForBestValue().test(metric);
assertThat(result).isTrue();
}
.setBestValue(null)
.setOptimizedBestValue(true);
- boolean result = MetricDtoFunctions.isOptimizedForBestValue().apply(metric);
+ boolean result = MetricDtoFunctions.isOptimizedForBestValue().test(metric);
assertThat(result).isFalse();
}
.setBestValue(42.0d)
.setOptimizedBestValue(false);
- boolean result = MetricDtoFunctions.isOptimizedForBestValue().apply(metric);
+ boolean result = MetricDtoFunctions.isOptimizedForBestValue().test(metric);
assertThat(result).isFalse();
}