From b79b8b8427611d8669ccf27f4c1a299232ce40a9 Mon Sep 17 00:00:00 2001 From: Teryk Bellahsene Date: Mon, 20 Jun 2016 14:07:47 +0200 Subject: [PATCH] Quality Profile's SearchDataLoader with Java 8 streams --- .../qualityprofile/ActiveRuleChange.java | 17 +++---- .../qualityprofile/QProfileFactory.java | 1 - .../server/qualityprofile/QProfileLoader.java | 6 +-- .../ws/RuleActivationActions.java | 21 ++------ .../qualityprofile/ws/SearchAction.java | 4 +- .../qualityprofile/ws/SearchDataLoader.java | 49 +++++-------------- .../db/qualityprofile/QualityProfileDao.java | 1 - 7 files changed, 27 insertions(+), 72 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ActiveRuleChange.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ActiveRuleChange.java index 0d75ff26320..8c910f5140c 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ActiveRuleChange.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ActiveRuleChange.java @@ -21,15 +21,13 @@ package org.sonar.server.qualityprofile; import com.google.common.base.MoreObjects; import com.google.common.collect.Maps; +import java.util.Map; +import javax.annotation.CheckForNull; +import javax.annotation.Nullable; import org.apache.commons.lang.StringUtils; import org.sonar.db.qualityprofile.ActiveRuleKey; import org.sonar.server.activity.Activity; -import javax.annotation.CheckForNull; -import javax.annotation.Nullable; - -import java.util.Map; - public class ActiveRuleChange { public enum Type { @@ -98,11 +96,10 @@ public class ActiveRuleChange { activity.setData("ruleKey", getKey().ruleKey().toString()); activity.setData("profileKey", getKey().qProfile()); - for (Map.Entry param : parameters.entrySet()) { - if (!param.getKey().isEmpty()) { - activity.setData("param_" + param.getKey(), param.getValue()); - } - } + parameters.entrySet().stream() + .filter(param -> !param.getKey().isEmpty()) + .forEach(param -> activity.setData("param_" + param.getKey(), param.getValue())); + if (StringUtils.isNotEmpty(severity)) { activity.setData("severity", severity); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileFactory.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileFactory.java index 29903ee8dcc..f2e9ff6b6a0 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileFactory.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileFactory.java @@ -140,7 +140,6 @@ public class QProfileFactory { return db.qualityProfileDao().selectDefaultProfile(session, language); } - @CheckForNull public List getDefaults(DbSession session, Collection languageKeys) { return db.qualityProfileDao().selectDefaultProfiles(session, languageKeys); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileLoader.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileLoader.java index a48b7dbca74..735a96edaec 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileLoader.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileLoader.java @@ -23,6 +23,7 @@ import com.google.common.collect.Lists; import com.google.common.collect.Multimap; import java.util.List; import java.util.Map; +import java.util.stream.Collectors; import javax.annotation.CheckForNull; import org.sonar.api.rule.RuleStatus; import org.sonar.api.server.ServerSide; @@ -86,10 +87,7 @@ public class QProfileLoader { } public Map> getAllProfileStats() { - List keys = Lists.newArrayList(); - for (QualityProfileDto profile : this.findAll()) { - keys.add(profile.getKey()); - } + List keys = findAll().stream().map(QualityProfileDto::getKey).collect(Collectors.toList()); return activeRuleIndex.getStatsByProfileKeys(keys); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/RuleActivationActions.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/RuleActivationActions.java index 4e561ec8872..f1777449867 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/RuleActivationActions.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/RuleActivationActions.java @@ -19,11 +19,10 @@ */ package org.sonar.server.qualityprofile.ws; -import org.sonar.api.server.ServerSide; import org.sonar.api.rule.RuleKey; import org.sonar.api.rule.Severity; +import org.sonar.api.server.ServerSide; import org.sonar.api.server.ws.Request; -import org.sonar.api.server.ws.RequestHandler; import org.sonar.api.server.ws.Response; import org.sonar.api.server.ws.WebService; import org.sonar.api.utils.KeyValueFormat; @@ -58,12 +57,7 @@ public class RuleActivationActions { WebService.NewAction activate = controller .createAction(ACTIVATE_ACTION) .setDescription("Activate a rule on a Quality profile") - .setHandler(new RequestHandler() { - @Override - public void handle(Request request, Response response) throws Exception { - activate(request, response); - } - }) + .setHandler(this::activate) .setPost(true) .setSince("4.4"); @@ -87,18 +81,13 @@ public class RuleActivationActions { WebService.NewAction deactivate = controller .createAction(DEACTIVATE_ACTION) .setDescription("Deactivate a rule on a Quality profile") - .setHandler(new RequestHandler() { - @Override - public void handle(Request request, Response response) throws Exception { - deactivate(request, response); - } - }) + .setHandler(this::deactivate) .setPost(true) .setSince("4.4"); defineActiveRuleKeyParameters(deactivate); } - private void defineActiveRuleKeyParameters(WebService.NewAction action) { + private static void defineActiveRuleKeyParameters(WebService.NewAction action) { action.createParam(PROFILE_KEY) .setDescription("Key of Quality profile, can be obtained through api/profiles/list") .setRequired(true) @@ -127,7 +116,7 @@ public class RuleActivationActions { service.deactivate(ActiveRuleKey.of(request.mandatoryParam(PROFILE_KEY), ruleKey)); } - private RuleKey readRuleKey(Request request) { + private static RuleKey readRuleKey(Request request) { return RuleKey.parse(request.mandatoryParam(RULE_KEY)); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/SearchAction.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/SearchAction.java index 6e447e3bc6a..93e2b05fdbf 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/SearchAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/SearchAction.java @@ -89,7 +89,7 @@ public class SearchAction implements QProfileWsAction { @Override public void handle(Request request, Response response) throws Exception { - SearchWsResponse searchWsResponse = buildResponse(toSearchWsRequest(request)); + SearchWsResponse searchWsResponse = doHandle(toSearchWsRequest(request)); writeProtobuf(searchWsResponse, request, response); } @@ -101,7 +101,7 @@ public class SearchAction implements QProfileWsAction { .setLanguage(request.param(PARAM_LANGUAGE)); } - private SearchWsResponse buildResponse(SearchWsRequest request) { + private SearchWsResponse doHandle(SearchWsRequest request) { SearchData data = dataLoader.load(request); return buildResponse(data); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/SearchDataLoader.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/SearchDataLoader.java index cb5c848cdca..3434805616f 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/SearchDataLoader.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/SearchDataLoader.java @@ -19,8 +19,6 @@ */ package org.sonar.server.qualityprofile.ws; -import com.google.common.base.Function; -import com.google.common.base.Predicate; import com.google.common.collect.Sets; import java.util.Arrays; import java.util.Collection; @@ -29,7 +27,8 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; -import javax.annotation.Nonnull; +import java.util.function.Function; +import java.util.stream.Collectors; import javax.annotation.Nullable; import org.apache.commons.lang.builder.CompareToBuilder; import org.sonar.api.resources.Language; @@ -45,7 +44,6 @@ import org.sonar.server.qualityprofile.QProfileLoader; import org.sonar.server.qualityprofile.QProfileLookup; import org.sonarqube.ws.client.qualityprofile.SearchWsRequest; -import static com.google.common.collect.FluentIterable.from; import static java.lang.String.format; import static org.sonar.server.ws.WsUtils.checkRequest; @@ -56,7 +54,6 @@ public class SearchDataLoader { private final QProfileFactory profileFactory; private final DbClient dbClient; private final ComponentFinder componentFinder; - private final IsLanguageKnown isLanguageKnown; public SearchDataLoader(Languages languages, QProfileLookup profileLookup, QProfileLoader profileLoader, QProfileFactory profileFactory, DbClient dbClient, ComponentFinder componentFinder) { @@ -66,7 +63,6 @@ public class SearchDataLoader { this.profileFactory = profileFactory; this.dbClient = dbClient; this.componentFinder = componentFinder; - this.isLanguageKnown = new IsLanguageKnown(); } SearchData load(SearchWsRequest request) { @@ -88,7 +84,7 @@ public class SearchDataLoader { profiles = findAllProfiles(request); } - return from(profiles).toSortedList(QProfileComparator.INSTANCE); + return profiles.stream().sorted(QProfileComparator.INSTANCE).collect(Collectors.toList()); } private Collection findDefaultProfiles(SearchWsRequest request) { @@ -136,7 +132,7 @@ public class SearchDataLoader { String language = request.getLanguage(); if (language == null) { - return from(profileLookup.allProfiles()).filter(isLanguageKnown).toList(); + return profileLookup.allProfiles().stream().filter(qProfile -> languages.get(qProfile.language()) != null).collect(Collectors.toList()); } return profileLookup.profiles(language); } @@ -182,25 +178,21 @@ public class SearchDataLoader { } private static void addAllFromDto(Map qualityProfiles, Collection list) { - for (QualityProfileDto qualityProfileDto : list) { - qualityProfiles.put(qualityProfileDto.getLanguage(), QualityProfileDtoToQProfile.INSTANCE.apply(qualityProfileDto)); - } + list.forEach(qualityProfile -> qualityProfiles.put(qualityProfile.getLanguage(), QualityProfileDtoToQProfile.INSTANCE.apply(qualityProfile))); } private static void addAll(Map qualityProfiles, Collection list) { - for (QProfile qProfile : list) { - qualityProfiles.put(qProfile.language(), qProfile); - } + list.forEach(qualityProfile -> qualityProfiles.put(qualityProfile.language(), qualityProfile)); } private Set getLanguageKeys() { - return from(Arrays.asList(languages.all())).transform(LanguageToKey.INSTANCE).toSet(); + return Arrays.stream(languages.all()).map(Language::getKey).collect(Collectors.toSet()); } private List findDefaultProfiles(final DbSession dbSession, Set languageKeys) { - return from(profileFactory.getDefaults(dbSession, languageKeys)) - .transform(QualityProfileDtoToQProfile.INSTANCE) - .toList(); + return profileFactory.getDefaults(dbSession, languageKeys).stream() + .map(QualityProfileDtoToQProfile.INSTANCE) + .collect(Collectors.toList()); } private static void validateRequest(SearchWsRequest request) { @@ -241,22 +233,11 @@ public class SearchDataLoader { } } - private enum LanguageToKey implements Function { - INSTANCE; - - @Override - @Nonnull - public String apply(@Nonnull Language input) { - return input.getKey(); - } - } - private enum QualityProfileDtoToQProfile implements Function { INSTANCE; @Override - @Nonnull - public QProfile apply(@Nonnull QualityProfileDto input) { + public QProfile apply(QualityProfileDto input) { return new QProfile() .setKey(input.getKey()) .setName(input.getName()) @@ -264,13 +245,5 @@ public class SearchDataLoader { .setDefault(input.isDefault()) .setRulesUpdatedAt(input.getRulesUpdatedAt()); } - - } - - private class IsLanguageKnown implements Predicate { - @Override - public boolean apply(@Nonnull QProfile profile) { - return languages.get(profile.language()) != null; - } } } diff --git a/sonar-db/src/main/java/org/sonar/db/qualityprofile/QualityProfileDao.java b/sonar-db/src/main/java/org/sonar/db/qualityprofile/QualityProfileDao.java index 274eb63d36a..b1d54d299e9 100644 --- a/sonar-db/src/main/java/org/sonar/db/qualityprofile/QualityProfileDao.java +++ b/sonar-db/src/main/java/org/sonar/db/qualityprofile/QualityProfileDao.java @@ -171,7 +171,6 @@ public class QualityProfileDao implements Dao { } } - @CheckForNull public List selectDefaultProfiles(DbSession session, Collection languageKeys) { return executeLargeInputs(languageKeys, mapper(session)::selectDefaultProfiles); } -- 2.39.5