org.sonar.api.utils.Duration.decode()方法的使用及代码示例

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

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

Duration.decode介绍

[英]Create a Duration from a text duration and the number of hours in a day.
For instance, Duration.decode("1d 1h", 8) will have a number of minutes of 540 (1860 + 60).
[中]根据文本持续时间和一天中的小时数创建持续时间。
例如,持续时间。解码(“1d 1h”,8)的分钟数为540(1860+60)。

代码示例

代码示例来源: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

@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 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

@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: 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

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

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

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

代码示例来源: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: SonarSource/sonarqube

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

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

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

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

/**
 * 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: org.codehaus.sonar/sonar-plugin-api

/**
 * Convert the text to a Duration
 * <br>
 * Example : decode("9d 10 h") -> Duration.encode("10d2h") (if sonar.technicalDebt.hoursInDay property is set to 8)
 */
public Duration decode(String duration) {
 return Duration.decode(duration, hoursInDay());
}

代码示例来源: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: org.sonarsource.sonarqube/sonar-plugin-api

@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;
}

相关文章