ConfigUtil { public static long getDurationMillis(String duration) { Matcher matcher = NUMBER_AND_UNIT.matcher(duration); if (matcher.matches()) { long number = Long.parseLong(matcher.group(1)); if (matcher.group(2) != null) { String unitStr = matcher.group(2).toLowerCase(); char unit = unitStr.charAt(0); switch (unit) { case 's': return number * 1000; case 'm': if (unitStr.length() >= 2 && unitStr.charAt(1) == 's') { return number; } return number * 60 * 1000; case 'h': return number * 60 * 60 * 1000; case 'd': return number * 60 * 60 * 24 * 1000; case 'y': return number * 365 * 60 * 60 * 24 * 1000; default: throw new ConfigException("unknown time unit :" + unit); } } else { return number; } } else { throw new ConfigException("malformed duration string: " + duration); } }  static long getDurationMillis(String duration); static long getSizeBytes(String size);  }
@Test(groups = "fast") public void testDurationSanity() throws Exception { Assert.assertEquals(ConfigUtil.getDurationMillis("61"), 61); Assert.assertEquals(ConfigUtil.getDurationMillis("59ms"), 59); Assert.assertEquals(ConfigUtil.getDurationMillis("100s"), 100000); Assert.assertEquals(ConfigUtil.getDurationMillis("16m"), 960000); Assert.assertEquals(ConfigUtil.getDurationMillis("1h"), 3600000); Assert.assertEquals(ConfigUtil.getDurationMillis("2h"), 7200000); Assert.assertEquals(ConfigUtil.getDurationMillis("1d"), 86400000); Assert.assertEquals(ConfigUtil.getDurationMillis("30d"), 2592000000L); Assert.assertEquals(ConfigUtil.getDurationMillis("2y"), 63072000000L); }