]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8951 changed guava’s Optional to java.util.Optional for RuleDao
authorDaniel Schwarz <daniel.schwarz@sonarsource.com>
Wed, 5 Apr 2017 15:29:37 +0000 (17:29 +0200)
committerDaniel Schwarz <bartfastiel@users.noreply.github.com>
Fri, 14 Apr 2017 06:57:18 +0000 (08:57 +0200)
server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleDao.java
server/sonar-db-dao/src/test/java/org/sonar/db/rule/RuleDaoTest.java
server/sonar-server/src/main/java/org/sonar/server/issue/IssueUpdater.java
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/RuleActivatorContextFactory.java
server/sonar-server/src/main/java/org/sonar/server/rule/DefaultRuleFinder.java
server/sonar-server/src/main/java/org/sonar/server/rule/RegisterRules.java
server/sonar-server/src/main/java/org/sonar/server/rule/RuleCreator.java
server/sonar-server/src/main/java/org/sonar/server/rule/RuleUpdater.java
server/sonar-server/src/main/java/org/sonar/server/rule/ws/CreateAction.java
server/sonar-server/src/main/java/org/sonar/server/rule/ws/ShowAction.java
server/sonar-server/src/main/java/org/sonar/server/rule/ws/UpdateAction.java

index d5036c4e4f063e19a24593fe2e24055b62623a98..e52750da6f23c81e4fb5e1b83bdd153599e02c5a 100644 (file)
@@ -19,9 +19,9 @@
  */
 package org.sonar.db.rule;
 
-import com.google.common.base.Optional;
 import java.util.Collection;
 import java.util.List;
+import java.util.Optional;
 import javax.annotation.Nullable;
 import org.apache.ibatis.session.ResultHandler;
 import org.sonar.api.rule.RuleKey;
