org.sonar.api.utils.Duration类的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(8.3k)|赞(0)|评价(0)|浏览(177)

本文整理了Java中org.sonar.api.utils.Duration类的一些代码示例,展示了Duration类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Duration类的具体详情如下:
包路径:org.sonar.api.utils.Duration
类名称:Duration

Duration介绍

暂无

代码示例

代码示例来源:origin: SonarSource/sonarqube

/**
 * Add the given duration to the current one.
 */
public Duration add(Duration with) {
 return Duration.create(durationInMinutes + with.durationInMinutes);
}

代码示例来源:origin: SonarSource/sonarqube

/**
 * Return true if the given duration is greater than the current one.
 */
public boolean isGreaterThan(Duration other) {
 return toMinutes() > other.toMinutes();
}

代码示例来源:origin: SonarSource/sonarqube

@Test
public void add() {
 assertThat(Duration.decode("1h", HOURS_IN_DAY).add(Duration.decode("1min", HOURS_IN_DAY))).isEqualTo(Duration.decode("1h1min", HOURS_IN_DAY));
}

代码示例来源:origin: SonarSource/sonarqube

@CheckForNull
private static String sanitizeValue(String label, @Nullable String s) {
 if (StringUtils.isNotBlank(s)) {
  try {
   Duration duration = Duration.decode(s, HOURS_IN_DAY);
   return duration.encode(HOURS_IN_DAY);
  } catch (Exception e) {
   throw new IllegalArgumentException(String.format("Invalid %s: %s (%s)", label, s, e.getMessage()), e);
  }
 }
 return null;
}

代码示例来源:origin: SonarSource/sonarqube

@Test
public void is_greater_than() {
 assertThat(Duration.decode("1h", HOURS_IN_DAY).isGreaterThan(Duration.decode("1min", HOURS_IN_DAY))).isTrue();
 assertThat(Duration.decode("1min", HOURS_IN_DAY).isGreaterThan(Duration.decode("1d", HOURS_IN_DAY))).isFalse();
 assertThat(Duration.decode("1d", HOURS_IN_DAY).isGreaterThan(Duration.decode("1d", HOURS_IN_DAY))).isFalse();
 assertThat(Duration.decode("1d", 10).isGreaterThan(Duration.decode("1d", 8))).isTrue();
}

代码示例来源:origin: SonarSource/sonarqube

@Test
public void decode() {
 assertThat(Duration.decode("    15 d  23  h     42min  ", HOURS_IN_DAY)).isEqualTo(Duration.create(15 * ONE_DAY_IN_MINUTES + 23 * ONE_HOUR_IN_MINUTES + 42 * ONE_MINUTE));
 assertThat(Duration.decode("15d23h42min", HOURS_IN_DAY)).isEqualTo(Duration.create(15 * ONE_DAY_IN_MINUTES + 23 * ONE_HOUR_IN_MINUTES + 42 * ONE_MINUTE));
 assertThat(Duration.decode("23h", HOURS_IN_DAY)).isEqualTo(Duration.create(23 * ONE_HOUR_IN_MINUTES));
 assertThat(Duration.decode("15d", HOURS_IN_DAY)).isEqualTo(Duration.create(15 * ONE_DAY_IN_MINUTES));
 assertThat(Duration.decode("42min", HOURS_IN_DAY)).isEqualTo(Duration.create(42 * ONE_MINUTE));
 assertThat(Duration.decode("0min", HOURS_IN_DAY)).isEqualTo(Duration.create(0));
 assertThat(Duration.decode("25h61min", HOURS_IN_DAY)).isEqualTo(Duration.create(25 * ONE_HOUR_IN_MINUTES + 61));
}

代码示例来源:origin: SonarSource/sonarqube

@CheckForNull
public Duration calculate(DefaultIssue issue) {
 if (issue.isFromExternalRuleEngine()) {
  return issue.effort();
 }
 Rule rule = ruleRepository.getByKey(issue.ruleKey());
 DebtRemediationFunction fn = rule.getRemediationFunction();
 if (fn != null) {
  verifyEffortToFix(issue, fn);
  Duration debt = Duration.create(0);
  String gapMultiplier = fn.gapMultiplier();
  if (fn.type().usesGapMultiplier() && !Strings.isNullOrEmpty(gapMultiplier)) {
   int effortToFixValue = MoreObjects.firstNonNull(issue.gap(), 1).intValue();
   // TODO convert to Duration directly in Rule#remediationFunction -> better performance + error handling
   debt = durations.decode(gapMultiplier).multiply(effortToFixValue);
  }
  String baseEffort = fn.baseEffort();
  if (fn.type().usesBaseEffort() && !Strings.isNullOrEmpty(baseEffort)) {
   // TODO convert to Duration directly in Rule#remediationFunction -> better performance + error handling
   debt = debt.add(durations.decode(baseEffort));
  }
  return debt;
 }
 return null;
}

代码示例来源:origin: SonarSource/sonarqube

@Test
public void create_from_duration_in_minutes() {
 Duration duration = Duration.create(ONE_DAY_IN_MINUTES + ONE_HOUR_IN_MINUTES + ONE_MINUTE);
 assertThat(duration.toMinutes()).isEqualTo(ONE_DAY_IN_MINUTES + ONE_HOUR_IN_MINUTES + ONE_MINUTE);
}

代码示例来源:origin: SonarSource/sonarqube

