]> source.dussan.org Git - sonarqube.git/commitdiff
add ComponentDtoFunctions to factor ComponentDto functions 499/head
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Mon, 7 Sep 2015 08:51:25 +0000 (10:51 +0200)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Tue, 8 Sep 2015 16:12:22 +0000 (18:12 +0200)
server/sonar-server/src/main/java/org/sonar/server/component/ComponentService.java
server/sonar-server/src/main/java/org/sonar/server/computation/step/PersistComponentsStep.java
server/sonar-server/src/main/java/org/sonar/server/computation/step/ValidateProjectStep.java
server/sonar-server/src/main/java/org/sonar/server/issue/IssueQueryService.java
server/sonar-server/src/main/java/org/sonar/server/test/ws/CoveredFilesAction.java
server/sonar-server/src/main/java/org/sonar/server/test/ws/ListAction.java
sonar-db/src/main/java/org/sonar/db/component/ComponentDtoFunctions.java [new file with mode: 0644]

index aa11cb36b3600caee5ccd842826f5befb5eb50a9..1530536274ab8926ac4696f518812cb9c5403fba 100644 (file)
@@ -20,7 +20,6 @@
 
 package org.sonar.server.component;
 
-import com.google.common.base.Function;
 import com.google.common.base.Joiner;
 import com.google.common.base.Optional;
 import com.google.common.collect.Collections2;
@@ -37,10 +36,10 @@ import org.sonar.api.i18n.I18n;
 import org.sonar.api.resources.Scopes;
 import org.sonar.api.server.ServerSide;
 import org.sonar.api.utils.System2;
-import org.sonar.core.util.Uuids;
 import org.sonar.api.web.UserRole;
 import org.sonar.core.component.ComponentKeys;
 import org.sonar.core.permission.GlobalPermissions;
+import org.sonar.core.util.Uuids;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.component.ComponentDto;
@@ -49,6 +48,7 @@ import org.sonar.server.exceptions.NotFoundException;
 import org.sonar.server.user.UserSession;
 
 import static com.google.common.collect.Lists.newArrayList;
