TimeUnitExt { public static Optional<Pair<Long, TimeUnit>> parse(String intervalWithUnit) { Matcher matcher = INTERVAL_WITH_UNIT_RE.matcher(intervalWithUnit); if (!matcher.matches()) { return Optional.empty(); } long interval = Long.parseLong(matcher.group(1)); String unit = matcher.group(2); switch (unit) { case "ms": return Optional.of(Pair.of(interval, TimeUnit.MILLISECONDS)); case "s": return Optional.of(Pair.of(interval, TimeUnit.SECONDS)); case "m": return Optional.of(Pair.of(interval, TimeUnit.MINUTES)); case "h": return Optional.of(Pair.of(interval, TimeUnit.HOURS)); case "d": return Optional.of(Pair.of(interval, TimeUnit.DAYS)); } return Optional.empty(); }  static Optional<Pair<Long, TimeUnit>> parse(String intervalWithUnit); static Optional<Long> toMillis(String intervalWithUnit);  }
@Test public void testParse() { testParse("1ms", 1, TimeUnit.MILLISECONDS); testParse("2s", 2, TimeUnit.SECONDS); testParse("3m", 3, TimeUnit.MINUTES); testParse("4h", 4, TimeUnit.HOURS); testParse("5d", 5, TimeUnit.DAYS); }
@Test public void testParseWithInvalidData() { assertThat(TimeUnitExt.parse("1b2mss")).isNotPresent(); }