@@ -47,11 +47,11 @@ public class RuleDao implements Dao {
   public Optional<RuleDto> selectByKey(DbSession session, String organizationUuid, RuleKey key) {
     RuleDto res = mapper(session).selectByKey(organizationUuid, key);
     ensureOrganizationIsSet(organizationUuid, res);
-    return Optional.fromNullable(res);
+    return Optional.ofNullable(res);
   }
 
   public Optional<RuleDefinitionDto> selectDefinitionByKey(DbSession session, RuleKey key) {
-    return Optional.fromNullable(mapper(session).selectDefinitionByKey(key));
+    return Optional.ofNullable(mapper(session).selectDefinitionByKey(key));
   }
 
   public java.util.Optional<RuleMetadataDto> selectMetadataByKey(DbSession session, RuleKey key, OrganizationDto organization) {
@@ -78,11 +78,11 @@ public class RuleDao implements Dao {
   public Optional<RuleDto> selectById(long id, String organizationUuid, DbSession session) {
     RuleDto res = mapper(session).selectById(organizationUuid, id);
     ensureOrganizationIsSet(organizationUuid, res);
-    return Optional.fromNullable(res);
+    return Optional.ofNullable(res);
   }
 
   public Optional<RuleDefinitionDto> selectDefinitionById(long id, DbSession session) {
-    return Optional.fromNullable(mapper(session).selectDefinitionById(id));
+    return Optional.ofNullable(mapper(session).selectDefinitionById(id));
   }
 
   public List<RuleDto> selectByIds(DbSession session, String organizationUuid, List<Integer> ids) {
index e42b757fe488e20780c6dcd16f6780b964d131cf..3796587ccd0ecc1f132b9740876fcb520b301954 100644 (file)
  */
 package org.sonar.db.rule;
 
-import com.google.common.base.Optional;
 import com.google.common.collect.Iterables;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.List;
+import java.util.Optional;
 import org.apache.ibatis.session.ResultHandler;
 import org.junit.Rule;
 import org.junit.Test;
@@ -46,7 +46,6 @@ import static com.google.common.collect.Sets.newHashSet;
 import static java.util.Arrays.asList;
 import static java.util.Collections.singletonList;
 import static org.assertj.core.api.Assertions.assertThat;
-import static org.assertj.guava.api.Assertions.assertThat;
 
 public class RuleDaoTest {
 
@@ -94,7 +93,7 @@ public class RuleDaoTest {
     dbTester.prepareDbUnit(getClass(), "shared.xml");
     String organizationUuid = "org-1";
 
-    assertThat(underTest.selectById(55l, organizationUuid, dbTester.getSession())).isAbsent();
+    assertThat(underTest.selectById(55l, organizationUuid, dbTester.getSession())).isEmpty();
     Optional<RuleDto> ruleDtoOptional = underTest.selectById(1l, organizationUuid, dbTester.getSession());
     assertThat(ruleDtoOptional).isPresent();
     assertThat(ruleDtoOptional.get().getId()).isEqualTo(1);
@@ -113,7 +112,7 @@ public class RuleDaoTest {
   public void selectDefinitionById() {
     dbTester.prepareDbUnit(getClass(), "shared.xml");
 
-    assertThat(underTest.selectDefinitionById(55l, dbTester.getSession())).isAbsent();
+    assertThat(underTest.selectDefinitionById(55l, dbTester.getSession())).isEmpty();
     Optional<RuleDefinitionDto> ruleDtoOptional = underTest.selectDefinitionById(1l, dbTester.getSession());
     assertThat(ruleDtoOptional).isPresent();
     assertThat(ruleDtoOptional.get().getId()).isEqualTo(1);
@@ -296,8 +295,8 @@ public class RuleDaoTest {
     String organizationUuid = "org-1";
 
     assertThat(underTest.selectByQuery(dbTester.getSession(), organizationUuid, RuleQuery.create()))
-        .extracting(RuleDto::getOrganizationUuid)
-        .containsExactly(organizationUuid, organizationUuid);
+      .extracting(RuleDto::getOrganizationUuid)
+      .containsExactly(organizationUuid, organizationUuid);
   }
 
   @Test
@@ -457,23 +456,23 @@ public class RuleDaoTest {
     String organizationUuid = "org-1";
     OrganizationDto organization = OrganizationTesting.newOrganizationDto().setUuid(organizationUuid);
     RuleMetadataDto metadataV1 = new RuleMetadataDto()
-        .setRuleId(1)
-        .setOrganizationUuid(organizationUuid)
-        .setCreatedAt(3_500_000_000_000L)
-        .setUpdatedAt(4_000_000_000_000L);
+      .setRuleId(1)
+      .setOrganizationUuid(organizationUuid)
+      .setCreatedAt(3_500_000_000_000L)
+      .setUpdatedAt(4_000_000_000_000L);
     RuleMetadataDto metadataV2 = new RuleMetadataDto()
-        .setRuleId(1)
-        .setOrganizationUuid(organizationUuid)
-        .setNoteData("My note")
-        .setNoteUserLogin("admin")
-        .setNoteCreatedAt(DateUtils.parseDate("2013-12-19").getTime())
-        .setNoteUpdatedAt(DateUtils.parseDate("2013-12-20").getTime())
-        .setRemediationFunction(DebtRemediationFunction.Type.LINEAR.toString())
-        .setRemediationGapMultiplier("1h")
-        .setRemediationBaseEffort("5min")
-        .setTags(newHashSet("tag1", "tag2"))
-        .setCreatedAt(6_500_000_000_000L)
-        .setUpdatedAt(7_000_000_000_000L);
+      .setRuleId(1)
+      .setOrganizationUuid(organizationUuid)
+      .setNoteData("My note")
+      .setNoteUserLogin("admin")
+      .setNoteCreatedAt(DateUtils.parseDate("2013-12-19").getTime())
+      .setNoteUpdatedAt(DateUtils.parseDate("2013-12-20").getTime())
+      .setRemediationFunction(DebtRemediationFunction.Type.LINEAR.toString())
+      .setRemediationGapMultiplier("1h")
+      .setRemediationBaseEffort("5min")
+      .setTags(newHashSet("tag1", "tag2"))
+      .setCreatedAt(6_500_000_000_000L)
+      .setUpdatedAt(7_000_000_000_000L);
 
     underTest.insertOrUpdate(dbTester.getSession(), metadataV1);
     dbTester.commit();
index 19fafa2652529cd88821cc42789c46870495dc28..4976b5b5a7aec61b428cf876250be1fd71aaaed3 100644 (file)
@@ -58,7 +58,7 @@ public class IssueUpdater {
   }
 
   private Optional<RuleDefinitionDto> getRuleByKey(DbSession session, RuleKey ruleKey) {
-    Optional<RuleDefinitionDto> rule = Optional.ofNullable(dbClient.ruleDao().selectDefinitionByKey(session, ruleKey).orNull());
+    Optional<RuleDefinitionDto> rule = Optional.ofNullable(dbClient.ruleDao().selectDefinitionByKey(session, ruleKey).orElse(null));
     return (rule.isPresent() && rule.get().getStatus() != RuleStatus.REMOVED) ? rule : Optional.empty();
   }
 }
index b111d8e4b1fbf2d1dc84f802c8338953cd7f3474..d7edffbfe7cd76eee90531886c17c174d48c07d1 100644 (file)
@@ -90,7 +90,7 @@ public class RuleActivatorContextFactory {
   }
 
   Optional<RuleDefinitionDto> getRule(DbSession dbSession, RuleKey ruleKey) {
-    return Optional.ofNullable(db.ruleDao().selectDefinitionByKey(dbSession, ruleKey).orNull());
+    return Optional.ofNullable(db.ruleDao().selectDefinitionByKey(dbSession, ruleKey).orElse(null));
   }
 
   Optional<ActiveRuleDto> getActiveRule(DbSession session, ActiveRuleKey key) {
index b3263640f13ba9d38e24296c38570f16349e33ca..fcaf87bd8b8558a536e6d58706278b4d46a51d52 100644 (file)
@@ -20,7 +20,6 @@
 package org.sonar.server.rule;
 
 import com.google.common.base.Function;
-import com.google.common.base.Optional;
 import com.google.common.collect.FluentIterable;
 import com.google.common.collect.ImmutableListMultimap;
 import java.util.ArrayList;
@@ -28,6 +27,7 @@ import java.util.Collection;
 import java.util.Collections;
 import java.util.Date;
 import java.util.List;
+import java.util.Optional;
 import javax.annotation.CheckForNull;
 import javax.annotation.Nonnull;
 import org.sonar.api.rule.RuleKey;
index 1a66a64b28ae06974fee6e1ad5bee54a29dbf54e..1535c0ab7a035100c1e2bfd199599add602d9bfa 100644 (file)
@@ -19,7 +19,6 @@
  */
 package org.sonar.server.rule;
 
-import com.google.common.base.Optional;
 import com.google.common.collect.Iterables;
 import com.google.common.collect.Maps;
 import java.util.ArrayList;
@@ -28,6 +27,7 @@ import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Optional;
 import java.util.stream.Collectors;
 import javax.annotation.Nullable;
 import org.apache.commons.lang.ObjectUtils;
index d0c2ebf083fdfe68e1dd1678bf2fc3e47a69b5cf..3577427aa9994ead0b84e2e82a07910dfe60deb7 100644 (file)
@@ -151,7 +151,7 @@ public class RuleCreator {
   }
 
   private Optional<RuleDefinitionDto> loadRule(RuleKey ruleKey, DbSession dbSession) {
-    return dbClient.ruleDao().selectDefinitionByKey(dbSession, ruleKey).transform(Optional::of).or(Optional::empty);
+    return dbClient.ruleDao().selectDefinitionByKey(dbSession, ruleKey);
   }
 
   private RuleKey createCustomRule(RuleKey ruleKey, NewCustomRule newRule, RuleDto templateRuleDto, DbSession dbSession) {
index 4bde48c10fa61fad4bc57a3a1ed6fc98cc601aa4..f09c69951e929aae86fc82a9f36647814a740363 100644 (file)
@@ -20,7 +20,6 @@
 package org.sonar.server.rule;
 
 import com.google.common.base.Function;
-import com.google.common.base.Optional;
 import com.google.common.base.Strings;
 import com.google.common.collect.Lists;
 import com.google.common.collect.Multimap;
@@ -28,6 +27,7 @@ import java.util.Collection;
 import java.util.Collections;
 import java.util.List;
 import java.util.Map;
+import java.util.Optional;
 import java.util.Set;
 import java.util.function.Consumer;
 import javax.annotation.Nonnull;
index 7effe66a7c69702cf7779b7cc5f063d462d02c40..d8f4ebb5512a4b68dd6907f7a67162925eea34cc 100644 (file)
  */
 package org.sonar.server.rule.ws;
 
-import com.google.common.base.Optional;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
+import java.util.Optional;
 import org.sonar.api.rule.RuleKey;
 import org.sonar.api.rule.RuleStatus;
 import org.sonar.api.rule.Severity;
@@ -158,7 +158,6 @@ public class CreateAction implements RulesWsAction {
 
   private Rules.CreateResponse createResponse(DbSession dbSession, RuleKey ruleKey) {
     RuleDefinitionDto rule = dbClient.ruleDao().selectDefinitionByKey(dbSession, ruleKey)
-      .transform(java.util.Optional::of).or(java.util.Optional::empty)
       .orElseThrow(() -> new IllegalStateException(String.format("Cannot load rule, that has just been created '%s'", ruleKey)));
     List<RuleDefinitionDto> templateRules = new ArrayList<>();
     if (rule.getTemplateId() != null) {
index 05264eef7d32d2f0ada1c85e716b2654093a453f..fc34e656e05af35d19122a00c0c0c5acb2fa60b4 100644 (file)
  */
 package org.sonar.server.rule.ws;
 
-import com.google.common.base.Optional;
 import com.google.common.io.Resources;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
+import java.util.Optional;
 import org.sonar.api.rule.RuleKey;
 import org.sonar.api.server.ws.Request;
 import org.sonar.api.server.ws.Response;
index 8bde435eef448dce9c437a32abda9c54700d402a..2ec2d1adb0b291c750ae7c6ea73901b8ddf6f9d8 100644 (file)
@@ -24,7 +24,6 @@ import com.google.common.collect.Sets;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
-import java.util.Optional;
 import org.apache.commons.lang.StringUtils;
 import org.sonar.api.rule.RuleKey;
 import org.sonar.api.rule.RuleStatus;
@@ -226,7 +225,6 @@ public class UpdateAction implements RulesWsAction {
 
   private RuleUpdate createRuleUpdate(DbSession dbSession, RuleKey key, OrganizationDto organization) {
     RuleDto rule = dbClient.ruleDao().selectByKey(dbSession, organization, key)
-      .transform(Optional::of).or(Optional::empty)
       .orElseThrow(() -> new NotFoundException(format("This rule does not exist: %s", key)));
     RuleUpdate ruleUpdate = ofNullable(rule.getTemplateId())
       .map(x -> RuleUpdate.createForCustomRule(key))
@@ -272,12 +270,10 @@ public class UpdateAction implements RulesWsAction {
 
   private UpdateResponse buildResponse(DbSession dbSession, RuleKey key, OrganizationDto organization) {
     RuleDto rule = dbClient.ruleDao().selectByKey(dbSession, organization, key)
-      .transform(Optional::of).or(Optional::empty)
       .orElseThrow(() -> new NotFoundException(format("Rule not found: %s", key)));
     List<RuleDefinitionDto> templateRules = new ArrayList<>(1);
     if (rule.getTemplateId() != null) {
       dbClient.ruleDao().selectDefinitionById(rule.getTemplateId(), dbSession)
-        .transform(Optional::of).or(Optional::empty)
         .ifPresent(templateRules::add);
     }
     List<RuleParamDto> ruleParameters = dbClient.ruleDao().selectRuleParamsByRuleIds(dbSession, singletonList(rule.getId()));