+import static org.sonar.db.component.ComponentDtoFunctions.toKey;
 
 @ServerSide
 public class ComponentService {
@@ -196,12 +196,7 @@ public class ComponentService {
       List<ComponentDto> components = dbClient.componentDao().selectByKeys(session, componentKeys);
 
       if (!ignoreMissingComponents && components.size() < componentKeys.size()) {
-        Collection<String> foundKeys = Collections2.transform(components, new Function<ComponentDto, String>() {
-          @Override
-          public String apply(ComponentDto component) {
-            return component.key();
-          }
-        });
+        Collection<String> foundKeys = Collections2.transform(components, toKey());
         Set<String> missingKeys = Sets.newHashSet(componentKeys);
         missingKeys.removeAll(foundKeys);
         throw new NotFoundException("The following component keys do not match any component:\n" +
index 65224e1f51bd9758270a3575ee60f91a553fd2a3..a6f797b07a78b19a7d7d3d15617a0c0567e3191f 100644 (file)
@@ -20,7 +20,6 @@
 
 package org.sonar.server.computation.step;
 
-import com.google.common.base.Function;
 import com.google.common.base.Predicate;
 import java.util.Date;
 import java.util.Map;
@@ -45,6 +44,7 @@ import org.sonar.server.computation.component.TreeRootHolder;
 
 import static com.google.common.collect.FluentIterable.from;
 import static org.sonar.db.component.ComponentDto.MODULE_UUID_PATH_SEP;
+import static org.sonar.db.component.ComponentDtoFunctions.toKey;
 import static org.sonar.server.computation.component.ComponentVisitor.Order.PRE_ORDER;
 
 /**
@@ -52,7 +52,6 @@ import static org.sonar.server.computation.component.ComponentVisitor.Order.PRE_
  * Also feed the components cache {@link DbIdsRepositoryImpl} with component ids
  */
 public class PersistComponentsStep implements ComputationStep {
-
   private final DbClient dbClient;
   private final TreeRootHolder treeRootHolder;
   private final MutableDbIdsRepository dbIdsRepository;
@@ -84,18 +83,8 @@ public class PersistComponentsStep implements ComputationStep {
   }
 
   private Map<String, ComponentDto> indexExistingDtosByKey(DbSession session) {
-    return from(dbClient.componentDao()
-      .selectAllComponentsFromProjectKey(session, treeRootHolder.getRoot().getKey()))
-        .uniqueIndex(ComponentKey.INSTANCE);
-  }
-
-  private enum ComponentKey implements Function<ComponentDto, String> {
-    INSTANCE;
-
-    @Override
-    public String apply(@Nonnull ComponentDto input) {
-      return input.key();
-    }
+    return from(dbClient.componentDao().selectAllComponentsFromProjectKey(session, treeRootHolder.getRoot().getKey()))
+      .uniqueIndex(toKey());
   }
 
   private class PersistComponentStepsVisitor extends PathAwareVisitorAdapter<ComponentDtoHolder> {
index c3a9857430a1f6c6453a2712de1a4cd8cd8603c3..a6eeea2e770b4214e164b8d596db24008ca77a92 100644 (file)
@@ -20,7 +20,6 @@
 
 package org.sonar.server.computation.step;
 
-import com.google.common.base.Function;
 import com.google.common.base.Joiner;
 import com.google.common.base.Optional;
 import com.google.common.collect.FluentIterable;
@@ -29,7 +28,6 @@ import java.util.Date;
 import java.util.List;
 import java.util.Map;
 import javax.annotation.CheckForNull;
-import javax.annotation.Nonnull;
 import org.sonar.api.CoreProperties;
 import org.sonar.api.config.Settings;
 import org.sonar.api.utils.MessageException;
@@ -49,6 +47,7 @@ import org.sonar.server.computation.component.TreeRootHolder;
 import org.sonar.server.computation.component.TypeAwareVisitorAdapter;
 
 import static org.sonar.api.utils.DateUtils.formatDateTime;
+import static org.sonar.db.component.ComponentDtoFunctions.toKey;
 
 /**
  * Validate project and modules. It will fail in the following cases :
@@ -82,7 +81,7 @@ public class ValidateProjectStep implements ComputationStep {
     DbSession session = dbClient.openSession(false);
     try {
       List<ComponentDto> baseModules = dbClient.componentDao().selectEnabledModulesFromProjectKey(session, treeRootHolder.getRoot().getKey());
-      Map<String, ComponentDto> baseModulesByKey = FluentIterable.from(baseModules).uniqueIndex(ComponentDtoToKey.INSTANCE);
+      Map<String, ComponentDto> baseModulesByKey = FluentIterable.from(baseModules).uniqueIndex(toKey());
       ValidateProjectsVisitor visitor = new ValidateProjectsVisitor(session, dbClient.componentDao(),
         settings.getBoolean(CoreProperties.CORE_PREVENT_AUTOMATIC_PROJECT_CREATION), baseModulesByKey);
       new DepthTraversalTypeAwareCrawler(visitor).visit(treeRootHolder.getRoot());
@@ -221,12 +220,4 @@ public class ValidateProjectStep implements ComputationStep {
     }
   }
 
-  private enum ComponentDtoToKey implements Function<ComponentDto, String> {
-    INSTANCE;
-
-    @Override
-    public String apply(@Nonnull ComponentDto input) {
-      return input.key();
-    }
-  }
 }
index cf99bfbbec1929446262cbe19f81719794c75803..c14c2d845b6441aa41badf80e8fe55442bc2f488 100644 (file)
@@ -21,7 +21,6 @@
 package org.sonar.server.issue;
 
 import com.google.common.annotations.VisibleForTesting;
-import com.google.common.base.Function;
 import com.google.common.base.Joiner;
 import com.google.common.base.Preconditions;
 import com.google.common.base.Splitter;
@@ -58,6 +57,9 @@ import org.sonar.server.user.UserSession;
 import org.sonar.server.util.RubyUtils;
 
 import static com.google.common.collect.Lists.newArrayList;
+import static org.sonar.db.component.ComponentDtoFunctions.toCopyResourceId;
+import static org.sonar.db.component.ComponentDtoFunctions.toProjectUuid;
+import static org.sonar.db.component.ComponentDtoFunctions.toUuid;
 
 /**
  * This component is used to create an IssueQuery, in order to transform the component and component roots keys into uuid.
@@ -120,9 +122,7 @@ public class IssueQueryService {
         RubyUtils.toStrings(
           ObjectUtils.defaultIfNull(
             params.get(IssueFilterParameters.PROJECT_KEYS),
-            params.get(IssueFilterParameters.PROJECTS)
-          )
-        ),
+            params.get(IssueFilterParameters.PROJECTS))),
         RubyUtils.toStrings(params.get(IssueFilterParameters.MODULE_UUIDS)),
         RubyUtils.toStrings(params.get(IssueFilterParameters.DIRECTORIES)),
         RubyUtils.toStrings(params.get(IssueFilterParameters.FILE_UUIDS)),
@@ -226,12 +226,12 @@ public class IssueQueryService {
   }
 
   private boolean mergeDeprecatedComponentParameters(DbSession session, Boolean onComponentOnly,
-                                                     @Nullable Collection<String> components,
-                                                     @Nullable Collection<String> componentUuids,
-                                                     @Nullable Collection<String> componentKeys,
-                                                     @Nullable Collection<String> componentRootUuids,
-                                                     @Nullable Collection<String> componentRoots,
-                                                     Set<String> allComponentUuids) {
+    @Nullable Collection<String> components,
+    @Nullable Collection<String> componentUuids,
+    @Nullable Collection<String> componentKeys,
+    @Nullable Collection<String> componentRootUuids,
+    @Nullable Collection<String> componentRoots,
+    Set<String> allComponentUuids) {
     boolean effectiveOnComponentOnly = false;
 
     failIfBothParametersSet(componentRootUuids, componentRoots, "componentRoots and componentRootUuids cannot be set simultaneously");
@@ -264,13 +264,13 @@ public class IssueQueryService {
   }
 
   private void addComponentParameters(IssueQuery.Builder builder, DbSession session,
-                                      boolean onComponentOnly,
-                                      Collection<String> componentUuids,
-                                      @Nullable Collection<String> projectUuids, @Nullable Collection<String> projects,
-                                      @Nullable Collection<String> moduleUuids,
-                                      @Nullable Collection<String> directories,
-                                      @Nullable Collection<String> fileUuids,
-                                      @Nullable Collection<String> authors) {
+    boolean onComponentOnly,
+    Collection<String> componentUuids,
+    @Nullable Collection<String> projectUuids, @Nullable Collection<String> projects,
+    @Nullable Collection<String> moduleUuids,
+    @Nullable Collection<String> directories,
+    @Nullable Collection<String> fileUuids,
+    @Nullable Collection<String> authors) {
 
     builder.onComponentOnly(onComponentOnly);
     if (onComponentOnly) {
@@ -353,27 +353,12 @@ public class IssueQueryService {
 
   private void addDeveloperTechnicalProjects(IssueQuery.Builder builder, DbSession session, Collection<String> componentUuids, Collection<String> authors) {
     Collection<ComponentDto> technicalProjects = dbClient.componentDao().selectByUuids(session, componentUuids);
-    Collection<String> developerUuids = Collections2.transform(technicalProjects, new Function<ComponentDto, String>() {
-      @Override
-      public String apply(ComponentDto input) {
-        return input.projectUuid();
-      }
-    });
+    Collection<String> developerUuids = Collections2.transform(technicalProjects, toProjectUuid());
     Collection<String> authorsFromProjects = authorsFromParamsOrFromDeveloper(session, developerUuids, authors);
     builder.authors(authorsFromProjects);
-    Collection<Long> projectIds = Collections2.transform(technicalProjects, new Function<ComponentDto, Long>() {
-      @Override
-      public Long apply(ComponentDto input) {
-        return input.getCopyResourceId();
-      }
-    });
+    Collection<Long> projectIds = Collections2.transform(technicalProjects, toCopyResourceId());
     List<ComponentDto> originalProjects = dbClient.componentDao().selectByIds(session, projectIds);
-    Collection<String> projectUuids = Collections2.transform(originalProjects, new Function<ComponentDto, String>() {
-      @Override
-      public String apply(ComponentDto input) {
-        return input.uuid();
-      }
-    });
+    Collection<String> projectUuids = Collections2.transform(originalProjects, toUuid());
     builder.projectUuids(projectUuids);
   }
 
index 3881c25a2826ec447d4ed9bc485fd239cce42d25..048228ad8c1b2593098a53e2ae8919320e64ae4f 100644 (file)
@@ -35,6 +35,7 @@ import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.MyBatis;
 import org.sonar.db.component.ComponentDto;
+import org.sonar.db.component.ComponentDtoFunctions;
 import org.sonar.server.test.index.CoveredFileDoc;
 import org.sonar.server.test.index.TestIndex;
 import org.sonar.server.user.UserSession;
@@ -104,7 +105,7 @@ public class CoveredFilesAction implements TestsWsAction {
     } finally {
       MyBatis.closeQuietly(dbSession);
     }
-    return Maps.uniqueIndex(components, new ComponentToUuidFunction());
+    return Maps.uniqueIndex(components, ComponentDtoFunctions.toUuid());
   }
 
   private static class CoveredFileToFileUuidFunction implements Function<CoveredFileDoc, String> {
@@ -114,11 +115,4 @@ public class CoveredFilesAction implements TestsWsAction {
     }
   }
 
-  private static class ComponentToUuidFunction implements Function<ComponentDto, String> {
-    @Override
-    public String apply(ComponentDto component) {
-      return component.uuid();
-    }
-  }
-
 }
index 2081e95baeda7fda43c87a73979f859fb890c451..345d988d094c0c7a6fc2b5dbd22f2377de59bfb1 100644 (file)
@@ -37,6 +37,7 @@ import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.MyBatis;
 import org.sonar.db.component.ComponentDto;
+import org.sonar.db.component.ComponentDtoFunctions;
 import org.sonar.server.component.ComponentFinder;
 import org.sonar.server.es.SearchOptions;
 import org.sonar.server.es.SearchResult;
@@ -169,7 +170,7 @@ public class ListAction implements TestsWsAction {
     List<String> fileUuids = Lists.transform(tests, new TestToFileUuidFunction());
     List<ComponentDto> components = dbClient.componentDao().selectByUuids(dbSession, fileUuids);
 
-    return Maps.uniqueIndex(components, new ComponentToUuidFunction());
+    return Maps.uniqueIndex(components, ComponentDtoFunctions.toUuid());
   }
 
   private static class TestToFileUuidFunction implements Function<TestDoc, String> {
@@ -179,13 +180,6 @@ public class ListAction implements TestsWsAction {
     }
   }
 
-  private static class ComponentToUuidFunction implements Function<ComponentDto, String> {
-    @Override
-    public String apply(@Nonnull ComponentDto componentDto) {
-      return componentDto.uuid();
-    }
-  }
-
   private SearchResult<TestDoc> searchTests(DbSession dbSession, @Nullable String testUuid, @Nullable String testFileUuid, @Nullable String testFileKey,
                                             @Nullable String sourceFileUuid, @Nullable Integer sourceFileLineNumber, SearchOptions searchOptions) {
     if (testUuid != null) {
diff --git a/sonar-db/src/main/java/org/sonar/db/component/ComponentDtoFunctions.java b/sonar-db/src/main/java/org/sonar/db/component/ComponentDtoFunctions.java
new file mode 100644 (file)
index 0000000..c75518e
--- /dev/null
@@ -0,0 +1,84 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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.component;
+
+import com.google.common.base.Function;
+import javax.annotation.Nonnull;
+
+/**
+ * Common Functions on ComponentDto.
+ */
+public final class ComponentDtoFunctions {
+  private ComponentDtoFunctions() {
+    // prevents instantiation
+  }
+
+  public static Function<ComponentDto, String> toKey() {
+    return ToKey.INSTANCE;
+  }
+
+  private enum ToKey implements Function<ComponentDto, String> {
+    INSTANCE;
+
+    @Override
+    public String apply(@Nonnull ComponentDto input) {
+      return input.key();
+    }
+  }
+
+  public static Function<ComponentDto, String> toProjectUuid() {
+    return ToProjectUuid.INSTANCE;
+  }
+
+  private enum ToProjectUuid implements Function<ComponentDto, String> {
+    INSTANCE;
+
+    @Override
+    public String apply(ComponentDto input) {
+      return input.projectUuid();
+    }
+  }
+
+  public static Function<ComponentDto, String> toUuid() {
+    return ToUuid.INSTANCE;
+  }
+
+  private enum ToUuid implements Function<ComponentDto, String> {
+    INSTANCE;
+
+    @Override
+    public String apply(ComponentDto input) {
+      return input.uuid();
+    }
+  }
+
+  public static Function<ComponentDto, Long> toCopyResourceId() {
+    return ToCopyResourceId.INSTANCE;
+  }
+
+  private enum ToCopyResourceId implements Function<ComponentDto, Long> {
+    INSTANCE;
+
+    @Override
+    public Long apply(ComponentDto input) {
+      return input.getCopyResourceId();
+    }
+  }
+}