diff options
author | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2015-09-07 10:51:25 +0200 |
---|---|---|
committer | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2015-09-08 18:12:22 +0200 |
commit | b3101c1575b89000737c854abf87631f1ec22a6f (patch) | |
tree | d2e3c242557f25fcd4333a4c8a5209f6bae8a76b /server | |
parent | 8e3a7f2357be0806b484cb180fce19c108e9a4bf (diff) | |
download | sonarqube-b3101c1575b89000737c854abf87631f1ec22a6f.tar.gz sonarqube-b3101c1575b89000737c854abf87631f1ec22a6f.zip |
add ComponentDtoFunctions to factor ComponentDto functions
Diffstat (limited to 'server')
6 files changed, 32 insertions, 84 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/component/ComponentService.java b/server/sonar-server/src/main/java/org/sonar/server/component/ComponentService.java index aa11cb36b36..1530536274a 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/component/ComponentService.java +++ b/server/sonar-server/src/main/java/org/sonar/server/component/ComponentService.java @@ -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" + diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/step/PersistComponentsStep.java b/server/sonar-server/src/main/java/org/sonar/server/computation/step/PersistComponentsStep.java index 65224e1f51b..a6f797b07a7 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/step/PersistComponentsStep.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/step/PersistComponentsStep.java @@ -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> { diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/step/ValidateProjectStep.java b/server/sonar-server/src/main/java/org/sonar/server/computation/step/ValidateProjectStep.java index c3a9857430a..a6eeea2e770 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/step/ValidateProjectStep.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/step/ValidateProjectStep.java @@ -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(); - } - } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/issue/IssueQueryService.java b/server/sonar-server/src/main/java/org/sonar/server/issue/IssueQueryService.java index cf99bfbbec1..c14c2d845b6 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/issue/IssueQueryService.java +++ b/server/sonar-server/src/main/java/org/sonar/server/issue/IssueQueryService.java @@ -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); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/test/ws/CoveredFilesAction.java b/server/sonar-server/src/main/java/org/sonar/server/test/ws/CoveredFilesAction.java index 3881c25a282..048228ad8c1 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/test/ws/CoveredFilesAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/test/ws/CoveredFilesAction.java @@ -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(); - } - } - } diff --git a/server/sonar-server/src/main/java/org/sonar/server/test/ws/ListAction.java b/server/sonar-server/src/main/java/org/sonar/server/test/ws/ListAction.java index 2081e95baed..345d988d094 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/test/ws/ListAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/test/ws/ListAction.java @@ -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) { |