@Test
public void encode() {
 assertThat(Duration.create(2 * ONE_DAY_IN_MINUTES + 5 * ONE_HOUR_IN_MINUTES + 46 * ONE_MINUTE).encode(HOURS_IN_DAY)).isEqualTo("2d5h46min");
 assertThat(Duration.create(ONE_DAY_IN_MINUTES).encode(HOURS_IN_DAY)).isEqualTo("1d");
 assertThat(Duration.create(ONE_HOUR_IN_MINUTES).encode(HOURS_IN_DAY)).isEqualTo("1h");
 assertThat(Duration.create(HOURS_IN_DAY * ONE_HOUR_IN_MINUTES).encode(HOURS_IN_DAY)).isEqualTo("1d");
 assertThat(Duration.create(ONE_MINUTE).encode(HOURS_IN_DAY)).isEqualTo("1min");
 assertThat(Duration.create(0).encode(HOURS_IN_DAY)).isEqualTo("0min");
}

代码示例来源:origin: SonarSource/sonarqube

@Test
public void test_equals_and_hashcode() throws Exception {
 Duration duration = Duration.create(ONE_DAY_IN_MINUTES + ONE_HOUR_IN_MINUTES + ONE_MINUTE);
 Duration durationWithSameValue = Duration.create(ONE_DAY_IN_MINUTES + ONE_HOUR_IN_MINUTES + ONE_MINUTE);
 Duration durationWithDifferentValue = Duration.create(ONE_DAY_IN_MINUTES + ONE_HOUR_IN_MINUTES);
 assertThat(duration).isEqualTo(duration);
 assertThat(durationWithSameValue).isEqualTo(duration);
 assertThat(durationWithDifferentValue).isNotEqualTo(duration);
 assertThat(duration).isNotEqualTo(null);
 assertThat(duration.hashCode()).isEqualTo(duration.hashCode());
 assertThat(durationWithSameValue.hashCode()).isEqualTo(duration.hashCode());
 assertThat(durationWithDifferentValue.hashCode()).isNotEqualTo(duration.hashCode());
}

代码示例来源:origin: SonarSource/sonarqube

@Test
public void test_toString() throws Exception {
 assertThat(Duration.create(ONE_DAY_IN_MINUTES + ONE_HOUR_IN_MINUTES + ONE_MINUTE).toString()).isNotNull();
}

代码示例来源:origin: SonarSource/sonarqube

@Test
public void multiply() {
 assertThat(Duration.decode("1h", HOURS_IN_DAY).multiply(2)).isEqualTo(Duration.decode("2h", HOURS_IN_DAY));
}

代码示例来源:origin: SonarSource/sonarqube

/**
 * Convert the text to a Duration
 * <br>
 * Example : decode("9d 10 h") -&gt; Duration.encode("10d2h")
 * <br>
 * @throws IllegalArgumentException
 */
public Duration decode(String duration) {
 return Duration.decode(duration, HOURS_IN_DAY);
}

代码示例来源:origin: SonarSource/sonarqube

/**
 * Return the string value of the Duration.
 * <br>
 * Example : encode(Duration.encode("9d 10h")) -&gt; "10d2h"
 */
public String encode(Duration duration) {
 return duration.encode(HOURS_IN_DAY);
}

代码示例来源:origin: SonarSource/sonarqube

@Test
public void subtract() {
 assertThat(Duration.decode("1h", HOURS_IN_DAY).subtract(Duration.decode("1min", HOURS_IN_DAY))).isEqualTo(Duration.decode("59min", HOURS_IN_DAY));
}

代码示例来源:origin: org.codehaus.sonar/sonar-batch

private Duration calculateDebt(DebtRemediationFunction function, @Nullable Double effortToFix, RuleKey ruleKey) {
 if (DebtRemediationFunction.Type.CONSTANT_ISSUE.equals(function.type()) && effortToFix != null) {
  throw new IllegalArgumentException("Rule '" + ruleKey + "' can not use 'Constant/issue' remediation function " +
   "because this rule does not have a fixed remediation cost.");
 }
 Duration result = Duration.create(0);
 Duration factor = function.coefficient();
 Duration offset = function.offset();
 if (factor != null) {
  int effortToFixValue = Objects.firstNonNull(effortToFix, 1).intValue();
  result = factor.multiply(effortToFixValue);
 }
 if (offset != null) {
  result = result.add(offset);
 }
 return result;
}

代码示例来源:origin: SonarSource/sonarqube

@Test
public void copy_effort_for_external_issues() {
 issue.setGap(null);
 issue.setIsFromExternalRuleEngine(true);
 issue.setEffort(Duration.create(20l));
 rule.setFunction(null);
 assertThat(underTest.calculate(issue).toMinutes()).isEqualTo(20l);
}

代码示例来源:origin: org.codehaus.sonar/sonar-plugin-api

@CheckForNull
private String sanitizeValue(String label, @Nullable String s) {
 if (StringUtils.isNotBlank(s)) {
  try {
   Duration duration = Duration.decode(s, HOURS_IN_DAY);
   return duration.encode(HOURS_IN_DAY);
  } catch (Exception e) {
   throw new IllegalArgumentException(String.format("Invalid %s: %s (%s)", label, s, e.getMessage()), e);
  }
 }
 return null;
}

代码示例来源:origin: SonarSource/sonarqube

@Test
public void fail_to_decode_if_only_number() {
 try {
  Duration.decode("15", HOURS_IN_DAY);
  fail();
 } catch (Exception e) {
  assertThat(e).isInstanceOf(IllegalArgumentException.class).hasMessage("Duration '15' is invalid, it should use the following sample format : 2d 10h 15min");
 }
}

代码示例来源:origin: org.sonarsource.sonarqube/sonar-plugin-api

/**
 * Return the string value of the Duration.
 * <br>
 * Example : encode(Duration.encode("9d 10h")) -&gt; "10d2h"
 */
public String encode(Duration duration) {
 return duration.encode(HOURS_IN_DAY);
}

相关文章