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