]> source.dussan.org Git - sonarqube.git/commitdiff
add DateUtils#truncateToSeconds and use it instead of +1_000L
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Wed, 20 Sep 2017 08:16:36 +0000 (10:16 +0200)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Thu, 21 Sep 2017 07:54:23 +0000 (09:54 +0200)
server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/issue/IssueCounter.java
server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/issue/NewEffortCalculator.java
server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/qualitymodel/NewReliabilityAndSecurityRatingMeasuresVisitor.java
sonar-core/src/main/java/org/sonar/core/issue/DefaultIssue.java
sonar-plugin-api/src/main/java/org/sonar/api/utils/DateUtils.java

index 6be67d9d68e510ed4640cb3bd6c64547a661fb9b..2f03f8c0b55fff955d60778633b9c4baf031aba0 100644 (file)
@@ -64,6 +64,7 @@ import static org.sonar.api.rule.Severity.CRITICAL;
 import static org.sonar.api.rule.Severity.INFO;
 import static org.sonar.api.rule.Severity.MAJOR;
 import static org.sonar.api.rule.Severity.MINOR;
+import static org.sonar.api.utils.DateUtils.truncateToSeconds;
 
 /**
  * For each component, computes the measures related to number of issues:
@@ -138,7 +139,7 @@ public class IssueCounter extends IssueVisitor {
     }
     Period period = periodHolder.getPeriod();
     // Add one second to not take into account issues created during current analysis
-    if (issue.creationDate().getTime() >= period.getSnapshotDate() + 1000L) {
+    if (issue.creationDate().getTime() > truncateToSeconds(period.getSnapshotDate())) {
       currentCounters.addOnPeriod(issue);
     }
   }
index f1c3bf603f0d125effbf0516a685a12fe7bd4183..397f3faac1ca4f4308b987944413b3e1a5a2c92f 100644 (file)
@@ -24,7 +24,6 @@ import com.google.common.base.MoreObjects;
 import com.google.common.base.Predicate;
 import com.google.common.collect.Lists;
 import com.google.common.collect.Ordering;
-import java.util.Calendar;
 import java.util.Collection;
 import java.util.Comparator;
 import java.util.Date;
@@ -33,7 +32,6 @@ import java.util.List;
 import javax.annotation.CheckForNull;
 import javax.annotation.Nonnull;
 import javax.annotation.Nullable;
-import org.apache.commons.lang.time.DateUtils;
 import org.sonar.core.issue.DefaultIssue;
 import org.sonar.core.issue.FieldDiffs;
 import org.sonar.db.issue.IssueChangeDto;
@@ -41,6 +39,7 @@ import org.sonar.server.computation.task.projectanalysis.period.Period;
 import org.sonar.server.issue.IssueFieldsSetter;
 
 import static com.google.common.collect.FluentIterable.from;
+import static org.sonar.api.utils.DateUtils.truncateToSeconds;
 
 /**
  * Gets the issue debt that was introduced on a period. The algorithm
@@ -52,15 +51,10 @@ public class NewEffortCalculator {
    * Changelog have to be sorted from newest to oldest.
    * Null date should be the first as this happen when technical debt has changed since previous analysis.
    */
