public void removeListener(final StatisticsListener listener) { if (listener == null) { throw new NullPointerException("StatisticsListener is null"); } synchronized (this.mutex) { if (this.listeners.contains(listener)) { this.listeners.remove(listener); if (this.listeners.isEmpty()) { try { getStatMonitorHandler().removeMonitor(this); } catch (IllegalStateException ignore) { // sample collector and handlers were closed (ok on removal) } } } } }
@Test public void testRemoveMissingMonitorReturnsFalse() throws Exception { StatMonitorHandler handler = new StatMonitorHandler(); assertTrue(handler.getMonitorsSnapshot().isEmpty()); StatisticsMonitor monitor = new TestStatisticsMonitor(); assertFalse(handler.getMonitorsSnapshot().contains(monitor)); assertFalse(handler.removeMonitor(monitor)); assertTrue(handler.getMonitorsSnapshot().isEmpty()); }
@Test public void testRemoveExistingMonitor() throws Exception { StatMonitorHandler handler = new StatMonitorHandler(); assertTrue(handler.getMonitorsSnapshot().isEmpty()); TestStatisticsMonitor monitor = new TestStatisticsMonitor(); assertTrue(handler.addMonitor(monitor)); assertFalse(handler.getMonitorsSnapshot().isEmpty()); assertTrue(handler.getMonitorsSnapshot().contains(monitor)); assertTrue(handler.removeMonitor(monitor)); assertFalse(handler.getMonitorsSnapshot().contains(monitor)); assertTrue(handler.getMonitorsSnapshot().isEmpty()); handler.sampled(NanoTimer.getTime(), Collections.<ResourceInstance>emptyList()); assertEquals(0, monitor.getNotificationCount()); }