@Test public void testParseWithInvalidData() { assertThat(TimeUnitExt.parse("1b2mss")).isNotPresent(); }
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(); }
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(); } }
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(); }  }
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); }
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);  }