PeriodParser { public static long parse(String period) throws IllegalArgumentException { if (period == null) { return 0; } Pattern pattern = Pattern.compile("\\d+"); Matcher matcher = pattern.matcher(period); if (matcher.matches()) { return new Long(period); } pattern = Pattern.compile("(\\d+)MILLISECONDS"); matcher = pattern.matcher(period); if (matcher.matches()) { return new Long(matcher.group(1)); } pattern = Pattern.compile("(\\d+)SECONDS"); matcher = pattern.matcher(period); if (matcher.matches()) { return new Long(matcher.group(1)) * 1000; } pattern = Pattern.compile("(\\d+)MINUTES"); matcher = pattern.matcher(period); if (matcher.matches()) { return new Long(matcher.group(1)) * 60 * 1000; } pattern = Pattern.compile("(\\d+)HOURS"); matcher = pattern.matcher(period); if (matcher.matches()) { return new Long(matcher.group(1)) * 60 * 60 * 1000; } throw new IllegalStateException("Invalid period syntax: " + period); }  static long parse(String period);  }
@Test public void test() throws Exception { long period = PeriodParser.parse("450"); Assert.assertEquals(450, period); period = PeriodParser.parse("200MILLISECONDS"); Assert.assertEquals(200, period); period = PeriodParser.parse("2SECONDS"); Assert.assertEquals(2 * 1000, period); period = PeriodParser.parse("5MINUTES"); Assert.assertEquals(5 * 60 * 1000, period); period = PeriodParser.parse("2HOURS"); Assert.assertEquals(2 * 60 * 60 * 1000, period); }