Pollers { static long[] parse(String pollers) { String[] periods = pollers.split(",\\s*"); long[] result = new long[periods.length]; boolean errors = false; Logger logger = LoggerFactory.getLogger(Pollers.class); for (int i = 0; i < periods.length; ++i) { String period = periods[i]; try { result[i] = Long.parseLong(period); if (result[i] <= 0) { logger.error("Invalid polling interval: {} must be positive.", period); errors = true; } } catch (NumberFormatException e) { logger.error("Cannot parse '{}' as a long: {}", period, e.getMessage()); errors = true; } } if (errors || periods.length == 0) { logger.info("Using a default configuration for poller intervals: {}", join(DEFAULT_PERIODS)); return DEFAULT_PERIODS; } else { return result; } } private  Pollers(); static List<Long> getPollingIntervals(); static final String POLLERS; static final int NUM_POLLERS; }
@Test public void testParseOneEntry() throws Exception { long[] expected1 = {1L}; assertEquals(Pollers.parse("1"), expected1); long[] expected2 = {42000L}; assertEquals(Pollers.parse("42000"), expected2); }
@Test public void testParseInvalid() throws Exception { assertEquals(Pollers.parse("0"), Pollers.DEFAULT_PERIODS); assertEquals(Pollers.parse("-1"), Pollers.DEFAULT_PERIODS); assertEquals(Pollers.parse("1L"), Pollers.DEFAULT_PERIODS); assertEquals(Pollers.parse("100,-1"), Pollers.DEFAULT_PERIODS); assertEquals(Pollers.parse("100,0"), Pollers.DEFAULT_PERIODS); }
@Test public void testParseMultiple() throws Exception { long[] expected = {60000L, 10000L, 2000L}; assertEquals(Pollers.parse("60000,10000,2000"), expected); }