From 2cb89afa584faf12559473030b6a82db3557deff Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Thu, 16 Jun 2016 14:28:17 +0200 Subject: [PATCH] Fix quality flaws --- .../sonar/server/ce/ws/ActivityAction.java | 34 +++---- .../org/sonar/server/ce/ws/TaskFormatter.java | 99 +++++-------------- 2 files changed, 41 insertions(+), 92 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/ce/ws/ActivityAction.java b/server/sonar-server/src/main/java/org/sonar/server/ce/ws/ActivityAction.java index 2a40aa7308b..038ff9fb24a 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/ce/ws/ActivityAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/ce/ws/ActivityAction.java @@ -20,6 +20,21 @@ package org.sonar.server.ce.ws; +import static java.lang.String.format; +import static java.util.Collections.singletonList; +import static org.apache.commons.lang.StringUtils.defaultString; +import static org.sonar.api.utils.DateUtils.parseDateQuietly; +import static org.sonar.api.utils.DateUtils.parseDateTimeQuietly; +import static org.sonar.server.ws.WsUtils.checkRequest; +import static org.sonar.server.ws.WsUtils.writeProtobuf; +import static org.sonarqube.ws.client.ce.CeWsParameters.PARAM_COMPONENT_ID; +import static org.sonarqube.ws.client.ce.CeWsParameters.PARAM_COMPONENT_QUERY; +import static org.sonarqube.ws.client.ce.CeWsParameters.PARAM_MAX_EXECUTED_AT; +import static org.sonarqube.ws.client.ce.CeWsParameters.PARAM_MIN_SUBMITTED_AT; +import static org.sonarqube.ws.client.ce.CeWsParameters.PARAM_ONLY_CURRENTS; +import static org.sonarqube.ws.client.ce.CeWsParameters.PARAM_STATUS; +import static org.sonarqube.ws.client.ce.CeWsParameters.PARAM_TYPE; + import com.google.common.base.Joiner; import com.google.common.base.Optional; import com.google.common.collect.ImmutableList; @@ -56,21 +71,6 @@ import org.sonarqube.ws.WsCe; import org.sonarqube.ws.WsCe.ActivityResponse; import org.sonarqube.ws.client.ce.ActivityWsRequest; -import static java.lang.String.format; -import static java.util.Collections.singletonList; -import static org.apache.commons.lang.StringUtils.defaultString; -import static org.sonar.api.utils.DateUtils.parseDateQuietly; -import static org.sonar.api.utils.DateUtils.parseDateTimeQuietly; -import static org.sonar.server.ws.WsUtils.checkRequest; -import static org.sonar.server.ws.WsUtils.writeProtobuf; -import static org.sonarqube.ws.client.ce.CeWsParameters.PARAM_COMPONENT_ID; -import static org.sonarqube.ws.client.ce.CeWsParameters.PARAM_COMPONENT_QUERY; -import static org.sonarqube.ws.client.ce.CeWsParameters.PARAM_MAX_EXECUTED_AT; -import static org.sonarqube.ws.client.ce.CeWsParameters.PARAM_MIN_SUBMITTED_AT; -import static org.sonarqube.ws.client.ce.CeWsParameters.PARAM_ONLY_CURRENTS; -import static org.sonarqube.ws.client.ce.CeWsParameters.PARAM_STATUS; -import static org.sonarqube.ws.client.ce.CeWsParameters.PARAM_TYPE; - public class ActivityAction implements CeWsAction { private static final int OFFSET = 0; private static final int MAX_PAGE_SIZE = 1000; @@ -242,12 +242,12 @@ public class ActivityAction implements CeWsAction { return null; } - private Iterable loadQueuedTasks(DbSession dbSession, ActivityWsRequest request, CeTaskQuery query) { + private List loadQueuedTasks(DbSession dbSession, ActivityWsRequest request, CeTaskQuery query) { List dtos = dbClient.ceQueueDao().selectByQueryInDescOrder(dbSession, query, request.getPageSize()); return formatter.formatQueue(dbSession, dtos); } - private Iterable loadPastTasks(DbSession dbSession, ActivityWsRequest request, CeTaskQuery query) { + private List loadPastTasks(DbSession dbSession, ActivityWsRequest request, CeTaskQuery query) { List dtos = dbClient.ceActivityDao().selectByQuery(dbSession, query, OFFSET, request.getPageSize()); return formatter.formatActivity(dbSession, dtos); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/ce/ws/TaskFormatter.java b/server/sonar-server/src/main/java/org/sonar/server/ce/ws/TaskFormatter.java index 9e1fb53a750..12c249b7868 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/ce/ws/TaskFormatter.java +++ b/server/sonar-server/src/main/java/org/sonar/server/ce/ws/TaskFormatter.java @@ -19,32 +19,29 @@ */ package org.sonar.server.ce.ws; -import com.google.common.base.Function; import com.google.common.base.Optional; import com.google.common.collect.ImmutableMap; import java.util.Collections; import java.util.Date; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.Set; +import java.util.function.Function; +import java.util.stream.Collectors; import javax.annotation.CheckForNull; -import javax.annotation.Nonnull; import javax.annotation.Nullable; import org.sonar.api.utils.DateUtils; import org.sonar.api.utils.System2; +import org.sonar.ce.log.CeLogging; +import org.sonar.ce.log.LogFileRef; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.ce.CeActivityDto; import org.sonar.db.ce.CeQueueDto; import org.sonar.db.component.ComponentDto; -import org.sonar.db.component.ComponentDtoFunctions; -import org.sonar.ce.log.CeLogging; -import org.sonar.ce.log.LogFileRef; import org.sonarqube.ws.WsCe; -import static com.google.common.base.Predicates.notNull; -import static com.google.common.collect.FluentIterable.from; - /** * Converts {@link CeActivityDto} and {@link CeQueueDto} to the protobuf objects * used to write WS responses (see ws-ce.proto in module sonar-ws) @@ -61,10 +58,9 @@ public class TaskFormatter { this.system2 = system2; } - public Iterable formatQueue(DbSession dbSession, List dtos) { + public List formatQueue(DbSession dbSession, List dtos) { ComponentDtoCache cache = new ComponentDtoCache(dbSession, ceQueueDtoToComponentUuids(dtos)); - return from(dtos) - .transform(new CeQueueDtoToTask(cache)); + return dtos.stream().map(input -> formatQueue(input, cache)).collect(Collectors.toList()); } public WsCe.Task formatQueue(DbSession dbSession, CeQueueDto dto) { @@ -100,9 +96,9 @@ public class TaskFormatter { return formatActivity(dto, new ComponentDtoCache(dbSession, dto.getComponentUuid())); } - public Iterable formatActivity(DbSession dbSession, List dtos) { + public List formatActivity(DbSession dbSession, List dtos) { ComponentDtoCache cache = new ComponentDtoCache(dbSession, ceActivityDtoToComponentUuids(dtos)); - return from(dtos).transform(new CeActivityDtoToTask(cache)); + return dtos.stream().map(input -> formatActivity(input, cache)).collect(Collectors.toList()); } private WsCe.Task formatActivity(CeActivityDto dto, ComponentDtoCache componentDtoCache) { @@ -142,50 +138,30 @@ public class TaskFormatter { } } - private static Set ceQueueDtoToComponentUuids(Iterable dtos) { - return from(dtos) - .transform(CeQueueDtoToComponentUuid.INSTANCE) - .filter(notNull()) - .toSet(); - } - - private static Set ceActivityDtoToComponentUuids(Iterable dtos) { - return from(dtos) - .transform(CeActivityDtoToComponentUuid.INSTANCE) - .filter(notNull()) - .toSet(); - } - - private enum CeQueueDtoToComponentUuid implements Function { - INSTANCE; - - @Override - @Nullable - public String apply(@Nonnull CeQueueDto input) { - return input.getComponentUuid(); - } + private static Set ceQueueDtoToComponentUuids(List dtos) { + return dtos.stream().map(CeQueueDto::getComponentUuid) + .filter(Objects::nonNull) + .collect(Collectors.toSet()); } - private enum CeActivityDtoToComponentUuid implements Function { - INSTANCE; - - @Override - @Nullable - public String apply(@Nonnull CeActivityDto input) { - return input.getComponentUuid(); - } + private static Set ceActivityDtoToComponentUuids(List dtos) { + return dtos.stream().map(CeActivityDto::getComponentUuid) + .filter(Objects::nonNull) + .collect(Collectors.toSet()); } private class ComponentDtoCache { private final Map componentsByUuid; - public ComponentDtoCache(DbSession dbSession, Set uuids) { - this.componentsByUuid = from(dbClient.componentDao().selectByUuids(dbSession, uuids)).uniqueIndex(ComponentDtoFunctions.toUuid()); + ComponentDtoCache(DbSession dbSession, Set uuids) { + this.componentsByUuid = dbClient.componentDao().selectByUuids(dbSession, uuids) + .stream() + .collect(Collectors.toMap(ComponentDto::uuid, Function.identity())); } - public ComponentDtoCache(DbSession dbSession, String uuid) { + ComponentDtoCache(DbSession dbSession, String uuid) { Optional componentDto = dbClient.componentDao().selectByUuid(dbSession, uuid); - this.componentsByUuid = componentDto.isPresent() ? ImmutableMap.of(uuid, componentDto.get()) : Collections.emptyMap(); + this.componentsByUuid = componentDto.isPresent() ? ImmutableMap.of(uuid, componentDto.get()) : Collections.emptyMap(); } @CheckForNull @@ -201,7 +177,7 @@ public class TaskFormatter { * now - startedAt */ @CheckForNull - Long computeExecutionTimeMs(CeQueueDto dto) { + private Long computeExecutionTimeMs(CeQueueDto dto) { Long startedAt = dto.getStartedAt(); if (startedAt == null) { return null; @@ -209,31 +185,4 @@ public class TaskFormatter { return system2.now() - startedAt; } - private final class CeActivityDtoToTask implements Function { - private final ComponentDtoCache cache; - - public CeActivityDtoToTask(ComponentDtoCache cache) { - this.cache = cache; - } - - @Override - @Nonnull - public WsCe.Task apply(@Nonnull CeActivityDto input) { - return formatActivity(input, cache); - } - } - - private final class CeQueueDtoToTask implements Function { - private final ComponentDtoCache cache; - - public CeQueueDtoToTask(ComponentDtoCache cache) { - this.cache = cache; - } - - @Override - @Nonnull - public WsCe.Task apply(@Nonnull CeQueueDto input) { - return formatQueue(input, cache); - } - } } -- 2.39.5