]> source.dussan.org Git - sonarqube.git/commitdiff
Quality Profile's SearchDataLoader with Java 8 streams
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Mon, 20 Jun 2016 12:07:47 +0000 (14:07 +0200)
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Tue, 21 Jun 2016 13:06:44 +0000 (15:06 +0200)
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ActiveRuleChange.java
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileFactory.java
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileLoader.java
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/RuleActivationActions.java
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/SearchAction.java
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/SearchDataLoader.java
sonar-db/src/main/java/org/sonar/db/qualityprofile/QualityProfileDao.java

index 0d75ff263201f9438043d078aa91577a834358f1..8c910f5140c7ae8953daf80eb197bd709906d5d5 100644 (file)
@@ -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<String, String> 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);
     }
index 29903ee8dccd9d74552bf37be842a17b5718f667..f2e9ff6b6a0ecee9335662f130463f860a34bd4c 100644 (file)
@@ -140,7 +140,6 @@ public class QProfileFactory {
     return db.qualityProfileDao().selectDefaultProfile(session, language);
   }
 
-  @CheckForNull
   public List<QualityProfileDto> getDefaults(DbSession session, Collection<String> languageKeys) {
     return db.qualityProfileDao().selectDefaultProfiles(session, languageKeys);
   }
index a48b7dbca745b0680e7cc23474b6c4fa6a9fc074..735a96edaec31622d8a7a511c23f57a087814f32 100644 (file)
@@ -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<String, Multimap<String, FacetValue>> getAllProfileStats() {
-    List<String> keys = Lists.newArrayList();
-    for (QualityProfileDto profile : this.findAll()) {
-      keys.add(profile.getKey());
-    }
+    List<String> keys = findAll().stream().map(QualityProfileDto::getKey).collect(Collectors.toList());
     return activeRuleIndex.getStatsByProfileKeys(keys);
   }
 
index 4e561ec8872f2152a943cfa526e93f1a969ac6f3..f1777449867026d23d454532aaeb2a136bfce236 100644 (file)
  */
 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 <code>api/profiles/list</code>")
       .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));
   }
 
index 6e447e3bc6ac6c28dd0911b6f1b290cdcc1e35da..93e2b05fdbf85ff97c55b8a43a355a64d3783910 100644 (file)
@@ -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);
   }
index cb5c848cdcae1473a3ebddf1cd61ac1db40927aa..3434805616f07ac1a0e65cf7e949993359dd6113 100644 (file)
@@ -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<QProfile> 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<String, QProfile> qualityProfiles, Collection<QualityProfileDto> 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<String, QProfile> qualityProfiles, Collection<QProfile> list) {
-    for (QProfile qProfile : list) {
-      qualityProfiles.put(qProfile.language(), qProfile);
-    }
+    list.forEach(qualityProfile -> qualityProfiles.put(qualityProfile.language(), qualityProfile));
   }
 
   private Set<String> getLanguageKeys() {
-    return from(Arrays.asList(languages.all())).transform(LanguageToKey.INSTANCE).toSet();
+    return Arrays.stream(languages.all()).map(Language::getKey).collect(Collectors.toSet());
   }
 
   private List<QProfile> findDefaultProfiles(final DbSession dbSession, Set<String> 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<Language, String> {
-    INSTANCE;
-
-    @Override
-    @Nonnull
-    public String apply(@Nonnull Language input) {
-      return input.getKey();
-    }
-  }
-
   private enum QualityProfileDtoToQProfile implements Function<QualityProfileDto, QProfile> {
     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<QProfile> {
-    @Override
-    public boolean apply(@Nonnull QProfile profile) {
-      return languages.get(profile.language()) != null;
-    }
   }
 }
index 274eb63d36a19c5104c6367e33c7c5210ee72971..b1d54d299e930e0e10ec8b5adecd64380a97afcd 100644 (file)
@@ -171,7 +171,6 @@ public class QualityProfileDao implements Dao {
     }
   }
 
-  @CheckForNull
   public List<QualityProfileDto> selectDefaultProfiles(DbSession session, Collection<String> languageKeys) {
     return executeLargeInputs(languageKeys, mapper(session)::selectDefaultProfiles);
   }