-  private static final Comparator<FieldDiffs> CHANGE_ORDERING = Ordering.natural().reverse().nullsFirst().onResultOf(new Function<FieldDiffs, Date>() {
-    @Override
-    public Date apply(@Nonnull FieldDiffs dto) {
-      return dto.creationDate();
-    }
-  });
+  private static final Comparator<FieldDiffs> CHANGE_ORDERING = Ordering.natural().reverse().nullsFirst().onResultOf((Function<FieldDiffs, Date>) dto -> dto.creationDate());
 
   public long calculate(DefaultIssue issue, Collection<IssueChangeDto> debtChangelog, Period period) {
-    if (issue.creationDate().getTime() > period.getSnapshotDate() + 1000L) {
+    if (issue.creationDate().getTime() > truncateToSeconds(period.getSnapshotDate())) {
       return MoreObjects.firstNonNull(issue.effortInMinutes(), 0L);
     }
     return calculateFromChangelog(issue, debtChangelog, period.getSnapshotDate());
@@ -104,7 +98,7 @@ public class NewEffortCalculator {
   }
 
   private static boolean isBeforeOrEqual(@Nullable Date changeDate, Date periodDate) {
-    return (changeDate != null) && (DateUtils.truncatedCompareTo(changeDate, periodDate, Calendar.SECOND) <= 0);
+    return (changeDate != null) && (truncateToSeconds(changeDate.getTime()) <= truncateToSeconds(periodDate.getTime()));
   }
 
   private static FieldDiffs.Diff debtDiff(FieldDiffs diffs) {
index a9ca63e71f9d382e6dc2ae3b46d498ba821eadad..347c2fa339329097ff71da81038f26ea17bf123b 100644 (file)
@@ -43,6 +43,7 @@ import static org.sonar.api.rule.Severity.MAJOR;
 import static org.sonar.api.rule.Severity.MINOR;
 import static org.sonar.api.rules.RuleType.BUG;
 import static org.sonar.api.rules.RuleType.VULNERABILITY;
+import static org.sonar.api.utils.DateUtils.truncateToSeconds;
 import static org.sonar.server.computation.task.projectanalysis.component.ComponentVisitor.Order.POST_ORDER;
 import static org.sonar.server.computation.task.projectanalysis.component.CrawlerDepthLimit.LEAVES;
 import static org.sonar.server.computation.task.projectanalysis.measure.Measure.newMeasureBuilder;
@@ -167,7 +168,7 @@ public class NewReliabilityAndSecurityRatingMeasuresVisitor extends PathAwareVis
 
     private static boolean isOnPeriod(DefaultIssue issue, Period period) {
       // Add one second to not take into account issues created during current analysis
-      return issue.creationDate().getTime() >= period.getSnapshotDate() + 1000L;
+      return issue.creationDate().getTime() > truncateToSeconds(period.getSnapshotDate());
     }
   }
 
index 8ac7a205d584120419858130da0e58033f0ceab1..9685a49bb06d665444b6a2ce215ed14d94fa240c 100644 (file)
@@ -26,8 +26,6 @@ import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.ImmutableSet;
 import com.google.common.collect.Maps;
 import java.io.Serializable;
-import java.time.Instant;
-import java.time.temporal.ChronoUnit;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
@@ -51,6 +49,7 @@ import org.sonar.api.utils.Duration;
 import org.sonar.core.issue.tracking.Trackable;
 
 import static java.lang.String.format;
+import static org.sonar.api.utils.DateUtils.truncateToSeconds;
 
 public class DefaultIssue implements Issue, Trackable, org.sonar.api.ce.measure.Issue {
 
@@ -368,16 +367,6 @@ public class DefaultIssue implements Issue, Trackable, org.sonar.api.ce.measure.
     return this;
   }
 
-  @CheckForNull
-  private static Date truncateToSeconds(@Nullable Date d) {
-    if (d == null) {
-      return null;
-    }
-    Instant instant = d.toInstant();
-    instant = instant.truncatedTo(ChronoUnit.SECONDS);
-    return Date.from(instant);
-  }
-
   @Override
   @CheckForNull
   public Date updateDate() {
index ea2329164713596e11c2b929438102e2e8b0b60b..01e93aafa03615f8e9d85cdf34318779d909be8d 100644 (file)
@@ -285,4 +285,22 @@ public final class DateUtils {
     return Date.from(date.toInstant().plus(numberOfDays, ChronoUnit.DAYS));
   }
 
+  @CheckForNull
+  public static Date truncateToSeconds(@Nullable Date d) {
+    if (d == null) {
+      return null;
+    }
+    return truncateToSecondsImpl(d);
+  }
+
+  public static long truncateToSeconds(long dateTime) {
+    return truncateToSecondsImpl(new Date(dateTime)).getTime();
+  }
+
+  private static Date truncateToSecondsImpl(Date d) {
+    Instant instant = d.toInstant();
+    instant = instant.truncatedTo(ChronoUnit.SECONDS);
+    return Date.from(instant);
+  }
+
 }