ConfigHelper { public static long parseDuration(String durationStr, TimeUnit defaultUnit) throws InvalidConfigException { if (null == durationStr) return 0; TimeUnit unit = defaultUnit; Matcher m = DURATION_PATTERN.matcher(durationStr); if (!m.matches()) throw new InvalidConfigException("invalid duration string: " + durationStr); if (1 < m.groupCount() && null != m.group(2) && 0 < m.group(2).length()) { char unitChar1 = m.group(2).charAt(0); switch (unitChar1) { case 'n': case 'N': unit = TimeUnit.NANOSECONDS; break; case 'u': case 'U': unit = TimeUnit.MICROSECONDS; break; case 'm': case 'M': char unitChar3 = m.group(2).length() >= 3 ? m.group(2).charAt(2) : ' '; unit = ('n' == unitChar3 || 'N' == unitChar3) ? TimeUnit.MINUTES : TimeUnit.MILLISECONDS; break; case 's': case 'S': unit = TimeUnit.SECONDS; break; case 'h': case 'H': unit = TimeUnit.HOURS; break; case 'd': case 'D': unit = TimeUnit.DAYS; break; } } long value = Long.parseLong(m.group(1)); return defaultUnit.convert(value, unit); }  static long parseDuration(String durationStr, TimeUnit defaultUnit); static long parseByteSize(String sizeString); static final Pattern DURATION_PATTERN; static final Pattern BYTE_SIZE_PATTERN; }
@Test public void testParseDuration() throws Exception { Assert.assertEquals(ConfigHelper.parseDuration("123", TimeUnit.MILLISECONDS), 123); Assert.assertEquals(ConfigHelper.parseDuration(" 123s", TimeUnit.MILLISECONDS), 123000); Assert.assertEquals(ConfigHelper.parseDuration("10 MIN ", TimeUnit.MICROSECONDS), 10 * 60 * 1000000); Assert.assertEquals(ConfigHelper.parseDuration("70 Hr", TimeUnit.DAYS), 2); Assert.assertEquals(ConfigHelper.parseDuration("2 days", TimeUnit.DAYS), 2 ); Assert.assertEquals(ConfigHelper.parseDuration("2 days", TimeUnit.HOURS), 2L * 24 ); Assert.assertEquals(ConfigHelper.parseDuration("2 days", TimeUnit.MINUTES), 2L * 24 * 60); Assert.assertEquals(ConfigHelper.parseDuration("2 days", TimeUnit.SECONDS), 2L * 24 * 60 * 60); Assert.assertEquals(ConfigHelper.parseDuration("2 days", TimeUnit.MILLISECONDS), 2L * 24 * 60 * 60 * 1000); Assert.assertEquals(ConfigHelper.parseDuration("2 days", TimeUnit.MICROSECONDS), 2L * 24 * 60 * 60 * 1000 * 1000); Assert.assertEquals(ConfigHelper.parseDuration("2 days", TimeUnit.NANOSECONDS), 2L * 24 * 60 * 60 * 1000 * 1000 * 1000); Assert.assertEquals(ConfigHelper.parseDuration("70ms", TimeUnit.NANOSECONDS), 70000000); Assert.assertEquals(ConfigHelper.parseDuration("1hr", TimeUnit.MILLISECONDS), 1L * 60 * 60 * 1000); }
@Test public void testParseDurationInvalid() { try { ConfigHelper.parseDuration("", TimeUnit.MILLISECONDS); Assert.fail(); } catch (InvalidConfigException e) {} try { ConfigHelper.parseDuration("1month", TimeUnit.MILLISECONDS); Assert.fail(); } catch (InvalidConfigException e) {} try { ConfigHelper.parseDuration("1minutae", TimeUnit.MILLISECONDS); Assert.fail(); } catch (InvalidConfigException e) {} try { ConfigHelper.parseDuration("1.3hr", TimeUnit.MILLISECONDS); Assert.fail(); } catch (InvalidConfigException e) {} try { ConfigHelper.parseDuration("-2", TimeUnit.MILLISECONDS); Assert.fail(); } catch (InvalidConfigException e) {} }